blob: 105ba025fbe618af195a83bfd75df5325c6cb57a [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 Moolenaara800b422010-06-27 01:15:55 +0200448static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000450static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
452static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000453static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000455static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000457static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
458static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
461static int string2float __ARGS((char_u *text, float_T *value));
462#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
464static int find_internal_func __ARGS((char_u *name));
465static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
466static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200467static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000468static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000469static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
472static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200473static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000480#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200481static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200483static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
497static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
498#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000499static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000502static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000504#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000505static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000506static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#ifdef FEAT_FLOAT
512static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200513static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
518static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200529#ifdef FEAT_FLOAT
530static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
531#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000534static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#ifdef FEAT_FLOAT
541static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200543static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000544#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000545static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000554static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000555static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000556static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000562static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000570static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000571static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000572static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000573static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200576static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000577static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000585static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000599static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000605static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000606static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000617#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200618static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000619static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000626static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000627static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000629static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000633#ifdef vim_mkdir
634static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100637#ifdef FEAT_MZSCHEME
638static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
641static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000642static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000643#ifdef FEAT_FLOAT
644static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
645#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000647static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000648static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000650static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000651static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000663#ifdef FEAT_FLOAT
664static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
665#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000667static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000669static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000676static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000677static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000678static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000679static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200681static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000682static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000684static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000686#ifdef FEAT_FLOAT
687static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200688static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000691static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000692static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
698#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000699static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700#ifdef HAVE_STRFTIME
701static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
702#endif
703static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000714static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000716static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000717static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000718static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000719static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000720static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000722static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200723#ifdef FEAT_FLOAT
724static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
726#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
731static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
732#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200734static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200735static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000745static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000748static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000750static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000751static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752static int get_env_len __ARGS((char_u **arg));
753static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000754static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000755static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
756#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
757#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
758 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000759static 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 +0000760static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000761static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000762static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
763static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000764static typval_T *alloc_tv __ARGS((void));
765static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000766static void init_tv __ARGS((typval_T *varp));
767static long get_tv_number __ARGS((typval_T *varp));
768static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000769static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static char_u *get_tv_string __ARGS((typval_T *varp));
771static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000772static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000774static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
776static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
777static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000778static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
779static 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 +0000780static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
781static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000782static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000783static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000784static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000785static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
786static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
787static int eval_fname_script __ARGS((char_u *p));
788static int eval_fname_sid __ARGS((char_u *p));
789static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static ufunc_T *find_func __ARGS((char_u *name));
791static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000792static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000793#ifdef FEAT_PROFILE
794static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000795static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
796static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
797static int
798# ifdef __BORLANDC__
799 _RTLENTRYF
800# endif
801 prof_total_cmp __ARGS((const void *s1, const void *s2));
802static int
803# ifdef __BORLANDC__
804 _RTLENTRYF
805# endif
806 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000808static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000809static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000810static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000811static void func_free __ARGS((ufunc_T *fp));
812static void func_unref __ARGS((char_u *name));
813static void func_ref __ARGS((char_u *name));
814static 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 +0000815static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
816static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000817static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000818static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
819static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000820static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000821static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000822static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200824
825#ifdef EBCDIC
826static int compare_func_name __ARGS((const void *s1, const void *s2));
827static void sortFunctions __ARGS(());
828#endif
829
830
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000831/* Character used as separated in autoload function/variable names. */
832#define AUTOLOAD_CHAR '#'
833
Bram Moolenaar33570922005-01-25 22:26:29 +0000834/*
835 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000836 */
837 void
838eval_init()
839{
Bram Moolenaar33570922005-01-25 22:26:29 +0000840 int i;
841 struct vimvar *p;
842
843 init_var_dict(&globvardict, &globvars_var);
844 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000845 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000846 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000847
848 for (i = 0; i < VV_LEN; ++i)
849 {
850 p = &vimvars[i];
851 STRCPY(p->vv_di.di_key, p->vv_name);
852 if (p->vv_flags & VV_RO)
853 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
854 else if (p->vv_flags & VV_RO_SBX)
855 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
856 else
857 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000858
859 /* add to v: scope dict, unless the value is not always available */
860 if (p->vv_type != VAR_UNKNOWN)
861 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000863 /* add to compat scope dict */
864 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000865 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000866 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200867
868#ifdef EBCDIC
869 /*
870 * Sort the function table, to enable binary sort.
871 */
872 sortFunctions();
873#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000874}
875
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000876#if defined(EXITFREE) || defined(PROTO)
877 void
878eval_clear()
879{
880 int i;
881 struct vimvar *p;
882
883 for (i = 0; i < VV_LEN; ++i)
884 {
885 p = &vimvars[i];
886 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000887 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000888 vim_free(p->vv_str);
889 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000890 }
891 else if (p->vv_di.di_tv.v_type == VAR_LIST)
892 {
893 list_unref(p->vv_list);
894 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000895 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000896 }
897 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000898 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000899 hash_clear(&compat_hashtab);
900
Bram Moolenaard9fba312005-06-26 22:34:35 +0000901 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000902
903 /* global variables */
904 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000905
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000906 /* autoloaded script names */
907 ga_clear_strings(&ga_loaded);
908
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200909 /* script-local variables */
910 for (i = 1; i <= ga_scripts.ga_len; ++i)
911 {
912 vars_clear(&SCRIPT_VARS(i));
913 vim_free(SCRIPT_SV(i));
914 }
915 ga_clear(&ga_scripts);
916
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000917 /* unreferenced lists and dicts */
918 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000919
920 /* functions */
921 free_all_functions();
922 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000923}
924#endif
925
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 * Return the name of the executed function.
928 */
929 char_u *
930func_name(cookie)
931 void *cookie;
932{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000933 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934}
935
936/*
937 * Return the address holding the next breakpoint line for a funccall cookie.
938 */
939 linenr_T *
940func_breakpoint(cookie)
941 void *cookie;
942{
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944}
945
946/*
947 * Return the address holding the debug tick for a funccall cookie.
948 */
949 int *
950func_dbg_tick(cookie)
951 void *cookie;
952{
Bram Moolenaar33570922005-01-25 22:26:29 +0000953 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954}
955
956/*
957 * Return the nesting level for a funccall cookie.
958 */
959 int
960func_level(cookie)
961 void *cookie;
962{
Bram Moolenaar33570922005-01-25 22:26:29 +0000963 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964}
965
966/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000967funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000969/* pointer to list of previously used funccal, still around because some
970 * item in it is still being used. */
971funccall_T *previous_funccal = NULL;
972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973/*
974 * Return TRUE when a function was ended by a ":return" command.
975 */
976 int
977current_func_returned()
978{
979 return current_funccal->returned;
980}
981
982
983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 * Set an internal variable to a string value. Creates the variable if it does
985 * not already exist.
986 */
987 void
988set_internal_string_var(name, value)
989 char_u *name;
990 char_u *value;
991{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000992 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000993 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995 val = vim_strsave(value);
996 if (val != NULL)
997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000998 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000999 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001002 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
1004 }
1005}
1006
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001007static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001008static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001009static char_u *redir_endp = NULL;
1010static char_u *redir_varname = NULL;
1011
1012/*
1013 * Start recording command output to a variable
1014 * Returns OK if successfully completed the setup. FAIL otherwise.
1015 */
1016 int
1017var_redir_start(name, append)
1018 char_u *name;
1019 int append; /* append to an existing variable */
1020{
1021 int save_emsg;
1022 int err;
1023 typval_T tv;
1024
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001025 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001026 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001027 {
1028 EMSG(_(e_invarg));
1029 return FAIL;
1030 }
1031
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001032 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001033 redir_varname = vim_strsave(name);
1034 if (redir_varname == NULL)
1035 return FAIL;
1036
1037 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1038 if (redir_lval == NULL)
1039 {
1040 var_redir_stop();
1041 return FAIL;
1042 }
1043
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001044 /* The output is stored in growarray "redir_ga" until redirection ends. */
1045 ga_init2(&redir_ga, (int)sizeof(char), 500);
1046
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001047 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001048 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1049 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1051 {
1052 if (redir_endp != NULL && *redir_endp != NUL)
1053 /* Trailing characters are present after the variable name */
1054 EMSG(_(e_trailing));
1055 else
1056 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001057 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001058 var_redir_stop();
1059 return FAIL;
1060 }
1061
1062 /* check if we can write to the variable: set it to or append an empty
1063 * string */
1064 save_emsg = did_emsg;
1065 did_emsg = FALSE;
1066 tv.v_type = VAR_STRING;
1067 tv.vval.v_string = (char_u *)"";
1068 if (append)
1069 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1070 else
1071 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1072 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001073 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001074 if (err)
1075 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001076 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001077 var_redir_stop();
1078 return FAIL;
1079 }
1080 if (redir_lval->ll_newkey != NULL)
1081 {
1082 /* Dictionary item was created, don't do it again. */
1083 vim_free(redir_lval->ll_newkey);
1084 redir_lval->ll_newkey = NULL;
1085 }
1086
1087 return OK;
1088}
1089
1090/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001091 * Append "value[value_len]" to the variable set by var_redir_start().
1092 * The actual appending is postponed until redirection ends, because the value
1093 * appended may in fact be the string we write to, changing it may cause freed
1094 * memory to be used:
1095 * :redir => foo
1096 * :let foo
1097 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 */
1099 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001102 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001104 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105
1106 if (redir_lval == NULL)
1107 return;
1108
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001110 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001114 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115 {
1116 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 }
1119 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121}
1122
1123/*
1124 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001125 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 */
1127 void
1128var_redir_stop()
1129{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 typval_T tv;
1131
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 if (redir_lval != NULL)
1133 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 /* If there was no error: assign the text to the variable. */
1135 if (redir_endp != NULL)
1136 {
1137 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1138 tv.v_type = VAR_STRING;
1139 tv.vval.v_string = redir_ga.ga_data;
1140 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1141 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 /* free the collected output */
1144 vim_free(redir_ga.ga_data);
1145 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001146
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 clear_lval(redir_lval);
1148 vim_free(redir_lval);
1149 redir_lval = NULL;
1150 }
1151 vim_free(redir_varname);
1152 redir_varname = NULL;
1153}
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155# if defined(FEAT_MBYTE) || defined(PROTO)
1156 int
1157eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1158 char_u *enc_from;
1159 char_u *enc_to;
1160 char_u *fname_from;
1161 char_u *fname_to;
1162{
1163 int err = FALSE;
1164
1165 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1166 set_vim_var_string(VV_CC_TO, enc_to, -1);
1167 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1168 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1169 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1170 err = TRUE;
1171 set_vim_var_string(VV_CC_FROM, NULL, -1);
1172 set_vim_var_string(VV_CC_TO, NULL, -1);
1173 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1174 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1175
1176 if (err)
1177 return FAIL;
1178 return OK;
1179}
1180# endif
1181
1182# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1183 int
1184eval_printexpr(fname, args)
1185 char_u *fname;
1186 char_u *args;
1187{
1188 int err = FALSE;
1189
1190 set_vim_var_string(VV_FNAME_IN, fname, -1);
1191 set_vim_var_string(VV_CMDARG, args, -1);
1192 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1193 err = TRUE;
1194 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1195 set_vim_var_string(VV_CMDARG, NULL, -1);
1196
1197 if (err)
1198 {
1199 mch_remove(fname);
1200 return FAIL;
1201 }
1202 return OK;
1203}
1204# endif
1205
1206# if defined(FEAT_DIFF) || defined(PROTO)
1207 void
1208eval_diff(origfile, newfile, outfile)
1209 char_u *origfile;
1210 char_u *newfile;
1211 char_u *outfile;
1212{
1213 int err = FALSE;
1214
1215 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1216 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1217 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1218 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1219 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1220 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1221 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1222}
1223
1224 void
1225eval_patch(origfile, difffile, outfile)
1226 char_u *origfile;
1227 char_u *difffile;
1228 char_u *outfile;
1229{
1230 int err;
1231
1232 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1233 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1234 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1235 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1236 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1237 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1238 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1239}
1240# endif
1241
1242/*
1243 * Top level evaluation function, returning a boolean.
1244 * Sets "error" to TRUE if there was an error.
1245 * Return TRUE or FALSE.
1246 */
1247 int
1248eval_to_bool(arg, error, nextcmd, skip)
1249 char_u *arg;
1250 int *error;
1251 char_u **nextcmd;
1252 int skip; /* only parse, don't execute */
1253{
Bram Moolenaar33570922005-01-25 22:26:29 +00001254 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 int retval = FALSE;
1256
1257 if (skip)
1258 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001259 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 else
1262 {
1263 *error = FALSE;
1264 if (!skip)
1265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001266 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269 }
1270 if (skip)
1271 --emsg_skip;
1272
1273 return retval;
1274}
1275
1276/*
1277 * Top level evaluation function, returning a string. If "skip" is TRUE,
1278 * only parsing to "nextcmd" is done, without reporting errors. Return
1279 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1280 */
1281 char_u *
1282eval_to_string_skip(arg, nextcmd, skip)
1283 char_u *arg;
1284 char_u **nextcmd;
1285 int skip; /* only parse, don't execute */
1286{
Bram Moolenaar33570922005-01-25 22:26:29 +00001287 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 char_u *retval;
1289
1290 if (skip)
1291 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001292 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 retval = NULL;
1294 else
1295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001296 retval = vim_strsave(get_tv_string(&tv));
1297 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 if (skip)
1300 --emsg_skip;
1301
1302 return retval;
1303}
1304
1305/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001306 * Skip over an expression at "*pp".
1307 * Return FAIL for an error, OK otherwise.
1308 */
1309 int
1310skip_expr(pp)
1311 char_u **pp;
1312{
Bram Moolenaar33570922005-01-25 22:26:29 +00001313 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001314
1315 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001316 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001317}
1318
1319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001321 * When "convert" is TRUE convert a List into a sequence of lines and convert
1322 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 * Return pointer to allocated memory, or NULL for failure.
1324 */
1325 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001326eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 char_u *arg;
1328 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001329 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
Bram Moolenaar33570922005-01-25 22:26:29 +00001331 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001333 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001334#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001335 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001338 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 retval = NULL;
1340 else
1341 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001342 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001343 {
1344 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001345 if (tv.vval.v_list != NULL)
1346 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001347 ga_append(&ga, NUL);
1348 retval = (char_u *)ga.ga_data;
1349 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001350#ifdef FEAT_FLOAT
1351 else if (convert && tv.v_type == VAR_FLOAT)
1352 {
1353 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1354 retval = vim_strsave(numbuf);
1355 }
1356#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001357 else
1358 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361
1362 return retval;
1363}
1364
1365/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001366 * Call eval_to_string() without using current local variables and using
1367 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 */
1369 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001370eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 char_u *arg;
1372 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001373 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
1375 char_u *retval;
1376 void *save_funccalp;
1377
1378 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001379 if (use_sandbox)
1380 ++sandbox;
1381 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001382 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383 if (use_sandbox)
1384 --sandbox;
1385 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 restore_funccal(save_funccalp);
1387 return retval;
1388}
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390/*
1391 * Top level evaluation function, returning a number.
1392 * Evaluates "expr" silently.
1393 * Returns -1 for an error.
1394 */
1395 int
1396eval_to_number(expr)
1397 char_u *expr;
1398{
Bram Moolenaar33570922005-01-25 22:26:29 +00001399 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001401 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402
1403 ++emsg_off;
1404
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001405 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 retval = -1;
1407 else
1408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001409 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001410 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
1412 --emsg_off;
1413
1414 return retval;
1415}
1416
Bram Moolenaara40058a2005-07-11 22:42:07 +00001417/*
1418 * Prepare v: variable "idx" to be used.
1419 * Save the current typeval in "save_tv".
1420 * When not used yet add the variable to the v: hashtable.
1421 */
1422 static void
1423prepare_vimvar(idx, save_tv)
1424 int idx;
1425 typval_T *save_tv;
1426{
1427 *save_tv = vimvars[idx].vv_tv;
1428 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1429 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1430}
1431
1432/*
1433 * Restore v: variable "idx" to typeval "save_tv".
1434 * When no longer defined, remove the variable from the v: hashtable.
1435 */
1436 static void
1437restore_vimvar(idx, save_tv)
1438 int idx;
1439 typval_T *save_tv;
1440{
1441 hashitem_T *hi;
1442
Bram Moolenaara40058a2005-07-11 22:42:07 +00001443 vimvars[idx].vv_tv = *save_tv;
1444 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1445 {
1446 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1447 if (HASHITEM_EMPTY(hi))
1448 EMSG2(_(e_intern2), "restore_vimvar()");
1449 else
1450 hash_remove(&vimvarht, hi);
1451 }
1452}
1453
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001454#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001455/*
1456 * Evaluate an expression to a list with suggestions.
1457 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001458 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001459 */
1460 list_T *
1461eval_spell_expr(badword, expr)
1462 char_u *badword;
1463 char_u *expr;
1464{
1465 typval_T save_val;
1466 typval_T rettv;
1467 list_T *list = NULL;
1468 char_u *p = skipwhite(expr);
1469
1470 /* Set "v:val" to the bad word. */
1471 prepare_vimvar(VV_VAL, &save_val);
1472 vimvars[VV_VAL].vv_type = VAR_STRING;
1473 vimvars[VV_VAL].vv_str = badword;
1474 if (p_verbose == 0)
1475 ++emsg_off;
1476
1477 if (eval1(&p, &rettv, TRUE) == OK)
1478 {
1479 if (rettv.v_type != VAR_LIST)
1480 clear_tv(&rettv);
1481 else
1482 list = rettv.vval.v_list;
1483 }
1484
1485 if (p_verbose == 0)
1486 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001487 restore_vimvar(VV_VAL, &save_val);
1488
1489 return list;
1490}
1491
1492/*
1493 * "list" is supposed to contain two items: a word and a number. Return the
1494 * word in "pp" and the number as the return value.
1495 * Return -1 if anything isn't right.
1496 * Used to get the good word and score from the eval_spell_expr() result.
1497 */
1498 int
1499get_spellword(list, pp)
1500 list_T *list;
1501 char_u **pp;
1502{
1503 listitem_T *li;
1504
1505 li = list->lv_first;
1506 if (li == NULL)
1507 return -1;
1508 *pp = get_tv_string(&li->li_tv);
1509
1510 li = li->li_next;
1511 if (li == NULL)
1512 return -1;
1513 return get_tv_number(&li->li_tv);
1514}
1515#endif
1516
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001517/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001518 * Top level evaluation function.
1519 * Returns an allocated typval_T with the result.
1520 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001521 */
1522 typval_T *
1523eval_expr(arg, nextcmd)
1524 char_u *arg;
1525 char_u **nextcmd;
1526{
1527 typval_T *tv;
1528
1529 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001530 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001531 {
1532 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001533 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001534 }
1535
1536 return tv;
1537}
1538
1539
Bram Moolenaar4f688582007-07-24 12:34:30 +00001540#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1541 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001543 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001544 * Uses argv[argc] for the function arguments. Only Number and String
1545 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001546 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001548 static int
1549call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 char_u *func;
1551 int argc;
1552 char_u **argv;
1553 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
Bram Moolenaar33570922005-01-25 22:26:29 +00001556 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 long n;
1558 int len;
1559 int i;
1560 int doesrange;
1561 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001564 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001566 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
1568 for (i = 0; i < argc; i++)
1569 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001570 /* Pass a NULL or empty argument as an empty string */
1571 if (argv[i] == NULL || *argv[i] == NUL)
1572 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001573 argvars[i].v_type = VAR_STRING;
1574 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001575 continue;
1576 }
1577
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 /* Recognize a number argument, the others must be strings. */
1579 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1580 if (len != 0 && len == (int)STRLEN(argv[i]))
1581 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001582 argvars[i].v_type = VAR_NUMBER;
1583 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
1585 else
1586 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001587 argvars[i].v_type = VAR_STRING;
1588 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 }
1590 }
1591
1592 if (safe)
1593 {
1594 save_funccalp = save_funccal();
1595 ++sandbox;
1596 }
1597
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001598 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1599 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001601 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (safe)
1603 {
1604 --sandbox;
1605 restore_funccal(save_funccalp);
1606 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001607 vim_free(argvars);
1608
1609 if (ret == FAIL)
1610 clear_tv(rettv);
1611
1612 return ret;
1613}
1614
Bram Moolenaar4f688582007-07-24 12:34:30 +00001615# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001616/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001617 * Call vimL function "func" and return the result as a string.
1618 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 * Uses argv[argc] for the function arguments.
1620 */
1621 void *
1622call_func_retstr(func, argc, argv, safe)
1623 char_u *func;
1624 int argc;
1625 char_u **argv;
1626 int safe; /* use the sandbox */
1627{
1628 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001629 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630
1631 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1632 return NULL;
1633
1634 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001635 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 return retval;
1637}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001638# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001639
Bram Moolenaar4f688582007-07-24 12:34:30 +00001640# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001642 * Call vimL function "func" and return the result as a number.
1643 * Returns -1 when calling the function fails.
1644 * Uses argv[argc] for the function arguments.
1645 */
1646 long
1647call_func_retnr(func, argc, argv, safe)
1648 char_u *func;
1649 int argc;
1650 char_u **argv;
1651 int safe; /* use the sandbox */
1652{
1653 typval_T rettv;
1654 long retval;
1655
1656 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1657 return -1;
1658
1659 retval = get_tv_number_chk(&rettv, NULL);
1660 clear_tv(&rettv);
1661 return retval;
1662}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001663# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001664
1665/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001666 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001667 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001668 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 */
1670 void *
1671call_func_retlist(func, argc, argv, safe)
1672 char_u *func;
1673 int argc;
1674 char_u **argv;
1675 int safe; /* use the sandbox */
1676{
1677 typval_T rettv;
1678
1679 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1680 return NULL;
1681
1682 if (rettv.v_type != VAR_LIST)
1683 {
1684 clear_tv(&rettv);
1685 return NULL;
1686 }
1687
1688 return rettv.vval.v_list;
1689}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690#endif
1691
Bram Moolenaar4f688582007-07-24 12:34:30 +00001692
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693/*
1694 * Save the current function call pointer, and set it to NULL.
1695 * Used when executing autocommands and for ":source".
1696 */
1697 void *
1698save_funccal()
1699{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001700 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 current_funccal = NULL;
1703 return (void *)fc;
1704}
1705
1706 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001707restore_funccal(vfc)
1708 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001710 funccall_T *fc = (funccall_T *)vfc;
1711
1712 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713}
1714
Bram Moolenaar05159a02005-02-26 23:04:13 +00001715#if defined(FEAT_PROFILE) || defined(PROTO)
1716/*
1717 * Prepare profiling for entering a child or something else that is not
1718 * counted for the script/function itself.
1719 * Should always be called in pair with prof_child_exit().
1720 */
1721 void
1722prof_child_enter(tm)
1723 proftime_T *tm; /* place to store waittime */
1724{
1725 funccall_T *fc = current_funccal;
1726
1727 if (fc != NULL && fc->func->uf_profiling)
1728 profile_start(&fc->prof_child);
1729 script_prof_save(tm);
1730}
1731
1732/*
1733 * Take care of time spent in a child.
1734 * Should always be called after prof_child_enter().
1735 */
1736 void
1737prof_child_exit(tm)
1738 proftime_T *tm; /* where waittime was stored */
1739{
1740 funccall_T *fc = current_funccal;
1741
1742 if (fc != NULL && fc->func->uf_profiling)
1743 {
1744 profile_end(&fc->prof_child);
1745 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1746 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1747 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1748 }
1749 script_prof_restore(tm);
1750}
1751#endif
1752
1753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754#ifdef FEAT_FOLDING
1755/*
1756 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1757 * it in "*cp". Doesn't give error messages.
1758 */
1759 int
1760eval_foldexpr(arg, cp)
1761 char_u *arg;
1762 int *cp;
1763{
Bram Moolenaar33570922005-01-25 22:26:29 +00001764 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 int retval;
1766 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001767 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1768 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
1770 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001771 if (use_sandbox)
1772 ++sandbox;
1773 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 retval = 0;
1777 else
1778 {
1779 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001780 if (tv.v_type == VAR_NUMBER)
1781 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001782 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 retval = 0;
1784 else
1785 {
1786 /* If the result is a string, check if there is a non-digit before
1787 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 if (!VIM_ISDIGIT(*s) && *s != '-')
1790 *cp = *s++;
1791 retval = atol((char *)s);
1792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 }
1795 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001796 if (use_sandbox)
1797 --sandbox;
1798 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800 return retval;
1801}
1802#endif
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 * ":let" list all variable values
1806 * ":let var1 var2" list variable values
1807 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001808 * ":let var += expr" assignment command.
1809 * ":let var -= expr" assignment command.
1810 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 */
1813 void
1814ex_let(eap)
1815 exarg_T *eap;
1816{
1817 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001819 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 int var_count = 0;
1822 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001823 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001824 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001825 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
Bram Moolenaardb552d602006-03-23 22:59:57 +00001827 argend = skip_var_list(arg, &var_count, &semicolon);
1828 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001830 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1831 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001832 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001833 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001835 /*
1836 * ":let" without "=": list variables
1837 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838 if (*arg == '[')
1839 EMSG(_(e_invarg));
1840 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001842 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001845 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001846 list_glob_vars(&first);
1847 list_buf_vars(&first);
1848 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001849#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001850 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001851#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001852 list_script_vars(&first);
1853 list_func_vars(&first);
1854 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 eap->nextcmd = check_nextcmd(arg);
1857 }
1858 else
1859 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001860 op[0] = '=';
1861 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001862 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001863 {
1864 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1865 op[0] = expr[-1]; /* +=, -= or .= */
1866 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (eap->skip)
1870 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (eap->skip)
1873 {
1874 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 --emsg_skip;
1877 }
1878 else if (i != FAIL)
1879 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 }
1884 }
1885}
1886
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887/*
1888 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1889 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001890 * When "nextchars" is not NULL it points to a string with characters that
1891 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1892 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 * Returns OK or FAIL;
1894 */
1895 static int
1896ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1897 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001898 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 int copy; /* copy values from "tv", don't move */
1900 int semicolon; /* from skip_var_list() */
1901 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001902 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903{
1904 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001905 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001907 listitem_T *item;
1908 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909
1910 if (*arg != '[')
1911 {
1912 /*
1913 * ":let var = expr" or ":for var in list"
1914 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 return FAIL;
1917 return OK;
1918 }
1919
1920 /*
1921 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1922 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001923 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 {
1925 EMSG(_(e_listreq));
1926 return FAIL;
1927 }
1928
1929 i = list_len(l);
1930 if (semicolon == 0 && var_count < i)
1931 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001932 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933 return FAIL;
1934 }
1935 if (var_count - semicolon > i)
1936 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001937 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 return FAIL;
1939 }
1940
1941 item = l->lv_first;
1942 while (*arg != ']')
1943 {
1944 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 item = item->li_next;
1947 if (arg == NULL)
1948 return FAIL;
1949
1950 arg = skipwhite(arg);
1951 if (*arg == ';')
1952 {
1953 /* Put the rest of the list (may be empty) in the var after ';'.
1954 * Create a new list for this. */
1955 l = list_alloc();
1956 if (l == NULL)
1957 return FAIL;
1958 while (item != NULL)
1959 {
1960 list_append_tv(l, &item->li_tv);
1961 item = item->li_next;
1962 }
1963
1964 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001965 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 ltv.vval.v_list = l;
1967 l->lv_refcount = 1;
1968
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1970 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 clear_tv(&ltv);
1972 if (arg == NULL)
1973 return FAIL;
1974 break;
1975 }
1976 else if (*arg != ',' && *arg != ']')
1977 {
1978 EMSG2(_(e_intern2), "ex_let_vars()");
1979 return FAIL;
1980 }
1981 }
1982
1983 return OK;
1984}
1985
1986/*
1987 * Skip over assignable variable "var" or list of variables "[var, var]".
1988 * Used for ":let varvar = expr" and ":for varvar in expr".
1989 * For "[var, var]" increment "*var_count" for each variable.
1990 * for "[var, var; var]" set "semicolon".
1991 * Return NULL for an error.
1992 */
1993 static char_u *
1994skip_var_list(arg, var_count, semicolon)
1995 char_u *arg;
1996 int *var_count;
1997 int *semicolon;
1998{
1999 char_u *p, *s;
2000
2001 if (*arg == '[')
2002 {
2003 /* "[var, var]": find the matching ']'. */
2004 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002005 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 {
2007 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2008 s = skip_var_one(p);
2009 if (s == p)
2010 {
2011 EMSG2(_(e_invarg2), p);
2012 return NULL;
2013 }
2014 ++*var_count;
2015
2016 p = skipwhite(s);
2017 if (*p == ']')
2018 break;
2019 else if (*p == ';')
2020 {
2021 if (*semicolon == 1)
2022 {
2023 EMSG(_("Double ; in list of variables"));
2024 return NULL;
2025 }
2026 *semicolon = 1;
2027 }
2028 else if (*p != ',')
2029 {
2030 EMSG2(_(e_invarg2), p);
2031 return NULL;
2032 }
2033 }
2034 return p + 1;
2035 }
2036 else
2037 return skip_var_one(arg);
2038}
2039
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002040/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002041 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002042 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002043 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 static char_u *
2045skip_var_one(arg)
2046 char_u *arg;
2047{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002048 if (*arg == '@' && arg[1] != NUL)
2049 return arg + 2;
2050 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2051 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052}
2053
Bram Moolenaara7043832005-01-21 11:56:39 +00002054/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002055 * List variables for hashtab "ht" with prefix "prefix".
2056 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002057 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002058 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002059list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002060 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002061 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002062 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002063 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002064{
Bram Moolenaar33570922005-01-25 22:26:29 +00002065 hashitem_T *hi;
2066 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002067 int todo;
2068
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002069 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2071 {
2072 if (!HASHITEM_EMPTY(hi))
2073 {
2074 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002075 di = HI2DI(hi);
2076 if (empty || di->di_tv.v_type != VAR_STRING
2077 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002078 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 }
2080 }
2081}
2082
2083/*
2084 * List global variables.
2085 */
2086 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002087list_glob_vars(first)
2088 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002090 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002091}
2092
2093/*
2094 * List buffer variables.
2095 */
2096 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097list_buf_vars(first)
2098 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002100 char_u numbuf[NUMBUFLEN];
2101
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002102 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2103 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002104
2105 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2107 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002108}
2109
2110/*
2111 * List window variables.
2112 */
2113 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114list_win_vars(first)
2115 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002116{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2118 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002119}
2120
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002121#ifdef FEAT_WINDOWS
2122/*
2123 * List tab page variables.
2124 */
2125 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126list_tab_vars(first)
2127 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002128{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2130 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002131}
2132#endif
2133
Bram Moolenaara7043832005-01-21 11:56:39 +00002134/*
2135 * List Vim variables.
2136 */
2137 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138list_vim_vars(first)
2139 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142}
2143
2144/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002145 * List script-local variables, if there is a script.
2146 */
2147 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148list_script_vars(first)
2149 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002150{
2151 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2153 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002154}
2155
2156/*
2157 * List function variables, if there is a function.
2158 */
2159 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160list_func_vars(first)
2161 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002162{
2163 if (current_funccal != NULL)
2164 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002166}
2167
2168/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002169 * List variables in "arg".
2170 */
2171 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 exarg_T *eap;
2174 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176{
2177 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002178 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002180 char_u *name_start;
2181 char_u *arg_subsc;
2182 char_u *tofree;
2183 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184
2185 while (!ends_excmd(*arg) && !got_int)
2186 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002187 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002189 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2191 {
2192 emsg_severe = TRUE;
2193 EMSG(_(e_trailing));
2194 break;
2195 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002199 /* get_name_len() takes care of expanding curly braces */
2200 name_start = name = arg;
2201 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2202 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 /* This is mainly to keep test 49 working: when expanding
2205 * curly braces fails overrule the exception error message. */
2206 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002208 emsg_severe = TRUE;
2209 EMSG2(_(e_invarg2), arg);
2210 break;
2211 }
2212 error = TRUE;
2213 }
2214 else
2215 {
2216 if (tofree != NULL)
2217 name = tofree;
2218 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 else
2221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 /* handle d.key, l[idx], f(expr) */
2223 arg_subsc = arg;
2224 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002225 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002227 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002231 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002232 case 'g': list_glob_vars(first); break;
2233 case 'b': list_buf_vars(first); break;
2234 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002235#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002236 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002237#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002238 case 'v': list_vim_vars(first); break;
2239 case 's': list_script_vars(first); break;
2240 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 default:
2242 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 }
2245 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 {
2247 char_u numbuf[NUMBUFLEN];
2248 char_u *tf;
2249 int c;
2250 char_u *s;
2251
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002252 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 c = *arg;
2254 *arg = NUL;
2255 list_one_var_a((char_u *)"",
2256 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002257 tv.v_type,
2258 s == NULL ? (char_u *)"" : s,
2259 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 *arg = c;
2261 vim_free(tf);
2262 }
2263 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 }
2266 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267
2268 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270
2271 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 }
2273
2274 return arg;
2275}
2276
2277/*
2278 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2279 * Returns a pointer to the char just after the var name.
2280 * Returns NULL if there is an error.
2281 */
2282 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002283ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002285 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002287 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002288 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289{
2290 int c1;
2291 char_u *name;
2292 char_u *p;
2293 char_u *arg_end = NULL;
2294 int len;
2295 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297
2298 /*
2299 * ":let $VAR = expr": Set environment variable.
2300 */
2301 if (*arg == '$')
2302 {
2303 /* Find the end of the name. */
2304 ++arg;
2305 name = arg;
2306 len = get_env_len(&arg);
2307 if (len == 0)
2308 EMSG2(_(e_invarg2), name - 1);
2309 else
2310 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002311 if (op != NULL && (*op == '+' || *op == '-'))
2312 EMSG2(_(e_letwrong), op);
2313 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002314 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 EMSG(_(e_letunexp));
2316 else
2317 {
2318 c1 = name[len];
2319 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002320 p = get_tv_string_chk(tv);
2321 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002322 {
2323 int mustfree = FALSE;
2324 char_u *s = vim_getenv(name, &mustfree);
2325
2326 if (s != NULL)
2327 {
2328 p = tofree = concat_str(s, p);
2329 if (mustfree)
2330 vim_free(s);
2331 }
2332 }
2333 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002334 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002336 if (STRICMP(name, "HOME") == 0)
2337 init_homedir();
2338 else if (didset_vim && STRICMP(name, "VIM") == 0)
2339 didset_vim = FALSE;
2340 else if (didset_vimruntime
2341 && STRICMP(name, "VIMRUNTIME") == 0)
2342 didset_vimruntime = FALSE;
2343 arg_end = arg;
2344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 }
2348 }
2349 }
2350
2351 /*
2352 * ":let &option = expr": Set option value.
2353 * ":let &l:option = expr": Set local option value.
2354 * ":let &g:option = expr": Set global option value.
2355 */
2356 else if (*arg == '&')
2357 {
2358 /* Find the end of the name. */
2359 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002360 if (p == NULL || (endchars != NULL
2361 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362 EMSG(_(e_letunexp));
2363 else
2364 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 long n;
2366 int opt_type;
2367 long numval;
2368 char_u *stringval = NULL;
2369 char_u *s;
2370
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 c1 = *p;
2372 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002373
2374 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002375 s = get_tv_string_chk(tv); /* != NULL if number or string */
2376 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 {
2378 opt_type = get_option_value(arg, &numval,
2379 &stringval, opt_flags);
2380 if ((opt_type == 1 && *op == '.')
2381 || (opt_type == 0 && *op != '.'))
2382 EMSG2(_(e_letwrong), op);
2383 else
2384 {
2385 if (opt_type == 1) /* number */
2386 {
2387 if (*op == '+')
2388 n = numval + n;
2389 else
2390 n = numval - n;
2391 }
2392 else if (opt_type == 0 && stringval != NULL) /* string */
2393 {
2394 s = concat_str(stringval, s);
2395 vim_free(stringval);
2396 stringval = s;
2397 }
2398 }
2399 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002400 if (s != NULL)
2401 {
2402 set_option_value(arg, n, s, opt_flags);
2403 arg_end = p;
2404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002405 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002406 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 }
2408 }
2409
2410 /*
2411 * ":let @r = expr": Set register contents.
2412 */
2413 else if (*arg == '@')
2414 {
2415 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 if (op != NULL && (*op == '+' || *op == '-'))
2417 EMSG2(_(e_letwrong), op);
2418 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002419 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 EMSG(_(e_letunexp));
2421 else
2422 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002423 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 char_u *s;
2425
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002426 p = get_tv_string_chk(tv);
2427 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002429 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 if (s != NULL)
2431 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002432 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433 vim_free(s);
2434 }
2435 }
2436 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002437 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 arg_end = arg + 1;
2440 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002441 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 }
2443 }
2444
2445 /*
2446 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002449 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002451 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002453 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2457 EMSG(_(e_letunexp));
2458 else
2459 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 arg_end = p;
2462 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465 }
2466
2467 else
2468 EMSG2(_(e_invarg2), arg);
2469
2470 return arg_end;
2471}
2472
2473/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2475 */
2476 static int
2477check_changedtick(arg)
2478 char_u *arg;
2479{
2480 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2481 {
2482 EMSG2(_(e_readonlyvar), arg);
2483 return TRUE;
2484 }
2485 return FALSE;
2486}
2487
2488/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * Get an lval: variable, Dict item or List item that can be assigned a value
2490 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2491 * "name.key", "name.key[expr]" etc.
2492 * Indexing only works if "name" is an existing List or Dictionary.
2493 * "name" points to the start of the name.
2494 * If "rettv" is not NULL it points to the value to be assigned.
2495 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2496 * wrong; must end in space or cmd separator.
2497 *
2498 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002499 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002501 */
2502 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002503get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002505 typval_T *rettv;
2506 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 int unlet;
2508 int skip;
2509 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002510 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 char_u *expr_start, *expr_end;
2514 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002515 dictitem_T *v;
2516 typval_T var1;
2517 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002519 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002520 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002521 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002522 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526
2527 if (skip)
2528 {
2529 /* When skipping just find the end of the name. */
2530 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002531 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 }
2533
2534 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002535 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 if (expr_start != NULL)
2537 {
2538 /* Don't expand the name when we already know there is an error. */
2539 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2540 && *p != '[' && *p != '.')
2541 {
2542 EMSG(_(e_trailing));
2543 return NULL;
2544 }
2545
2546 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2547 if (lp->ll_exp_name == NULL)
2548 {
2549 /* Report an invalid expression in braces, unless the
2550 * expression evaluation has been cancelled due to an
2551 * aborting error, an interrupt, or an exception. */
2552 if (!aborting() && !quiet)
2553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002554 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 EMSG2(_(e_invarg2), name);
2556 return NULL;
2557 }
2558 }
2559 lp->ll_name = lp->ll_exp_name;
2560 }
2561 else
2562 lp->ll_name = name;
2563
2564 /* Without [idx] or .key we are done. */
2565 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2566 return p;
2567
2568 cc = *p;
2569 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002570 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 if (v == NULL && !quiet)
2572 EMSG2(_(e_undefvar), lp->ll_name);
2573 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 if (v == NULL)
2575 return NULL;
2576
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 /*
2578 * Loop until no more [idx] or .key is following.
2579 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002580 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2584 && !(lp->ll_tv->v_type == VAR_DICT
2585 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 if (!quiet)
2588 EMSG(_("E689: Can only index a List or Dictionary"));
2589 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 if (!quiet)
2594 EMSG(_("E708: [:] must come last"));
2595 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002597
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 len = -1;
2599 if (*p == '.')
2600 {
2601 key = p + 1;
2602 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2603 ;
2604 if (len == 0)
2605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_(e_emptykey));
2608 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002609 }
2610 p = key + len;
2611 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002612 else
2613 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 if (*p == ':')
2617 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002618 else
2619 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 empty1 = FALSE;
2621 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002623 if (get_tv_string_chk(&var1) == NULL)
2624 {
2625 /* not a number or string */
2626 clear_tv(&var1);
2627 return NULL;
2628 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 }
2630
2631 /* Optionally get the second index [ :expr]. */
2632 if (*p == ':')
2633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002637 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002638 if (!empty1)
2639 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002641 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (rettv != NULL && (rettv->v_type != VAR_LIST
2643 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (!quiet)
2646 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 if (!empty1)
2648 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 }
2651 p = skipwhite(p + 1);
2652 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 else
2655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2658 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 if (!empty1)
2660 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002663 if (get_tv_string_chk(&var2) == NULL)
2664 {
2665 /* not a number or string */
2666 if (!empty1)
2667 clear_tv(&var1);
2668 clear_tv(&var2);
2669 return NULL;
2670 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (!quiet)
2680 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 if (!empty1)
2682 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 }
2687
2688 /* Skip to past ']'. */
2689 ++p;
2690 }
2691
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 {
2694 if (len == -1)
2695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002697 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 if (*key == NUL)
2699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (!quiet)
2701 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 }
2705 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002706 lp->ll_list = NULL;
2707 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002708 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002711 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 if (len == -1)
2717 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 }
2720 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (len == -1)
2725 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 p = NULL;
2728 break;
2729 }
2730 if (len == -1)
2731 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
2734 else
2735 {
2736 /*
2737 * Get the number and item for the only or first index of the List.
2738 */
2739 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 else
2742 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 clear_tv(&var1);
2745 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 lp->ll_list = lp->ll_tv->vval.v_list;
2748 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2749 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002751 if (lp->ll_n1 < 0)
2752 {
2753 lp->ll_n1 = 0;
2754 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2755 }
2756 }
2757 if (lp->ll_li == NULL)
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 }
2763
2764 /*
2765 * May need to find the item or absolute index for the second
2766 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 * When no index given: "lp->ll_empty2" is TRUE.
2768 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002772 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 }
2781
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2783 if (lp->ll_n1 < 0)
2784 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2785 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002787 }
2788
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002791 }
2792
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 return p;
2794}
2795
2796/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002797 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 */
2799 static void
2800clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002801 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802{
2803 vim_free(lp->ll_exp_name);
2804 vim_free(lp->ll_newkey);
2805}
2806
2807/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002808 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002810 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 */
2812 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002814 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002816 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002818 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819{
2820 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002821 listitem_T *ri;
2822 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823
2824 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002827 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 cc = *endp;
2829 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002830 if (op != NULL && *op != '=')
2831 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002832 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002833
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002834 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002835 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002836 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002837 {
2838 if (tv_op(&tv, rettv, op) == OK)
2839 set_var(lp->ll_name, &tv, FALSE);
2840 clear_tv(&tv);
2841 }
2842 }
2843 else
2844 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002846 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002848 else if (tv_check_lock(lp->ll_newkey == NULL
2849 ? lp->ll_tv->v_lock
2850 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2851 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 else if (lp->ll_range)
2853 {
2854 /*
2855 * Assign the List values to the list items.
2856 */
2857 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002858 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002859 if (op != NULL && *op != '=')
2860 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2861 else
2862 {
2863 clear_tv(&lp->ll_li->li_tv);
2864 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2865 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 ri = ri->li_next;
2867 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2868 break;
2869 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002870 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002872 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 ri = NULL;
2875 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002876 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002877 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 lp->ll_li = lp->ll_li->li_next;
2879 ++lp->ll_n1;
2880 }
2881 if (ri != NULL)
2882 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002883 else if (lp->ll_empty2
2884 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 : lp->ll_n1 != lp->ll_n2)
2886 EMSG(_("E711: List value has not enough items"));
2887 }
2888 else
2889 {
2890 /*
2891 * Assign to a List or Dictionary item.
2892 */
2893 if (lp->ll_newkey != NULL)
2894 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002895 if (op != NULL && *op != '=')
2896 {
2897 EMSG2(_(e_letwrong), op);
2898 return;
2899 }
2900
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002902 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 if (di == NULL)
2904 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002905 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2906 {
2907 vim_free(di);
2908 return;
2909 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002911 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 else if (op != NULL && *op != '=')
2913 {
2914 tv_op(lp->ll_tv, rettv, op);
2915 return;
2916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002917 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002919
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 /*
2921 * Assign the value to the variable or list item.
2922 */
2923 if (copy)
2924 copy_tv(rettv, lp->ll_tv);
2925 else
2926 {
2927 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002928 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 }
2931 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002932}
2933
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002934/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2936 * Returns OK or FAIL.
2937 */
2938 static int
2939tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002940 typval_T *tv1;
2941 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 char_u *op;
2943{
2944 long n;
2945 char_u numbuf[NUMBUFLEN];
2946 char_u *s;
2947
2948 /* Can't do anything with a Funcref or a Dict on the right. */
2949 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2950 {
2951 switch (tv1->v_type)
2952 {
2953 case VAR_DICT:
2954 case VAR_FUNC:
2955 break;
2956
2957 case VAR_LIST:
2958 if (*op != '+' || tv2->v_type != VAR_LIST)
2959 break;
2960 /* List += List */
2961 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2962 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2963 return OK;
2964
2965 case VAR_NUMBER:
2966 case VAR_STRING:
2967 if (tv2->v_type == VAR_LIST)
2968 break;
2969 if (*op == '+' || *op == '-')
2970 {
2971 /* nr += nr or nr -= nr*/
2972 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002973#ifdef FEAT_FLOAT
2974 if (tv2->v_type == VAR_FLOAT)
2975 {
2976 float_T f = n;
2977
2978 if (*op == '+')
2979 f += tv2->vval.v_float;
2980 else
2981 f -= tv2->vval.v_float;
2982 clear_tv(tv1);
2983 tv1->v_type = VAR_FLOAT;
2984 tv1->vval.v_float = f;
2985 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002987#endif
2988 {
2989 if (*op == '+')
2990 n += get_tv_number(tv2);
2991 else
2992 n -= get_tv_number(tv2);
2993 clear_tv(tv1);
2994 tv1->v_type = VAR_NUMBER;
2995 tv1->vval.v_number = n;
2996 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 }
2998 else
2999 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003000 if (tv2->v_type == VAR_FLOAT)
3001 break;
3002
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003003 /* str .= str */
3004 s = get_tv_string(tv1);
3005 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3006 clear_tv(tv1);
3007 tv1->v_type = VAR_STRING;
3008 tv1->vval.v_string = s;
3009 }
3010 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003011
3012#ifdef FEAT_FLOAT
3013 case VAR_FLOAT:
3014 {
3015 float_T f;
3016
3017 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3018 && tv2->v_type != VAR_NUMBER
3019 && tv2->v_type != VAR_STRING))
3020 break;
3021 if (tv2->v_type == VAR_FLOAT)
3022 f = tv2->vval.v_float;
3023 else
3024 f = get_tv_number(tv2);
3025 if (*op == '+')
3026 tv1->vval.v_float += f;
3027 else
3028 tv1->vval.v_float -= f;
3029 }
3030 return OK;
3031#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003032 }
3033 }
3034
3035 EMSG2(_(e_letwrong), op);
3036 return FAIL;
3037}
3038
3039/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003040 * Add a watcher to a list.
3041 */
3042 static void
3043list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003044 list_T *l;
3045 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003046{
3047 lw->lw_next = l->lv_watch;
3048 l->lv_watch = lw;
3049}
3050
3051/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003052 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003053 * No warning when it isn't found...
3054 */
3055 static void
3056list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003057 list_T *l;
3058 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003059{
Bram Moolenaar33570922005-01-25 22:26:29 +00003060 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003061
3062 lwp = &l->lv_watch;
3063 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3064 {
3065 if (lw == lwrem)
3066 {
3067 *lwp = lw->lw_next;
3068 break;
3069 }
3070 lwp = &lw->lw_next;
3071 }
3072}
3073
3074/*
3075 * Just before removing an item from a list: advance watchers to the next
3076 * item.
3077 */
3078 static void
3079list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003080 list_T *l;
3081 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003082{
Bram Moolenaar33570922005-01-25 22:26:29 +00003083 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003084
3085 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3086 if (lw->lw_item == item)
3087 lw->lw_item = item->li_next;
3088}
3089
3090/*
3091 * Evaluate the expression used in a ":for var in expr" command.
3092 * "arg" points to "var".
3093 * Set "*errp" to TRUE for an error, FALSE otherwise;
3094 * Return a pointer that holds the info. Null when there is an error.
3095 */
3096 void *
3097eval_for_line(arg, errp, nextcmdp, skip)
3098 char_u *arg;
3099 int *errp;
3100 char_u **nextcmdp;
3101 int skip;
3102{
Bram Moolenaar33570922005-01-25 22:26:29 +00003103 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003104 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003105 typval_T tv;
3106 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107
3108 *errp = TRUE; /* default: there is an error */
3109
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003111 if (fi == NULL)
3112 return NULL;
3113
3114 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3115 if (expr == NULL)
3116 return fi;
3117
3118 expr = skipwhite(expr);
3119 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3120 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003121 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122 return fi;
3123 }
3124
3125 if (skip)
3126 ++emsg_skip;
3127 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3128 {
3129 *errp = FALSE;
3130 if (!skip)
3131 {
3132 l = tv.vval.v_list;
3133 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003134 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003135 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003136 clear_tv(&tv);
3137 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003138 else
3139 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003140 /* No need to increment the refcount, it's already set for the
3141 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003142 fi->fi_list = l;
3143 list_add_watch(l, &fi->fi_lw);
3144 fi->fi_lw.lw_item = l->lv_first;
3145 }
3146 }
3147 }
3148 if (skip)
3149 --emsg_skip;
3150
3151 return fi;
3152}
3153
3154/*
3155 * Use the first item in a ":for" list. Advance to the next.
3156 * Assign the values to the variable (list). "arg" points to the first one.
3157 * Return TRUE when a valid item was found, FALSE when at end of list or
3158 * something wrong.
3159 */
3160 int
3161next_for_item(fi_void, arg)
3162 void *fi_void;
3163 char_u *arg;
3164{
Bram Moolenaar33570922005-01-25 22:26:29 +00003165 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003166 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003167 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168
3169 item = fi->fi_lw.lw_item;
3170 if (item == NULL)
3171 result = FALSE;
3172 else
3173 {
3174 fi->fi_lw.lw_item = item->li_next;
3175 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3176 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3177 }
3178 return result;
3179}
3180
3181/*
3182 * Free the structure used to store info used by ":for".
3183 */
3184 void
3185free_for_info(fi_void)
3186 void *fi_void;
3187{
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003190 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003191 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003193 list_unref(fi->fi_list);
3194 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 vim_free(fi);
3196}
3197
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3199
3200 void
3201set_context_for_expression(xp, arg, cmdidx)
3202 expand_T *xp;
3203 char_u *arg;
3204 cmdidx_T cmdidx;
3205{
3206 int got_eq = FALSE;
3207 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003210 if (cmdidx == CMD_let)
3211 {
3212 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003213 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214 {
3215 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003216 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 {
3218 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 if (vim_iswhite(*p))
3221 break;
3222 }
3223 return;
3224 }
3225 }
3226 else
3227 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3228 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 while ((xp->xp_pattern = vim_strpbrk(arg,
3230 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3231 {
3232 c = *xp->xp_pattern;
3233 if (c == '&')
3234 {
3235 c = xp->xp_pattern[1];
3236 if (c == '&')
3237 {
3238 ++xp->xp_pattern;
3239 xp->xp_context = cmdidx != CMD_let || got_eq
3240 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3241 }
3242 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003243 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003245 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3246 xp->xp_pattern += 2;
3247
3248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 }
3250 else if (c == '$')
3251 {
3252 /* environment variable */
3253 xp->xp_context = EXPAND_ENV_VARS;
3254 }
3255 else if (c == '=')
3256 {
3257 got_eq = TRUE;
3258 xp->xp_context = EXPAND_EXPRESSION;
3259 }
3260 else if (c == '<'
3261 && xp->xp_context == EXPAND_FUNCTIONS
3262 && vim_strchr(xp->xp_pattern, '(') == NULL)
3263 {
3264 /* Function name can start with "<SNR>" */
3265 break;
3266 }
3267 else if (cmdidx != CMD_let || got_eq)
3268 {
3269 if (c == '"') /* string */
3270 {
3271 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3272 if (c == '\\' && xp->xp_pattern[1] != NUL)
3273 ++xp->xp_pattern;
3274 xp->xp_context = EXPAND_NOTHING;
3275 }
3276 else if (c == '\'') /* literal string */
3277 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003278 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3280 /* skip */ ;
3281 xp->xp_context = EXPAND_NOTHING;
3282 }
3283 else if (c == '|')
3284 {
3285 if (xp->xp_pattern[1] == '|')
3286 {
3287 ++xp->xp_pattern;
3288 xp->xp_context = EXPAND_EXPRESSION;
3289 }
3290 else
3291 xp->xp_context = EXPAND_COMMANDS;
3292 }
3293 else
3294 xp->xp_context = EXPAND_EXPRESSION;
3295 }
3296 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 /* Doesn't look like something valid, expand as an expression
3298 * anyway. */
3299 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 arg = xp->xp_pattern;
3301 if (*arg != NUL)
3302 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3303 /* skip */ ;
3304 }
3305 xp->xp_pattern = arg;
3306}
3307
3308#endif /* FEAT_CMDL_COMPL */
3309
3310/*
3311 * ":1,25call func(arg1, arg2)" function call.
3312 */
3313 void
3314ex_call(eap)
3315 exarg_T *eap;
3316{
3317 char_u *arg = eap->arg;
3318 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003320 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003322 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 linenr_T lnum;
3324 int doesrange;
3325 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003326 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003328 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003329 if (fudi.fd_newkey != NULL)
3330 {
3331 /* Still need to give an error message for missing key. */
3332 EMSG2(_(e_dictkey), fudi.fd_newkey);
3333 vim_free(fudi.fd_newkey);
3334 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003335 if (tofree == NULL)
3336 return;
3337
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003338 /* Increase refcount on dictionary, it could get deleted when evaluating
3339 * the arguments. */
3340 if (fudi.fd_dict != NULL)
3341 ++fudi.fd_dict->dv_refcount;
3342
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003343 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003344 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003345 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
Bram Moolenaar532c7802005-01-27 14:44:31 +00003347 /* Skip white space to allow ":call func ()". Not good, but required for
3348 * backward compatibility. */
3349 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003350 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
3352 if (*startarg != '(')
3353 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003354 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 goto end;
3356 }
3357
3358 /*
3359 * When skipping, evaluate the function once, to find the end of the
3360 * arguments.
3361 * When the function takes a range, this is discovered after the first
3362 * call, and the loop is broken.
3363 */
3364 if (eap->skip)
3365 {
3366 ++emsg_skip;
3367 lnum = eap->line2; /* do it once, also with an invalid range */
3368 }
3369 else
3370 lnum = eap->line1;
3371 for ( ; lnum <= eap->line2; ++lnum)
3372 {
3373 if (!eap->skip && eap->addr_count > 0)
3374 {
3375 curwin->w_cursor.lnum = lnum;
3376 curwin->w_cursor.col = 0;
3377 }
3378 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003379 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003380 eap->line1, eap->line2, &doesrange,
3381 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
3383 failed = TRUE;
3384 break;
3385 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003386
3387 /* Handle a function returning a Funcref, Dictionary or List. */
3388 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3389 {
3390 failed = TRUE;
3391 break;
3392 }
3393
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003394 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 if (doesrange || eap->skip)
3396 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003399 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003400 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003401 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (aborting())
3403 break;
3404 }
3405 if (eap->skip)
3406 --emsg_skip;
3407
3408 if (!failed)
3409 {
3410 /* Check for trailing illegal characters and a following command. */
3411 if (!ends_excmd(*arg))
3412 {
3413 emsg_severe = TRUE;
3414 EMSG(_(e_trailing));
3415 }
3416 else
3417 eap->nextcmd = check_nextcmd(arg);
3418 }
3419
3420end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003421 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003422 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423}
3424
3425/*
3426 * ":unlet[!] var1 ... " command.
3427 */
3428 void
3429ex_unlet(eap)
3430 exarg_T *eap;
3431{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003432 ex_unletlock(eap, eap->arg, 0);
3433}
3434
3435/*
3436 * ":lockvar" and ":unlockvar" commands
3437 */
3438 void
3439ex_lockvar(eap)
3440 exarg_T *eap;
3441{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003443 int deep = 2;
3444
3445 if (eap->forceit)
3446 deep = -1;
3447 else if (vim_isdigit(*arg))
3448 {
3449 deep = getdigits(&arg);
3450 arg = skipwhite(arg);
3451 }
3452
3453 ex_unletlock(eap, arg, deep);
3454}
3455
3456/*
3457 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3458 */
3459 static void
3460ex_unletlock(eap, argstart, deep)
3461 exarg_T *eap;
3462 char_u *argstart;
3463 int deep;
3464{
3465 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003468 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
3470 do
3471 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003472 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003473 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3474 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003475 if (lv.ll_name == NULL)
3476 error = TRUE; /* error but continue parsing */
3477 if (name_end == NULL || (!vim_iswhite(*name_end)
3478 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003480 if (name_end != NULL)
3481 {
3482 emsg_severe = TRUE;
3483 EMSG(_(e_trailing));
3484 }
3485 if (!(eap->skip || error))
3486 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 break;
3488 }
3489
3490 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003491 {
3492 if (eap->cmdidx == CMD_unlet)
3493 {
3494 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3495 error = TRUE;
3496 }
3497 else
3498 {
3499 if (do_lock_var(&lv, name_end, deep,
3500 eap->cmdidx == CMD_lockvar) == FAIL)
3501 error = TRUE;
3502 }
3503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003505 if (!eap->skip)
3506 clear_lval(&lv);
3507
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 arg = skipwhite(name_end);
3509 } while (!ends_excmd(*arg));
3510
3511 eap->nextcmd = check_nextcmd(arg);
3512}
3513
Bram Moolenaar8c711452005-01-14 21:53:12 +00003514 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003515do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003516 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003517 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003518 int forceit;
3519{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003520 int ret = OK;
3521 int cc;
3522
3523 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003524 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003525 cc = *name_end;
3526 *name_end = NUL;
3527
3528 /* Normal name or expanded name. */
3529 if (check_changedtick(lp->ll_name))
3530 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003531 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003532 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003533 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003534 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003535 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3536 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003537 else if (lp->ll_range)
3538 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003539 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003540
3541 /* Delete a range of List items. */
3542 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3543 {
3544 li = lp->ll_li->li_next;
3545 listitem_remove(lp->ll_list, lp->ll_li);
3546 lp->ll_li = li;
3547 ++lp->ll_n1;
3548 }
3549 }
3550 else
3551 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003552 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003553 /* unlet a List item. */
3554 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003555 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003557 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003558 }
3559
3560 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003561}
3562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563/*
3564 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003565 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 */
3567 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003568do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003570 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
Bram Moolenaar33570922005-01-25 22:26:29 +00003572 hashtab_T *ht;
3573 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003574 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003575 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
Bram Moolenaar33570922005-01-25 22:26:29 +00003577 ht = find_var_ht(name, &varname);
3578 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003580 hi = hash_find(ht, varname);
3581 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003582 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003583 di = HI2DI(hi);
3584 if (var_check_fixed(di->di_flags, name)
3585 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003586 return FAIL;
3587 delete_var(ht, hi);
3588 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003591 if (forceit)
3592 return OK;
3593 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 return FAIL;
3595}
3596
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003597/*
3598 * Lock or unlock variable indicated by "lp".
3599 * "deep" is the levels to go (-1 for unlimited);
3600 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3601 */
3602 static int
3603do_lock_var(lp, name_end, deep, lock)
3604 lval_T *lp;
3605 char_u *name_end;
3606 int deep;
3607 int lock;
3608{
3609 int ret = OK;
3610 int cc;
3611 dictitem_T *di;
3612
3613 if (deep == 0) /* nothing to do */
3614 return OK;
3615
3616 if (lp->ll_tv == NULL)
3617 {
3618 cc = *name_end;
3619 *name_end = NUL;
3620
3621 /* Normal name or expanded name. */
3622 if (check_changedtick(lp->ll_name))
3623 ret = FAIL;
3624 else
3625 {
3626 di = find_var(lp->ll_name, NULL);
3627 if (di == NULL)
3628 ret = FAIL;
3629 else
3630 {
3631 if (lock)
3632 di->di_flags |= DI_FLAGS_LOCK;
3633 else
3634 di->di_flags &= ~DI_FLAGS_LOCK;
3635 item_lock(&di->di_tv, deep, lock);
3636 }
3637 }
3638 *name_end = cc;
3639 }
3640 else if (lp->ll_range)
3641 {
3642 listitem_T *li = lp->ll_li;
3643
3644 /* (un)lock a range of List items. */
3645 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3646 {
3647 item_lock(&li->li_tv, deep, lock);
3648 li = li->li_next;
3649 ++lp->ll_n1;
3650 }
3651 }
3652 else if (lp->ll_list != NULL)
3653 /* (un)lock a List item. */
3654 item_lock(&lp->ll_li->li_tv, deep, lock);
3655 else
3656 /* un(lock) a Dictionary item. */
3657 item_lock(&lp->ll_di->di_tv, deep, lock);
3658
3659 return ret;
3660}
3661
3662/*
3663 * Lock or unlock an item. "deep" is nr of levels to go.
3664 */
3665 static void
3666item_lock(tv, deep, lock)
3667 typval_T *tv;
3668 int deep;
3669 int lock;
3670{
3671 static int recurse = 0;
3672 list_T *l;
3673 listitem_T *li;
3674 dict_T *d;
3675 hashitem_T *hi;
3676 int todo;
3677
3678 if (recurse >= DICT_MAXNEST)
3679 {
3680 EMSG(_("E743: variable nested too deep for (un)lock"));
3681 return;
3682 }
3683 if (deep == 0)
3684 return;
3685 ++recurse;
3686
3687 /* lock/unlock the item itself */
3688 if (lock)
3689 tv->v_lock |= VAR_LOCKED;
3690 else
3691 tv->v_lock &= ~VAR_LOCKED;
3692
3693 switch (tv->v_type)
3694 {
3695 case VAR_LIST:
3696 if ((l = tv->vval.v_list) != NULL)
3697 {
3698 if (lock)
3699 l->lv_lock |= VAR_LOCKED;
3700 else
3701 l->lv_lock &= ~VAR_LOCKED;
3702 if (deep < 0 || deep > 1)
3703 /* recursive: lock/unlock the items the List contains */
3704 for (li = l->lv_first; li != NULL; li = li->li_next)
3705 item_lock(&li->li_tv, deep - 1, lock);
3706 }
3707 break;
3708 case VAR_DICT:
3709 if ((d = tv->vval.v_dict) != NULL)
3710 {
3711 if (lock)
3712 d->dv_lock |= VAR_LOCKED;
3713 else
3714 d->dv_lock &= ~VAR_LOCKED;
3715 if (deep < 0 || deep > 1)
3716 {
3717 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003718 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003719 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3720 {
3721 if (!HASHITEM_EMPTY(hi))
3722 {
3723 --todo;
3724 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3725 }
3726 }
3727 }
3728 }
3729 }
3730 --recurse;
3731}
3732
Bram Moolenaara40058a2005-07-11 22:42:07 +00003733/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003734 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3735 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003736 */
3737 static int
3738tv_islocked(tv)
3739 typval_T *tv;
3740{
3741 return (tv->v_lock & VAR_LOCKED)
3742 || (tv->v_type == VAR_LIST
3743 && tv->vval.v_list != NULL
3744 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3745 || (tv->v_type == VAR_DICT
3746 && tv->vval.v_dict != NULL
3747 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3748}
3749
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3751/*
3752 * Delete all "menutrans_" variables.
3753 */
3754 void
3755del_menutrans_vars()
3756{
Bram Moolenaar33570922005-01-25 22:26:29 +00003757 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003758 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
Bram Moolenaar33570922005-01-25 22:26:29 +00003760 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003761 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003762 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003763 {
3764 if (!HASHITEM_EMPTY(hi))
3765 {
3766 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003767 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3768 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003769 }
3770 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003771 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772}
3773#endif
3774
3775#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3776
3777/*
3778 * Local string buffer for the next two functions to store a variable name
3779 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3780 * get_user_var_name().
3781 */
3782
3783static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3784
3785static char_u *varnamebuf = NULL;
3786static int varnamebuflen = 0;
3787
3788/*
3789 * Function to concatenate a prefix and a variable name.
3790 */
3791 static char_u *
3792cat_prefix_varname(prefix, name)
3793 int prefix;
3794 char_u *name;
3795{
3796 int len;
3797
3798 len = (int)STRLEN(name) + 3;
3799 if (len > varnamebuflen)
3800 {
3801 vim_free(varnamebuf);
3802 len += 10; /* some additional space */
3803 varnamebuf = alloc(len);
3804 if (varnamebuf == NULL)
3805 {
3806 varnamebuflen = 0;
3807 return NULL;
3808 }
3809 varnamebuflen = len;
3810 }
3811 *varnamebuf = prefix;
3812 varnamebuf[1] = ':';
3813 STRCPY(varnamebuf + 2, name);
3814 return varnamebuf;
3815}
3816
3817/*
3818 * Function given to ExpandGeneric() to obtain the list of user defined
3819 * (global/buffer/window/built-in) variable names.
3820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 char_u *
3822get_user_var_name(xp, idx)
3823 expand_T *xp;
3824 int idx;
3825{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003826 static long_u gdone;
3827 static long_u bdone;
3828 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003829#ifdef FEAT_WINDOWS
3830 static long_u tdone;
3831#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003832 static int vidx;
3833 static hashitem_T *hi;
3834 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
3836 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003837 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003838 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003839#ifdef FEAT_WINDOWS
3840 tdone = 0;
3841#endif
3842 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003843
3844 /* Global variables */
3845 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003847 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003849 else
3850 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 while (HASHITEM_EMPTY(hi))
3852 ++hi;
3853 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3854 return cat_prefix_varname('g', hi->hi_key);
3855 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003857
3858 /* b: variables */
3859 ht = &curbuf->b_vars.dv_hashtab;
3860 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003862 if (bdone++ == 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('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003870 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003872 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 return (char_u *)"b:changedtick";
3874 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003875
3876 /* w: variables */
3877 ht = &curwin->w_vars.dv_hashtab;
3878 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003880 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003881 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003882 else
3883 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003884 while (HASHITEM_EMPTY(hi))
3885 ++hi;
3886 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003888
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003889#ifdef FEAT_WINDOWS
3890 /* t: variables */
3891 ht = &curtab->tp_vars.dv_hashtab;
3892 if (tdone < ht->ht_used)
3893 {
3894 if (tdone++ == 0)
3895 hi = ht->ht_array;
3896 else
3897 ++hi;
3898 while (HASHITEM_EMPTY(hi))
3899 ++hi;
3900 return cat_prefix_varname('t', hi->hi_key);
3901 }
3902#endif
3903
Bram Moolenaar33570922005-01-25 22:26:29 +00003904 /* v: variables */
3905 if (vidx < VV_LEN)
3906 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
3908 vim_free(varnamebuf);
3909 varnamebuf = NULL;
3910 varnamebuflen = 0;
3911 return NULL;
3912}
3913
3914#endif /* FEAT_CMDL_COMPL */
3915
3916/*
3917 * types for expressions.
3918 */
3919typedef enum
3920{
3921 TYPE_UNKNOWN = 0
3922 , TYPE_EQUAL /* == */
3923 , TYPE_NEQUAL /* != */
3924 , TYPE_GREATER /* > */
3925 , TYPE_GEQUAL /* >= */
3926 , TYPE_SMALLER /* < */
3927 , TYPE_SEQUAL /* <= */
3928 , TYPE_MATCH /* =~ */
3929 , TYPE_NOMATCH /* !~ */
3930} exptype_T;
3931
3932/*
3933 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003934 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3936 */
3937
3938/*
3939 * Handle zero level expression.
3940 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003941 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003942 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 * Return OK or FAIL.
3944 */
3945 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003946eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 char_u **nextcmd;
3950 int evaluate;
3951{
3952 int ret;
3953 char_u *p;
3954
3955 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003956 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 if (ret == FAIL || !ends_excmd(*p))
3958 {
3959 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003960 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 /*
3962 * Report the invalid expression unless the expression evaluation has
3963 * been cancelled due to an aborting error, an interrupt, or an
3964 * exception.
3965 */
3966 if (!aborting())
3967 EMSG2(_(e_invexpr2), arg);
3968 ret = FAIL;
3969 }
3970 if (nextcmd != NULL)
3971 *nextcmd = check_nextcmd(p);
3972
3973 return ret;
3974}
3975
3976/*
3977 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003978 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 *
3980 * "arg" must point to the first non-white of the expression.
3981 * "arg" is advanced to the next non-white after the recognized expression.
3982 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003983 * Note: "rettv.v_lock" is not set.
3984 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 * Return OK or FAIL.
3986 */
3987 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003988eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 int evaluate;
3992{
3993 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003994 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
3996 /*
3997 * Get the first variable.
3998 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003999 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 return FAIL;
4001
4002 if ((*arg)[0] == '?')
4003 {
4004 result = FALSE;
4005 if (evaluate)
4006 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004007 int error = FALSE;
4008
4009 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004011 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004012 if (error)
4013 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015
4016 /*
4017 * Get the second variable.
4018 */
4019 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004020 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 return FAIL;
4022
4023 /*
4024 * Check for the ":".
4025 */
4026 if ((*arg)[0] != ':')
4027 {
4028 EMSG(_("E109: Missing ':' after '?'"));
4029 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004030 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 return FAIL;
4032 }
4033
4034 /*
4035 * Get the third variable.
4036 */
4037 *arg = skipwhite(*arg + 1);
4038 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4039 {
4040 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004041 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 return FAIL;
4043 }
4044 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
4047
4048 return OK;
4049}
4050
4051/*
4052 * Handle first level expression:
4053 * expr2 || expr2 || expr2 logical OR
4054 *
4055 * "arg" must point to the first non-white of the expression.
4056 * "arg" is advanced to the next non-white after the recognized expression.
4057 *
4058 * Return OK or FAIL.
4059 */
4060 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004061eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 int evaluate;
4065{
Bram Moolenaar33570922005-01-25 22:26:29 +00004066 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 long result;
4068 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004069 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
4071 /*
4072 * Get the first variable.
4073 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 return FAIL;
4076
4077 /*
4078 * Repeat until there is no following "||".
4079 */
4080 first = TRUE;
4081 result = FALSE;
4082 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4083 {
4084 if (evaluate && first)
4085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004086 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004088 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004089 if (error)
4090 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 first = FALSE;
4092 }
4093
4094 /*
4095 * Get the second variable.
4096 */
4097 *arg = skipwhite(*arg + 2);
4098 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4099 return FAIL;
4100
4101 /*
4102 * Compute the result.
4103 */
4104 if (evaluate && !result)
4105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004106 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004108 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 if (error)
4110 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
4112 if (evaluate)
4113 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 rettv->v_type = VAR_NUMBER;
4115 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117 }
4118
4119 return OK;
4120}
4121
4122/*
4123 * Handle second level expression:
4124 * expr3 && expr3 && expr3 logical AND
4125 *
4126 * "arg" must point to the first non-white of the expression.
4127 * "arg" is advanced to the next non-white after the recognized expression.
4128 *
4129 * Return OK or FAIL.
4130 */
4131 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 int evaluate;
4136{
Bram Moolenaar33570922005-01-25 22:26:29 +00004137 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 long result;
4139 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004140 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142 /*
4143 * Get the first variable.
4144 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 return FAIL;
4147
4148 /*
4149 * Repeat until there is no following "&&".
4150 */
4151 first = TRUE;
4152 result = TRUE;
4153 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4154 {
4155 if (evaluate && first)
4156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004157 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004159 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004160 if (error)
4161 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 first = FALSE;
4163 }
4164
4165 /*
4166 * Get the second variable.
4167 */
4168 *arg = skipwhite(*arg + 2);
4169 if (eval4(arg, &var2, evaluate && result) == FAIL)
4170 return FAIL;
4171
4172 /*
4173 * Compute the result.
4174 */
4175 if (evaluate && result)
4176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004180 if (error)
4181 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183 if (evaluate)
4184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 rettv->v_type = VAR_NUMBER;
4186 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
4188 }
4189
4190 return OK;
4191}
4192
4193/*
4194 * Handle third level expression:
4195 * var1 == var2
4196 * var1 =~ var2
4197 * var1 != var2
4198 * var1 !~ var2
4199 * var1 > var2
4200 * var1 >= var2
4201 * var1 < var2
4202 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004203 * var1 is var2
4204 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 *
4206 * "arg" must point to the first non-white of the expression.
4207 * "arg" is advanced to the next non-white after the recognized expression.
4208 *
4209 * Return OK or FAIL.
4210 */
4211 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 int evaluate;
4216{
Bram Moolenaar33570922005-01-25 22:26:29 +00004217 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 char_u *p;
4219 int i;
4220 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004221 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 int len = 2;
4223 long n1, n2;
4224 char_u *s1, *s2;
4225 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4226 regmatch_T regmatch;
4227 int ic;
4228 char_u *save_cpo;
4229
4230 /*
4231 * Get the first variable.
4232 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 return FAIL;
4235
4236 p = *arg;
4237 switch (p[0])
4238 {
4239 case '=': if (p[1] == '=')
4240 type = TYPE_EQUAL;
4241 else if (p[1] == '~')
4242 type = TYPE_MATCH;
4243 break;
4244 case '!': if (p[1] == '=')
4245 type = TYPE_NEQUAL;
4246 else if (p[1] == '~')
4247 type = TYPE_NOMATCH;
4248 break;
4249 case '>': if (p[1] != '=')
4250 {
4251 type = TYPE_GREATER;
4252 len = 1;
4253 }
4254 else
4255 type = TYPE_GEQUAL;
4256 break;
4257 case '<': if (p[1] != '=')
4258 {
4259 type = TYPE_SMALLER;
4260 len = 1;
4261 }
4262 else
4263 type = TYPE_SEQUAL;
4264 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004265 case 'i': if (p[1] == 's')
4266 {
4267 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4268 len = 5;
4269 if (!vim_isIDc(p[len]))
4270 {
4271 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4272 type_is = TRUE;
4273 }
4274 }
4275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277
4278 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004279 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 */
4281 if (type != TYPE_UNKNOWN)
4282 {
4283 /* extra question mark appended: ignore case */
4284 if (p[len] == '?')
4285 {
4286 ic = TRUE;
4287 ++len;
4288 }
4289 /* extra '#' appended: match case */
4290 else if (p[len] == '#')
4291 {
4292 ic = FALSE;
4293 ++len;
4294 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004295 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 else
4297 ic = p_ic;
4298
4299 /*
4300 * Get the second variable.
4301 */
4302 *arg = skipwhite(p + len);
4303 if (eval5(arg, &var2, evaluate) == FAIL)
4304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 return FAIL;
4307 }
4308
4309 if (evaluate)
4310 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004311 if (type_is && rettv->v_type != var2.v_type)
4312 {
4313 /* For "is" a different type always means FALSE, for "notis"
4314 * it means TRUE. */
4315 n1 = (type == TYPE_NEQUAL);
4316 }
4317 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4318 {
4319 if (type_is)
4320 {
4321 n1 = (rettv->v_type == var2.v_type
4322 && rettv->vval.v_list == var2.vval.v_list);
4323 if (type == TYPE_NEQUAL)
4324 n1 = !n1;
4325 }
4326 else if (rettv->v_type != var2.v_type
4327 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4328 {
4329 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004330 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004331 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004332 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004333 clear_tv(rettv);
4334 clear_tv(&var2);
4335 return FAIL;
4336 }
4337 else
4338 {
4339 /* Compare two Lists for being equal or unequal. */
4340 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4341 if (type == TYPE_NEQUAL)
4342 n1 = !n1;
4343 }
4344 }
4345
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004346 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4347 {
4348 if (type_is)
4349 {
4350 n1 = (rettv->v_type == var2.v_type
4351 && rettv->vval.v_dict == var2.vval.v_dict);
4352 if (type == TYPE_NEQUAL)
4353 n1 = !n1;
4354 }
4355 else if (rettv->v_type != var2.v_type
4356 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4357 {
4358 if (rettv->v_type != var2.v_type)
4359 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4360 else
4361 EMSG(_("E736: Invalid operation for Dictionary"));
4362 clear_tv(rettv);
4363 clear_tv(&var2);
4364 return FAIL;
4365 }
4366 else
4367 {
4368 /* Compare two Dictionaries for being equal or unequal. */
4369 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4370 if (type == TYPE_NEQUAL)
4371 n1 = !n1;
4372 }
4373 }
4374
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004375 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4376 {
4377 if (rettv->v_type != var2.v_type
4378 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4379 {
4380 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004381 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004382 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004383 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004384 clear_tv(rettv);
4385 clear_tv(&var2);
4386 return FAIL;
4387 }
4388 else
4389 {
4390 /* Compare two Funcrefs for being equal or unequal. */
4391 if (rettv->vval.v_string == NULL
4392 || var2.vval.v_string == NULL)
4393 n1 = FALSE;
4394 else
4395 n1 = STRCMP(rettv->vval.v_string,
4396 var2.vval.v_string) == 0;
4397 if (type == TYPE_NEQUAL)
4398 n1 = !n1;
4399 }
4400 }
4401
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004402#ifdef FEAT_FLOAT
4403 /*
4404 * If one of the two variables is a float, compare as a float.
4405 * When using "=~" or "!~", always compare as string.
4406 */
4407 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4408 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4409 {
4410 float_T f1, f2;
4411
4412 if (rettv->v_type == VAR_FLOAT)
4413 f1 = rettv->vval.v_float;
4414 else
4415 f1 = get_tv_number(rettv);
4416 if (var2.v_type == VAR_FLOAT)
4417 f2 = var2.vval.v_float;
4418 else
4419 f2 = get_tv_number(&var2);
4420 n1 = FALSE;
4421 switch (type)
4422 {
4423 case TYPE_EQUAL: n1 = (f1 == f2); break;
4424 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4425 case TYPE_GREATER: n1 = (f1 > f2); break;
4426 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4427 case TYPE_SMALLER: n1 = (f1 < f2); break;
4428 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4429 case TYPE_UNKNOWN:
4430 case TYPE_MATCH:
4431 case TYPE_NOMATCH: break; /* avoid gcc warning */
4432 }
4433 }
4434#endif
4435
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 /*
4437 * If one of the two variables is a number, compare as a number.
4438 * When using "=~" or "!~", always compare as string.
4439 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4442 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 n1 = get_tv_number(rettv);
4444 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 switch (type)
4446 {
4447 case TYPE_EQUAL: n1 = (n1 == n2); break;
4448 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4449 case TYPE_GREATER: n1 = (n1 > n2); break;
4450 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4451 case TYPE_SMALLER: n1 = (n1 < n2); break;
4452 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4453 case TYPE_UNKNOWN:
4454 case TYPE_MATCH:
4455 case TYPE_NOMATCH: break; /* avoid gcc warning */
4456 }
4457 }
4458 else
4459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004460 s1 = get_tv_string_buf(rettv, buf1);
4461 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4463 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4464 else
4465 i = 0;
4466 n1 = FALSE;
4467 switch (type)
4468 {
4469 case TYPE_EQUAL: n1 = (i == 0); break;
4470 case TYPE_NEQUAL: n1 = (i != 0); break;
4471 case TYPE_GREATER: n1 = (i > 0); break;
4472 case TYPE_GEQUAL: n1 = (i >= 0); break;
4473 case TYPE_SMALLER: n1 = (i < 0); break;
4474 case TYPE_SEQUAL: n1 = (i <= 0); break;
4475
4476 case TYPE_MATCH:
4477 case TYPE_NOMATCH:
4478 /* avoid 'l' flag in 'cpoptions' */
4479 save_cpo = p_cpo;
4480 p_cpo = (char_u *)"";
4481 regmatch.regprog = vim_regcomp(s2,
4482 RE_MAGIC + RE_STRING);
4483 regmatch.rm_ic = ic;
4484 if (regmatch.regprog != NULL)
4485 {
4486 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4487 vim_free(regmatch.regprog);
4488 if (type == TYPE_NOMATCH)
4489 n1 = !n1;
4490 }
4491 p_cpo = save_cpo;
4492 break;
4493
4494 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4495 }
4496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004497 clear_tv(rettv);
4498 clear_tv(&var2);
4499 rettv->v_type = VAR_NUMBER;
4500 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502 }
4503
4504 return OK;
4505}
4506
4507/*
4508 * Handle fourth level expression:
4509 * + number addition
4510 * - number subtraction
4511 * . string concatenation
4512 *
4513 * "arg" must point to the first non-white of the expression.
4514 * "arg" is advanced to the next non-white after the recognized expression.
4515 *
4516 * Return OK or FAIL.
4517 */
4518 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004519eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 int evaluate;
4523{
Bram Moolenaar33570922005-01-25 22:26:29 +00004524 typval_T var2;
4525 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 int op;
4527 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004528#ifdef FEAT_FLOAT
4529 float_T f1 = 0, f2 = 0;
4530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 char_u *s1, *s2;
4532 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4533 char_u *p;
4534
4535 /*
4536 * Get the first variable.
4537 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004538 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 return FAIL;
4540
4541 /*
4542 * Repeat computing, until no '+', '-' or '.' is following.
4543 */
4544 for (;;)
4545 {
4546 op = **arg;
4547 if (op != '+' && op != '-' && op != '.')
4548 break;
4549
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004550 if ((op != '+' || rettv->v_type != VAR_LIST)
4551#ifdef FEAT_FLOAT
4552 && (op == '.' || rettv->v_type != VAR_FLOAT)
4553#endif
4554 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004555 {
4556 /* For "list + ...", an illegal use of the first operand as
4557 * a number cannot be determined before evaluating the 2nd
4558 * operand: if this is also a list, all is ok.
4559 * For "something . ...", "something - ..." or "non-list + ...",
4560 * we know that the first operand needs to be a string or number
4561 * without evaluating the 2nd operand. So check before to avoid
4562 * side effects after an error. */
4563 if (evaluate && get_tv_string_chk(rettv) == NULL)
4564 {
4565 clear_tv(rettv);
4566 return FAIL;
4567 }
4568 }
4569
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 /*
4571 * Get the second variable.
4572 */
4573 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004574 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004576 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 return FAIL;
4578 }
4579
4580 if (evaluate)
4581 {
4582 /*
4583 * Compute the result.
4584 */
4585 if (op == '.')
4586 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004587 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4588 s2 = get_tv_string_buf_chk(&var2, buf2);
4589 if (s2 == NULL) /* type error ? */
4590 {
4591 clear_tv(rettv);
4592 clear_tv(&var2);
4593 return FAIL;
4594 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004595 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004596 clear_tv(rettv);
4597 rettv->v_type = VAR_STRING;
4598 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004600 else if (op == '+' && rettv->v_type == VAR_LIST
4601 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004602 {
4603 /* concatenate Lists */
4604 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4605 &var3) == FAIL)
4606 {
4607 clear_tv(rettv);
4608 clear_tv(&var2);
4609 return FAIL;
4610 }
4611 clear_tv(rettv);
4612 *rettv = var3;
4613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 else
4615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004616 int error = FALSE;
4617
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004618#ifdef FEAT_FLOAT
4619 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004620 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004621 f1 = rettv->vval.v_float;
4622 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004623 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004624 else
4625#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004626 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004627 n1 = get_tv_number_chk(rettv, &error);
4628 if (error)
4629 {
4630 /* This can only happen for "list + non-list". For
4631 * "non-list + ..." or "something - ...", we returned
4632 * before evaluating the 2nd operand. */
4633 clear_tv(rettv);
4634 return FAIL;
4635 }
4636#ifdef FEAT_FLOAT
4637 if (var2.v_type == VAR_FLOAT)
4638 f1 = n1;
4639#endif
4640 }
4641#ifdef FEAT_FLOAT
4642 if (var2.v_type == VAR_FLOAT)
4643 {
4644 f2 = var2.vval.v_float;
4645 n2 = 0;
4646 }
4647 else
4648#endif
4649 {
4650 n2 = get_tv_number_chk(&var2, &error);
4651 if (error)
4652 {
4653 clear_tv(rettv);
4654 clear_tv(&var2);
4655 return FAIL;
4656 }
4657#ifdef FEAT_FLOAT
4658 if (rettv->v_type == VAR_FLOAT)
4659 f2 = n2;
4660#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004662 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004663
4664#ifdef FEAT_FLOAT
4665 /* If there is a float on either side the result is a float. */
4666 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4667 {
4668 if (op == '+')
4669 f1 = f1 + f2;
4670 else
4671 f1 = f1 - f2;
4672 rettv->v_type = VAR_FLOAT;
4673 rettv->vval.v_float = f1;
4674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004676#endif
4677 {
4678 if (op == '+')
4679 n1 = n1 + n2;
4680 else
4681 n1 = n1 - n2;
4682 rettv->v_type = VAR_NUMBER;
4683 rettv->vval.v_number = n1;
4684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004686 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
4688 }
4689 return OK;
4690}
4691
4692/*
4693 * Handle fifth level expression:
4694 * * number multiplication
4695 * / number division
4696 * % number modulo
4697 *
4698 * "arg" must point to the first non-white of the expression.
4699 * "arg" is advanced to the next non-white after the recognized expression.
4700 *
4701 * Return OK or FAIL.
4702 */
4703 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004704eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004708 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
Bram Moolenaar33570922005-01-25 22:26:29 +00004710 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 int op;
4712 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713#ifdef FEAT_FLOAT
4714 int use_float = FALSE;
4715 float_T f1 = 0, f2;
4716#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718
4719 /*
4720 * Get the first variable.
4721 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004722 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 return FAIL;
4724
4725 /*
4726 * Repeat computing, until no '*', '/' or '%' is following.
4727 */
4728 for (;;)
4729 {
4730 op = **arg;
4731 if (op != '*' && op != '/' && op != '%')
4732 break;
4733
4734 if (evaluate)
4735 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004736#ifdef FEAT_FLOAT
4737 if (rettv->v_type == VAR_FLOAT)
4738 {
4739 f1 = rettv->vval.v_float;
4740 use_float = TRUE;
4741 n1 = 0;
4742 }
4743 else
4744#endif
4745 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004746 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004747 if (error)
4748 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 }
4750 else
4751 n1 = 0;
4752
4753 /*
4754 * Get the second variable.
4755 */
4756 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004757 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 return FAIL;
4759
4760 if (evaluate)
4761 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004762#ifdef FEAT_FLOAT
4763 if (var2.v_type == VAR_FLOAT)
4764 {
4765 if (!use_float)
4766 {
4767 f1 = n1;
4768 use_float = TRUE;
4769 }
4770 f2 = var2.vval.v_float;
4771 n2 = 0;
4772 }
4773 else
4774#endif
4775 {
4776 n2 = get_tv_number_chk(&var2, &error);
4777 clear_tv(&var2);
4778 if (error)
4779 return FAIL;
4780#ifdef FEAT_FLOAT
4781 if (use_float)
4782 f2 = n2;
4783#endif
4784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
4786 /*
4787 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004788 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004790#ifdef FEAT_FLOAT
4791 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793 if (op == '*')
4794 f1 = f1 * f2;
4795 else if (op == '/')
4796 {
4797 /* We rely on the floating point library to handle divide
4798 * by zero to result in "inf" and not a crash. */
4799 f1 = f1 / f2;
4800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004803 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004804 return FAIL;
4805 }
4806 rettv->v_type = VAR_FLOAT;
4807 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004812 if (op == '*')
4813 n1 = n1 * n2;
4814 else if (op == '/')
4815 {
4816 if (n2 == 0) /* give an error message? */
4817 {
4818 if (n1 == 0)
4819 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4820 else if (n1 < 0)
4821 n1 = -0x7fffffffL;
4822 else
4823 n1 = 0x7fffffffL;
4824 }
4825 else
4826 n1 = n1 / n2;
4827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004829 {
4830 if (n2 == 0) /* give an error message? */
4831 n1 = 0;
4832 else
4833 n1 = n1 % n2;
4834 }
4835 rettv->v_type = VAR_NUMBER;
4836 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 }
4839 }
4840
4841 return OK;
4842}
4843
4844/*
4845 * Handle sixth level expression:
4846 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004847 * "string" string constant
4848 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 * &option-name option value
4850 * @r register contents
4851 * identifier variable value
4852 * function() function call
4853 * $VAR environment variable
4854 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004855 * [expr, expr] List
4856 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 *
4858 * Also handle:
4859 * ! in front logical NOT
4860 * - in front unary minus
4861 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004862 * trailing [] subscript in String or List
4863 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 *
4865 * "arg" must point to the first non-white of the expression.
4866 * "arg" is advanced to the next non-white after the recognized expression.
4867 *
4868 * Return OK or FAIL.
4869 */
4870 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004871eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004875 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 long n;
4878 int len;
4879 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 char_u *start_leader, *end_leader;
4881 int ret = OK;
4882 char_u *alias;
4883
4884 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004886 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004888 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 /*
4891 * Skip '!' and '-' characters. They are handled later.
4892 */
4893 start_leader = *arg;
4894 while (**arg == '!' || **arg == '-' || **arg == '+')
4895 *arg = skipwhite(*arg + 1);
4896 end_leader = *arg;
4897
4898 switch (**arg)
4899 {
4900 /*
4901 * Number constant.
4902 */
4903 case '0':
4904 case '1':
4905 case '2':
4906 case '3':
4907 case '4':
4908 case '5':
4909 case '6':
4910 case '7':
4911 case '8':
4912 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913 {
4914#ifdef FEAT_FLOAT
4915 char_u *p = skipdigits(*arg + 1);
4916 int get_float = FALSE;
4917
4918 /* We accept a float when the format matches
4919 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004920 * strict to avoid backwards compatibility problems.
4921 * Don't look for a float after the "." operator, so that
4922 * ":let vers = 1.2.3" doesn't fail. */
4923 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004925 get_float = TRUE;
4926 p = skipdigits(p + 2);
4927 if (*p == 'e' || *p == 'E')
4928 {
4929 ++p;
4930 if (*p == '-' || *p == '+')
4931 ++p;
4932 if (!vim_isdigit(*p))
4933 get_float = FALSE;
4934 else
4935 p = skipdigits(p + 1);
4936 }
4937 if (ASCII_ISALPHA(*p) || *p == '.')
4938 get_float = FALSE;
4939 }
4940 if (get_float)
4941 {
4942 float_T f;
4943
4944 *arg += string2float(*arg, &f);
4945 if (evaluate)
4946 {
4947 rettv->v_type = VAR_FLOAT;
4948 rettv->vval.v_float = f;
4949 }
4950 }
4951 else
4952#endif
4953 {
4954 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4955 *arg += len;
4956 if (evaluate)
4957 {
4958 rettv->v_type = VAR_NUMBER;
4959 rettv->vval.v_number = n;
4960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
4962 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964
4965 /*
4966 * String constant: "string".
4967 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004968 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 break;
4970
4971 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004974 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004975 break;
4976
4977 /*
4978 * List: [expr, expr]
4979 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004980 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 break;
4982
4983 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004984 * Dictionary: {key: val, key: val}
4985 */
4986 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4987 break;
4988
4989 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004990 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004992 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 break;
4994
4995 /*
4996 * Environment variable: $VAR.
4997 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 break;
5000
5001 /*
5002 * Register contents: @r.
5003 */
5004 case '@': ++*arg;
5005 if (evaluate)
5006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005008 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
5010 if (**arg != NUL)
5011 ++*arg;
5012 break;
5013
5014 /*
5015 * nested expression: (expression).
5016 */
5017 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005018 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (**arg == ')')
5020 ++*arg;
5021 else if (ret == OK)
5022 {
5023 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005024 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 ret = FAIL;
5026 }
5027 break;
5028
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 break;
5031 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032
5033 if (ret == NOTDONE)
5034 {
5035 /*
5036 * Must be a variable or function name.
5037 * Can also be a curly-braces kind of name: {expr}.
5038 */
5039 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005040 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 if (alias != NULL)
5042 s = alias;
5043
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005044 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 ret = FAIL;
5046 else
5047 {
5048 if (**arg == '(') /* recursive! */
5049 {
5050 /* If "s" is the name of a variable of type VAR_FUNC
5051 * use its contents. */
5052 s = deref_func_name(s, &len);
5053
5054 /* Invoke the function. */
5055 ret = get_func_tv(s, len, rettv, arg,
5056 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005057 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058 /* Stop the expression evaluation when immediately
5059 * aborting on error, or when an interrupt occurred or
5060 * an exception was thrown but not caught. */
5061 if (aborting())
5062 {
5063 if (ret == OK)
5064 clear_tv(rettv);
5065 ret = FAIL;
5066 }
5067 }
5068 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005069 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005070 else
5071 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 }
5073
5074 if (alias != NULL)
5075 vim_free(alias);
5076 }
5077
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 *arg = skipwhite(*arg);
5079
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005080 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5081 * expr(expr). */
5082 if (ret == OK)
5083 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
5085 /*
5086 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5087 */
5088 if (ret == OK && evaluate && end_leader > start_leader)
5089 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005090 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005091 int val = 0;
5092#ifdef FEAT_FLOAT
5093 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005094
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005095 if (rettv->v_type == VAR_FLOAT)
5096 f = rettv->vval.v_float;
5097 else
5098#endif
5099 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005100 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005102 clear_tv(rettv);
5103 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005105 else
5106 {
5107 while (end_leader > start_leader)
5108 {
5109 --end_leader;
5110 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005111 {
5112#ifdef FEAT_FLOAT
5113 if (rettv->v_type == VAR_FLOAT)
5114 f = !f;
5115 else
5116#endif
5117 val = !val;
5118 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005119 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005120 {
5121#ifdef FEAT_FLOAT
5122 if (rettv->v_type == VAR_FLOAT)
5123 f = -f;
5124 else
5125#endif
5126 val = -val;
5127 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005128 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005129#ifdef FEAT_FLOAT
5130 if (rettv->v_type == VAR_FLOAT)
5131 {
5132 clear_tv(rettv);
5133 rettv->vval.v_float = f;
5134 }
5135 else
5136#endif
5137 {
5138 clear_tv(rettv);
5139 rettv->v_type = VAR_NUMBER;
5140 rettv->vval.v_number = val;
5141 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144
5145 return ret;
5146}
5147
5148/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005149 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5150 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5152 */
5153 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005154eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005156 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005158 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005159{
5160 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005161 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 long len = -1;
5164 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005167
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005168 if (rettv->v_type == VAR_FUNC
5169#ifdef FEAT_FLOAT
5170 || rettv->v_type == VAR_FLOAT
5171#endif
5172 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005173 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005174 if (verbose)
5175 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 return FAIL;
5177 }
5178
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005180 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 /*
5182 * dict.name
5183 */
5184 key = *arg + 1;
5185 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5186 ;
5187 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190 }
5191 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005192 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 /*
5194 * something[idx]
5195 *
5196 * Get the (first) variable from inside the [].
5197 */
5198 *arg = skipwhite(*arg + 1);
5199 if (**arg == ':')
5200 empty1 = TRUE;
5201 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5202 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005203 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5204 {
5205 /* not a number or string */
5206 clear_tv(&var1);
5207 return FAIL;
5208 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209
5210 /*
5211 * Get the second variable from inside the [:].
5212 */
5213 if (**arg == ':')
5214 {
5215 range = TRUE;
5216 *arg = skipwhite(*arg + 1);
5217 if (**arg == ']')
5218 empty2 = TRUE;
5219 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005221 if (!empty1)
5222 clear_tv(&var1);
5223 return FAIL;
5224 }
5225 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5226 {
5227 /* not a number or string */
5228 if (!empty1)
5229 clear_tv(&var1);
5230 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 return FAIL;
5232 }
5233 }
5234
5235 /* Check for the ']'. */
5236 if (**arg != ']')
5237 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005238 if (verbose)
5239 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 clear_tv(&var1);
5241 if (range)
5242 clear_tv(&var2);
5243 return FAIL;
5244 }
5245 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005246 }
5247
5248 if (evaluate)
5249 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 n1 = 0;
5251 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005253 n1 = get_tv_number(&var1);
5254 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 }
5256 if (range)
5257 {
5258 if (empty2)
5259 n2 = -1;
5260 else
5261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005262 n2 = get_tv_number(&var2);
5263 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 }
5265 }
5266
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005267 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 {
5269 case VAR_NUMBER:
5270 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005271 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 len = (long)STRLEN(s);
5273 if (range)
5274 {
5275 /* The resulting variable is a substring. If the indexes
5276 * are out of range the result is empty. */
5277 if (n1 < 0)
5278 {
5279 n1 = len + n1;
5280 if (n1 < 0)
5281 n1 = 0;
5282 }
5283 if (n2 < 0)
5284 n2 = len + n2;
5285 else if (n2 >= len)
5286 n2 = len;
5287 if (n1 >= len || n2 < 0 || n1 > n2)
5288 s = NULL;
5289 else
5290 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5291 }
5292 else
5293 {
5294 /* The resulting variable is a string of a single
5295 * character. If the index is too big or negative the
5296 * result is empty. */
5297 if (n1 >= len || n1 < 0)
5298 s = NULL;
5299 else
5300 s = vim_strnsave(s + n1, 1);
5301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005302 clear_tv(rettv);
5303 rettv->v_type = VAR_STRING;
5304 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 break;
5306
5307 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005308 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 if (n1 < 0)
5310 n1 = len + n1;
5311 if (!empty1 && (n1 < 0 || n1 >= len))
5312 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005313 /* For a range we allow invalid values and return an empty
5314 * list. A list index out of range is an error. */
5315 if (!range)
5316 {
5317 if (verbose)
5318 EMSGN(_(e_listidx), n1);
5319 return FAIL;
5320 }
5321 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 }
5323 if (range)
5324 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005325 list_T *l;
5326 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005327
5328 if (n2 < 0)
5329 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005330 else if (n2 >= len)
5331 n2 = len - 1;
5332 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005333 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 l = list_alloc();
5335 if (l == NULL)
5336 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005337 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 n1 <= n2; ++n1)
5339 {
5340 if (list_append_tv(l, &item->li_tv) == FAIL)
5341 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005342 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 return FAIL;
5344 }
5345 item = item->li_next;
5346 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005347 clear_tv(rettv);
5348 rettv->v_type = VAR_LIST;
5349 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005350 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 }
5352 else
5353 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005354 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005355 clear_tv(rettv);
5356 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 }
5358 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359
5360 case VAR_DICT:
5361 if (range)
5362 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005363 if (verbose)
5364 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005365 if (len == -1)
5366 clear_tv(&var1);
5367 return FAIL;
5368 }
5369 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005370 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005371
5372 if (len == -1)
5373 {
5374 key = get_tv_string(&var1);
5375 if (*key == NUL)
5376 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005377 if (verbose)
5378 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 clear_tv(&var1);
5380 return FAIL;
5381 }
5382 }
5383
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005384 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005386 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005387 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005388 if (len == -1)
5389 clear_tv(&var1);
5390 if (item == NULL)
5391 return FAIL;
5392
5393 copy_tv(&item->di_tv, &var1);
5394 clear_tv(rettv);
5395 *rettv = var1;
5396 }
5397 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 }
5399 }
5400
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 return OK;
5402}
5403
5404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 * Get an option value.
5406 * "arg" points to the '&' or '+' before the option name.
5407 * "arg" is advanced to character after the option name.
5408 * Return OK or FAIL.
5409 */
5410 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005411get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005413 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 int evaluate;
5415{
5416 char_u *option_end;
5417 long numval;
5418 char_u *stringval;
5419 int opt_type;
5420 int c;
5421 int working = (**arg == '+'); /* has("+option") */
5422 int ret = OK;
5423 int opt_flags;
5424
5425 /*
5426 * Isolate the option name and find its value.
5427 */
5428 option_end = find_option_end(arg, &opt_flags);
5429 if (option_end == NULL)
5430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005431 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 EMSG2(_("E112: Option name missing: %s"), *arg);
5433 return FAIL;
5434 }
5435
5436 if (!evaluate)
5437 {
5438 *arg = option_end;
5439 return OK;
5440 }
5441
5442 c = *option_end;
5443 *option_end = NUL;
5444 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005445 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446
5447 if (opt_type == -3) /* invalid name */
5448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005449 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 EMSG2(_("E113: Unknown option: %s"), *arg);
5451 ret = FAIL;
5452 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005453 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {
5455 if (opt_type == -2) /* hidden string option */
5456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 rettv->v_type = VAR_STRING;
5458 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 }
5460 else if (opt_type == -1) /* hidden number option */
5461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005462 rettv->v_type = VAR_NUMBER;
5463 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 }
5465 else if (opt_type == 1) /* number option */
5466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 rettv->v_type = VAR_NUMBER;
5468 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 }
5470 else /* string option */
5471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 rettv->v_type = VAR_STRING;
5473 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 }
5475 }
5476 else if (working && (opt_type == -2 || opt_type == -1))
5477 ret = FAIL;
5478
5479 *option_end = c; /* put back for error messages */
5480 *arg = option_end;
5481
5482 return ret;
5483}
5484
5485/*
5486 * Allocate a variable for a string constant.
5487 * Return OK or FAIL.
5488 */
5489 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005490get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 int evaluate;
5494{
5495 char_u *p;
5496 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 int extra = 0;
5498
5499 /*
5500 * Find the end of the string, skipping backslashed characters.
5501 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005502 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
5504 if (*p == '\\' && p[1] != NUL)
5505 {
5506 ++p;
5507 /* A "\<x>" form occupies at least 4 characters, and produces up
5508 * to 6 characters: reserve space for 2 extra */
5509 if (*p == '<')
5510 extra += 2;
5511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 }
5513
5514 if (*p != '"')
5515 {
5516 EMSG2(_("E114: Missing quote: %s"), *arg);
5517 return FAIL;
5518 }
5519
5520 /* If only parsing, set *arg and return here */
5521 if (!evaluate)
5522 {
5523 *arg = p + 1;
5524 return OK;
5525 }
5526
5527 /*
5528 * Copy the string into allocated memory, handling backslashed
5529 * characters.
5530 */
5531 name = alloc((unsigned)(p - *arg + extra));
5532 if (name == NULL)
5533 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005534 rettv->v_type = VAR_STRING;
5535 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
Bram Moolenaar8c711452005-01-14 21:53:12 +00005537 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 {
5539 if (*p == '\\')
5540 {
5541 switch (*++p)
5542 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005543 case 'b': *name++ = BS; ++p; break;
5544 case 'e': *name++ = ESC; ++p; break;
5545 case 'f': *name++ = FF; ++p; break;
5546 case 'n': *name++ = NL; ++p; break;
5547 case 'r': *name++ = CAR; ++p; break;
5548 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550 case 'X': /* hex: "\x1", "\x12" */
5551 case 'x':
5552 case 'u': /* Unicode: "\u0023" */
5553 case 'U':
5554 if (vim_isxdigit(p[1]))
5555 {
5556 int n, nr;
5557 int c = toupper(*p);
5558
5559 if (c == 'X')
5560 n = 2;
5561 else
5562 n = 4;
5563 nr = 0;
5564 while (--n >= 0 && vim_isxdigit(p[1]))
5565 {
5566 ++p;
5567 nr = (nr << 4) + hex2nr(*p);
5568 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570#ifdef FEAT_MBYTE
5571 /* For "\u" store the number according to
5572 * 'encoding'. */
5573 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 else
5576#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 break;
5580
5581 /* octal: "\1", "\12", "\123" */
5582 case '0':
5583 case '1':
5584 case '2':
5585 case '3':
5586 case '4':
5587 case '5':
5588 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589 case '7': *name = *p++ - '0';
5590 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 *name = (*name << 3) + *p++ - '0';
5593 if (*p >= '0' && *p <= '7')
5594 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 break;
5598
5599 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 if (extra != 0)
5602 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 break;
5605 }
5606 /* FALLTHROUGH */
5607
Bram Moolenaar8c711452005-01-14 21:53:12 +00005608 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 break;
5610 }
5611 }
5612 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005613 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 *arg = p + 1;
5618
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 return OK;
5620}
5621
5622/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 * Return OK or FAIL.
5625 */
5626 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005627get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 int evaluate;
5631{
5632 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005633 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005634 int reduce = 0;
5635
5636 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005638 */
5639 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5640 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005642 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005643 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005644 break;
5645 ++reduce;
5646 ++p;
5647 }
5648 }
5649
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005653 return FAIL;
5654 }
5655
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005657 if (!evaluate)
5658 {
5659 *arg = p + 1;
5660 return OK;
5661 }
5662
5663 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005665 */
5666 str = alloc((unsigned)((p - *arg) - reduce));
5667 if (str == NULL)
5668 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669 rettv->v_type = VAR_STRING;
5670 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005671
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005673 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005675 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005677 break;
5678 ++p;
5679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005683 *arg = p + 1;
5684
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005685 return OK;
5686}
5687
5688/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 * Allocate a variable for a List and fill it from "*arg".
5690 * Return OK or FAIL.
5691 */
5692 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005694 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005695 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 int evaluate;
5697{
Bram Moolenaar33570922005-01-25 22:26:29 +00005698 list_T *l = NULL;
5699 typval_T tv;
5700 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701
5702 if (evaluate)
5703 {
5704 l = list_alloc();
5705 if (l == NULL)
5706 return FAIL;
5707 }
5708
5709 *arg = skipwhite(*arg + 1);
5710 while (**arg != ']' && **arg != NUL)
5711 {
5712 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5713 goto failret;
5714 if (evaluate)
5715 {
5716 item = listitem_alloc();
5717 if (item != NULL)
5718 {
5719 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005720 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721 list_append(l, item);
5722 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005723 else
5724 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725 }
5726
5727 if (**arg == ']')
5728 break;
5729 if (**arg != ',')
5730 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732 goto failret;
5733 }
5734 *arg = skipwhite(*arg + 1);
5735 }
5736
5737 if (**arg != ']')
5738 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005739 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005740failret:
5741 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005742 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743 return FAIL;
5744 }
5745
5746 *arg = skipwhite(*arg + 1);
5747 if (evaluate)
5748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005749 rettv->v_type = VAR_LIST;
5750 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005751 ++l->lv_refcount;
5752 }
5753
5754 return OK;
5755}
5756
5757/*
5758 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005759 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005761 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005762list_alloc()
5763{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005764 list_T *l;
5765
5766 l = (list_T *)alloc_clear(sizeof(list_T));
5767 if (l != NULL)
5768 {
5769 /* Prepend the list to the list of lists for garbage collection. */
5770 if (first_list != NULL)
5771 first_list->lv_used_prev = l;
5772 l->lv_used_prev = NULL;
5773 l->lv_used_next = first_list;
5774 first_list = l;
5775 }
5776 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005777}
5778
5779/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005780 * Allocate an empty list for a return value.
5781 * Returns OK or FAIL.
5782 */
5783 static int
5784rettv_list_alloc(rettv)
5785 typval_T *rettv;
5786{
5787 list_T *l = list_alloc();
5788
5789 if (l == NULL)
5790 return FAIL;
5791
5792 rettv->vval.v_list = l;
5793 rettv->v_type = VAR_LIST;
5794 ++l->lv_refcount;
5795 return OK;
5796}
5797
5798/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 * Unreference a list: decrement the reference count and free it when it
5800 * becomes zero.
5801 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005802 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005804 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005806 if (l != NULL && --l->lv_refcount <= 0)
5807 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808}
5809
5810/*
5811 * Free a list, including all items it points to.
5812 * Ignores the reference count.
5813 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005814 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005815list_free(l, recurse)
5816 list_T *l;
5817 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818{
Bram Moolenaar33570922005-01-25 22:26:29 +00005819 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005820
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005821 /* Remove the list from the list of lists for garbage collection. */
5822 if (l->lv_used_prev == NULL)
5823 first_list = l->lv_used_next;
5824 else
5825 l->lv_used_prev->lv_used_next = l->lv_used_next;
5826 if (l->lv_used_next != NULL)
5827 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5828
Bram Moolenaard9fba312005-06-26 22:34:35 +00005829 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005831 /* Remove the item before deleting it. */
5832 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005833 if (recurse || (item->li_tv.v_type != VAR_LIST
5834 && item->li_tv.v_type != VAR_DICT))
5835 clear_tv(&item->li_tv);
5836 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837 }
5838 vim_free(l);
5839}
5840
5841/*
5842 * Allocate a list item.
5843 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005844 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005845listitem_alloc()
5846{
Bram Moolenaar33570922005-01-25 22:26:29 +00005847 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848}
5849
5850/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005851 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 */
5853 static void
5854listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005857 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 vim_free(item);
5859}
5860
5861/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005862 * Remove a list item from a List and free it. Also clears the value.
5863 */
5864 static void
5865listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005866 list_T *l;
5867 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005868{
5869 list_remove(l, item, item);
5870 listitem_free(item);
5871}
5872
5873/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005874 * Get the number of items in a list.
5875 */
5876 static long
5877list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005878 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 if (l == NULL)
5881 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005882 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883}
5884
5885/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005886 * Return TRUE when two lists have exactly the same values.
5887 */
5888 static int
5889list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005890 list_T *l1;
5891 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005892 int ic; /* ignore case for strings */
5893{
Bram Moolenaar33570922005-01-25 22:26:29 +00005894 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005895
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005896 if (l1 == NULL || l2 == NULL)
5897 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005898 if (l1 == l2)
5899 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005900 if (list_len(l1) != list_len(l2))
5901 return FALSE;
5902
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005903 for (item1 = l1->lv_first, item2 = l2->lv_first;
5904 item1 != NULL && item2 != NULL;
5905 item1 = item1->li_next, item2 = item2->li_next)
5906 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5907 return FALSE;
5908 return item1 == NULL && item2 == NULL;
5909}
5910
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01005911#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
5912 || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005913/*
5914 * Return the dictitem that an entry in a hashtable points to.
5915 */
5916 dictitem_T *
5917dict_lookup(hi)
5918 hashitem_T *hi;
5919{
5920 return HI2DI(hi);
5921}
5922#endif
5923
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005924/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005925 * Return TRUE when two dictionaries have exactly the same key/values.
5926 */
5927 static int
5928dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 dict_T *d1;
5930 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005931 int ic; /* ignore case for strings */
5932{
Bram Moolenaar33570922005-01-25 22:26:29 +00005933 hashitem_T *hi;
5934 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005935 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005936
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005937 if (d1 == NULL || d2 == NULL)
5938 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005939 if (d1 == d2)
5940 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005941 if (dict_len(d1) != dict_len(d2))
5942 return FALSE;
5943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005944 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005945 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005946 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005947 if (!HASHITEM_EMPTY(hi))
5948 {
5949 item2 = dict_find(d2, hi->hi_key, -1);
5950 if (item2 == NULL)
5951 return FALSE;
5952 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5953 return FALSE;
5954 --todo;
5955 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005956 }
5957 return TRUE;
5958}
5959
5960/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005961 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005962 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005963 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005964 */
5965 static int
5966tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005967 typval_T *tv1;
5968 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005969 int ic; /* ignore case */
5970{
5971 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005972 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005973 static int recursive = 0; /* cach recursive loops */
5974 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005975
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005976 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005977 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005978 /* Catch lists and dicts that have an endless loop by limiting
5979 * recursiveness to 1000. We guess they are equal then. */
5980 if (recursive >= 1000)
5981 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005982
5983 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005984 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005985 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005986 ++recursive;
5987 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5988 --recursive;
5989 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005990
5991 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005992 ++recursive;
5993 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5994 --recursive;
5995 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005996
5997 case VAR_FUNC:
5998 return (tv1->vval.v_string != NULL
5999 && tv2->vval.v_string != NULL
6000 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6001
6002 case VAR_NUMBER:
6003 return tv1->vval.v_number == tv2->vval.v_number;
6004
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006005#ifdef FEAT_FLOAT
6006 case VAR_FLOAT:
6007 return tv1->vval.v_float == tv2->vval.v_float;
6008#endif
6009
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006010 case VAR_STRING:
6011 s1 = get_tv_string_buf(tv1, buf1);
6012 s2 = get_tv_string_buf(tv2, buf2);
6013 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006015
6016 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006017 return TRUE;
6018}
6019
6020/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 * Locate item with index "n" in list "l" and return it.
6022 * A negative index is counted from the end; -1 is the last item.
6023 * Returns NULL when "n" is out of range.
6024 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006027 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 long n;
6029{
Bram Moolenaar33570922005-01-25 22:26:29 +00006030 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031 long idx;
6032
6033 if (l == NULL)
6034 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006035
6036 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006038 n = l->lv_len + n;
6039
6040 /* Check for index out of range. */
6041 if (n < 0 || n >= l->lv_len)
6042 return NULL;
6043
6044 /* When there is a cached index may start search from there. */
6045 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006047 if (n < l->lv_idx / 2)
6048 {
6049 /* closest to the start of the list */
6050 item = l->lv_first;
6051 idx = 0;
6052 }
6053 else if (n > (l->lv_idx + l->lv_len) / 2)
6054 {
6055 /* closest to the end of the list */
6056 item = l->lv_last;
6057 idx = l->lv_len - 1;
6058 }
6059 else
6060 {
6061 /* closest to the cached index */
6062 item = l->lv_idx_item;
6063 idx = l->lv_idx;
6064 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 }
6066 else
6067 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006068 if (n < l->lv_len / 2)
6069 {
6070 /* closest to the start of the list */
6071 item = l->lv_first;
6072 idx = 0;
6073 }
6074 else
6075 {
6076 /* closest to the end of the list */
6077 item = l->lv_last;
6078 idx = l->lv_len - 1;
6079 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006081
6082 while (n > idx)
6083 {
6084 /* search forward */
6085 item = item->li_next;
6086 ++idx;
6087 }
6088 while (n < idx)
6089 {
6090 /* search backward */
6091 item = item->li_prev;
6092 --idx;
6093 }
6094
6095 /* cache the used index */
6096 l->lv_idx = idx;
6097 l->lv_idx_item = item;
6098
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006099 return item;
6100}
6101
6102/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006103 * Get list item "l[idx]" as a number.
6104 */
6105 static long
6106list_find_nr(l, idx, errorp)
6107 list_T *l;
6108 long idx;
6109 int *errorp; /* set to TRUE when something wrong */
6110{
6111 listitem_T *li;
6112
6113 li = list_find(l, idx);
6114 if (li == NULL)
6115 {
6116 if (errorp != NULL)
6117 *errorp = TRUE;
6118 return -1L;
6119 }
6120 return get_tv_number_chk(&li->li_tv, errorp);
6121}
6122
6123/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006124 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6125 */
6126 char_u *
6127list_find_str(l, idx)
6128 list_T *l;
6129 long idx;
6130{
6131 listitem_T *li;
6132
6133 li = list_find(l, idx - 1);
6134 if (li == NULL)
6135 {
6136 EMSGN(_(e_listidx), idx);
6137 return NULL;
6138 }
6139 return get_tv_string(&li->li_tv);
6140}
6141
6142/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006143 * Locate "item" list "l" and return its index.
6144 * Returns -1 when "item" is not in the list.
6145 */
6146 static long
6147list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 list_T *l;
6149 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006150{
6151 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006153
6154 if (l == NULL)
6155 return -1;
6156 idx = 0;
6157 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6158 ++idx;
6159 if (li == NULL)
6160 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006161 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006162}
6163
6164/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165 * Append item "item" to the end of list "l".
6166 */
6167 static void
6168list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006169 list_T *l;
6170 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171{
6172 if (l->lv_last == NULL)
6173 {
6174 /* empty list */
6175 l->lv_first = item;
6176 l->lv_last = item;
6177 item->li_prev = NULL;
6178 }
6179 else
6180 {
6181 l->lv_last->li_next = item;
6182 item->li_prev = l->lv_last;
6183 l->lv_last = item;
6184 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006185 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006186 item->li_next = NULL;
6187}
6188
6189/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006190 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006191 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006193 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006194list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006195 list_T *l;
6196 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006198 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199
Bram Moolenaar05159a02005-02-26 23:04:13 +00006200 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006202 copy_tv(tv, &li->li_tv);
6203 list_append(l, li);
6204 return OK;
6205}
6206
6207/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006208 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006209 * Return FAIL when out of memory.
6210 */
6211 int
6212list_append_dict(list, dict)
6213 list_T *list;
6214 dict_T *dict;
6215{
6216 listitem_T *li = listitem_alloc();
6217
6218 if (li == NULL)
6219 return FAIL;
6220 li->li_tv.v_type = VAR_DICT;
6221 li->li_tv.v_lock = 0;
6222 li->li_tv.vval.v_dict = dict;
6223 list_append(list, li);
6224 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006225 return OK;
6226}
6227
6228/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006229 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006230 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006231 * Returns FAIL when out of memory.
6232 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006233 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006234list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006235 list_T *l;
6236 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006237 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006238{
6239 listitem_T *li = listitem_alloc();
6240
6241 if (li == NULL)
6242 return FAIL;
6243 list_append(l, li);
6244 li->li_tv.v_type = VAR_STRING;
6245 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006246 if (str == NULL)
6247 li->li_tv.vval.v_string = NULL;
6248 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006249 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006250 return FAIL;
6251 return OK;
6252}
6253
6254/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006255 * Append "n" to list "l".
6256 * Returns FAIL when out of memory.
6257 */
6258 static int
6259list_append_number(l, n)
6260 list_T *l;
6261 varnumber_T n;
6262{
6263 listitem_T *li;
6264
6265 li = listitem_alloc();
6266 if (li == NULL)
6267 return FAIL;
6268 li->li_tv.v_type = VAR_NUMBER;
6269 li->li_tv.v_lock = 0;
6270 li->li_tv.vval.v_number = n;
6271 list_append(l, li);
6272 return OK;
6273}
6274
6275/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006277 * If "item" is NULL append at the end.
6278 * Return FAIL when out of memory.
6279 */
6280 static int
6281list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 list_T *l;
6283 typval_T *tv;
6284 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006285{
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006287
6288 if (ni == NULL)
6289 return FAIL;
6290 copy_tv(tv, &ni->li_tv);
6291 if (item == NULL)
6292 /* Append new item at end of list. */
6293 list_append(l, ni);
6294 else
6295 {
6296 /* Insert new item before existing item. */
6297 ni->li_prev = item->li_prev;
6298 ni->li_next = item;
6299 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006301 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006302 ++l->lv_idx;
6303 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006304 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006305 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006306 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006307 l->lv_idx_item = NULL;
6308 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006311 }
6312 return OK;
6313}
6314
6315/*
6316 * Extend "l1" with "l2".
6317 * If "bef" is NULL append at the end, otherwise insert before this item.
6318 * Returns FAIL when out of memory.
6319 */
6320 static int
6321list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006322 list_T *l1;
6323 list_T *l2;
6324 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006325{
Bram Moolenaar33570922005-01-25 22:26:29 +00006326 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006327 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006329 /* We also quit the loop when we have inserted the original item count of
6330 * the list, avoid a hang when we extend a list with itself. */
6331 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006332 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6333 return FAIL;
6334 return OK;
6335}
6336
6337/*
6338 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6339 * Return FAIL when out of memory.
6340 */
6341 static int
6342list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 list_T *l1;
6344 list_T *l2;
6345 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006346{
Bram Moolenaar33570922005-01-25 22:26:29 +00006347 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006348
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006349 if (l1 == NULL || l2 == NULL)
6350 return FAIL;
6351
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006352 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006353 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006354 if (l == NULL)
6355 return FAIL;
6356 tv->v_type = VAR_LIST;
6357 tv->vval.v_list = l;
6358
6359 /* append all items from the second list */
6360 return list_extend(l, l2, NULL);
6361}
6362
6363/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006364 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006365 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006366 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006367 * Returns NULL when out of memory.
6368 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006369 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006370list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006373 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006374{
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 list_T *copy;
6376 listitem_T *item;
6377 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006378
6379 if (orig == NULL)
6380 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381
6382 copy = list_alloc();
6383 if (copy != NULL)
6384 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006385 if (copyID != 0)
6386 {
6387 /* Do this before adding the items, because one of the items may
6388 * refer back to this list. */
6389 orig->lv_copyID = copyID;
6390 orig->lv_copylist = copy;
6391 }
6392 for (item = orig->lv_first; item != NULL && !got_int;
6393 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394 {
6395 ni = listitem_alloc();
6396 if (ni == NULL)
6397 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006398 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006399 {
6400 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6401 {
6402 vim_free(ni);
6403 break;
6404 }
6405 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006406 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006407 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408 list_append(copy, ni);
6409 }
6410 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006411 if (item != NULL)
6412 {
6413 list_unref(copy);
6414 copy = NULL;
6415 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 }
6417
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 return copy;
6419}
6420
6421/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006423 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006426list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006427 list_T *l;
6428 listitem_T *item;
6429 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006430{
Bram Moolenaar33570922005-01-25 22:26:29 +00006431 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433 /* notify watchers */
6434 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006436 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437 list_fix_watch(l, ip);
6438 if (ip == item2)
6439 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006440 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441
6442 if (item2->li_next == NULL)
6443 l->lv_last = item->li_prev;
6444 else
6445 item2->li_next->li_prev = item->li_prev;
6446 if (item->li_prev == NULL)
6447 l->lv_first = item2->li_next;
6448 else
6449 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006450 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451}
6452
6453/*
6454 * Return an allocated string with the string representation of a list.
6455 * May return NULL.
6456 */
6457 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006458list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006460 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461{
6462 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463
6464 if (tv->vval.v_list == NULL)
6465 return NULL;
6466 ga_init2(&ga, (int)sizeof(char), 80);
6467 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006468 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006469 {
6470 vim_free(ga.ga_data);
6471 return NULL;
6472 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006473 ga_append(&ga, ']');
6474 ga_append(&ga, NUL);
6475 return (char_u *)ga.ga_data;
6476}
6477
6478/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006479 * Join list "l" into a string in "*gap", using separator "sep".
6480 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006481 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006482 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006483 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006484list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006485 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006487 char_u *sep;
6488 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006489 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006490{
6491 int first = TRUE;
6492 char_u *tofree;
6493 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006495 char_u *s;
6496
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006497 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006498 {
6499 if (first)
6500 first = FALSE;
6501 else
6502 ga_concat(gap, sep);
6503
6504 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006505 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006506 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006507 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006508 if (s != NULL)
6509 ga_concat(gap, s);
6510 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006511 if (s == NULL)
6512 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006513 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006514 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006515 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006516}
6517
6518/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006519 * Garbage collection for lists and dictionaries.
6520 *
6521 * We use reference counts to be able to free most items right away when they
6522 * are no longer used. But for composite items it's possible that it becomes
6523 * unused while the reference count is > 0: When there is a recursive
6524 * reference. Example:
6525 * :let l = [1, 2, 3]
6526 * :let d = {9: l}
6527 * :let l[1] = d
6528 *
6529 * Since this is quite unusual we handle this with garbage collection: every
6530 * once in a while find out which lists and dicts are not referenced from any
6531 * variable.
6532 *
6533 * Here is a good reference text about garbage collection (refers to Python
6534 * but it applies to all reference-counting mechanisms):
6535 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006536 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006537
6538/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006539 * Do garbage collection for lists and dicts.
6540 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006541 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006542 int
6543garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006544{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006545 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006546 buf_T *buf;
6547 win_T *wp;
6548 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006549 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006550 int did_free;
6551 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006552#ifdef FEAT_WINDOWS
6553 tabpage_T *tp;
6554#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006555
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006556 /* Only do this once. */
6557 want_garbage_collect = FALSE;
6558 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006559 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006560
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006561 /* We advance by two because we add one for items referenced through
6562 * previous_funccal. */
6563 current_copyID += COPYID_INC;
6564 copyID = current_copyID;
6565
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006566 /*
6567 * 1. Go through all accessible variables and mark all lists and dicts
6568 * with copyID.
6569 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006570
6571 /* Don't free variables in the previous_funccal list unless they are only
6572 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006573 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006574 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6575 {
6576 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6577 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6578 }
6579
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006580 /* script-local variables */
6581 for (i = 1; i <= ga_scripts.ga_len; ++i)
6582 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6583
6584 /* buffer-local variables */
6585 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6586 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6587
6588 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006589 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006590 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6591
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006592#ifdef FEAT_WINDOWS
6593 /* tabpage-local variables */
6594 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6595 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6596#endif
6597
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006598 /* global variables */
6599 set_ref_in_ht(&globvarht, copyID);
6600
6601 /* function-local variables */
6602 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6603 {
6604 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6605 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6606 }
6607
Bram Moolenaard812df62008-11-09 12:46:09 +00006608 /* v: vars */
6609 set_ref_in_ht(&vimvarht, copyID);
6610
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006611 /*
6612 * 2. Free lists and dictionaries that are not referenced.
6613 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006614 did_free = free_unref_items(copyID);
6615
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006616 /*
6617 * 3. Check if any funccal can be freed now.
6618 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006619 for (pfc = &previous_funccal; *pfc != NULL; )
6620 {
6621 if (can_free_funccal(*pfc, copyID))
6622 {
6623 fc = *pfc;
6624 *pfc = fc->caller;
6625 free_funccal(fc, TRUE);
6626 did_free = TRUE;
6627 did_free_funccal = TRUE;
6628 }
6629 else
6630 pfc = &(*pfc)->caller;
6631 }
6632 if (did_free_funccal)
6633 /* When a funccal was freed some more items might be garbage
6634 * collected, so run again. */
6635 (void)garbage_collect();
6636
6637 return did_free;
6638}
6639
6640/*
6641 * Free lists and dictionaries that are no longer referenced.
6642 */
6643 static int
6644free_unref_items(copyID)
6645 int copyID;
6646{
6647 dict_T *dd;
6648 list_T *ll;
6649 int did_free = FALSE;
6650
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006651 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006652 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006653 */
6654 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006655 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006656 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006657 /* Free the Dictionary and ordinary items it contains, but don't
6658 * recurse into Lists and Dictionaries, they will be in the list
6659 * of dicts or list of lists. */
6660 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006661 did_free = TRUE;
6662
6663 /* restart, next dict may also have been freed */
6664 dd = first_dict;
6665 }
6666 else
6667 dd = dd->dv_used_next;
6668
6669 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006670 * Go through the list of lists and free items without the copyID.
6671 * But don't free a list that has a watcher (used in a for loop), these
6672 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006673 */
6674 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006675 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6676 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006677 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006678 /* Free the List and ordinary items it contains, but don't recurse
6679 * into Lists and Dictionaries, they will be in the list of dicts
6680 * or list of lists. */
6681 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006682 did_free = TRUE;
6683
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006684 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006685 ll = first_list;
6686 }
6687 else
6688 ll = ll->lv_used_next;
6689
6690 return did_free;
6691}
6692
6693/*
6694 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6695 */
6696 static void
6697set_ref_in_ht(ht, copyID)
6698 hashtab_T *ht;
6699 int copyID;
6700{
6701 int todo;
6702 hashitem_T *hi;
6703
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006704 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006705 for (hi = ht->ht_array; todo > 0; ++hi)
6706 if (!HASHITEM_EMPTY(hi))
6707 {
6708 --todo;
6709 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6710 }
6711}
6712
6713/*
6714 * Mark all lists and dicts referenced through list "l" with "copyID".
6715 */
6716 static void
6717set_ref_in_list(l, copyID)
6718 list_T *l;
6719 int copyID;
6720{
6721 listitem_T *li;
6722
6723 for (li = l->lv_first; li != NULL; li = li->li_next)
6724 set_ref_in_item(&li->li_tv, copyID);
6725}
6726
6727/*
6728 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6729 */
6730 static void
6731set_ref_in_item(tv, copyID)
6732 typval_T *tv;
6733 int copyID;
6734{
6735 dict_T *dd;
6736 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006737
6738 switch (tv->v_type)
6739 {
6740 case VAR_DICT:
6741 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006742 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006743 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006744 /* Didn't see this dict yet. */
6745 dd->dv_copyID = copyID;
6746 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006747 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006748 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006749
6750 case VAR_LIST:
6751 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006752 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006753 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006754 /* Didn't see this list yet. */
6755 ll->lv_copyID = copyID;
6756 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006757 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006758 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006759 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006760 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006761}
6762
6763/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006764 * Allocate an empty header for a dictionary.
6765 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006766 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006767dict_alloc()
6768{
Bram Moolenaar33570922005-01-25 22:26:29 +00006769 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006770
Bram Moolenaar33570922005-01-25 22:26:29 +00006771 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006772 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006773 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006774 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006775 if (first_dict != NULL)
6776 first_dict->dv_used_prev = d;
6777 d->dv_used_next = first_dict;
6778 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006779 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006780
Bram Moolenaar33570922005-01-25 22:26:29 +00006781 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006782 d->dv_lock = 0;
6783 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006784 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006785 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006786 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006787}
6788
6789/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006790 * Allocate an empty dict for a return value.
6791 * Returns OK or FAIL.
6792 */
6793 static int
6794rettv_dict_alloc(rettv)
6795 typval_T *rettv;
6796{
6797 dict_T *d = dict_alloc();
6798
6799 if (d == NULL)
6800 return FAIL;
6801
6802 rettv->vval.v_dict = d;
6803 rettv->v_type = VAR_DICT;
6804 ++d->dv_refcount;
6805 return OK;
6806}
6807
6808
6809/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006810 * Unreference a Dictionary: decrement the reference count and free it when it
6811 * becomes zero.
6812 */
6813 static void
6814dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006815 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006816{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006817 if (d != NULL && --d->dv_refcount <= 0)
6818 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006819}
6820
6821/*
6822 * Free a Dictionary, including all items it contains.
6823 * Ignores the reference count.
6824 */
6825 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006826dict_free(d, recurse)
6827 dict_T *d;
6828 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006829{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006830 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006831 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006833
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 /* Remove the dict from the list of dicts for garbage collection. */
6835 if (d->dv_used_prev == NULL)
6836 first_dict = d->dv_used_next;
6837 else
6838 d->dv_used_prev->dv_used_next = d->dv_used_next;
6839 if (d->dv_used_next != NULL)
6840 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6841
6842 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006843 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006844 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006845 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006846 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006847 if (!HASHITEM_EMPTY(hi))
6848 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006849 /* Remove the item before deleting it, just in case there is
6850 * something recursive causing trouble. */
6851 di = HI2DI(hi);
6852 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006853 if (recurse || (di->di_tv.v_type != VAR_LIST
6854 && di->di_tv.v_type != VAR_DICT))
6855 clear_tv(&di->di_tv);
6856 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006857 --todo;
6858 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006859 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006860 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006861 vim_free(d);
6862}
6863
6864/*
6865 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006866 * The "key" is copied to the new item.
6867 * Note that the value of the item "di_tv" still needs to be initialized!
6868 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006869 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006870 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006871dictitem_alloc(key)
6872 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006873{
Bram Moolenaar33570922005-01-25 22:26:29 +00006874 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006875
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006876 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006877 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006878 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006879 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006880 di->di_flags = 0;
6881 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006882 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006883}
6884
6885/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006886 * Make a copy of a Dictionary item.
6887 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006888 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006889dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006890 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006891{
Bram Moolenaar33570922005-01-25 22:26:29 +00006892 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006893
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006894 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6895 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006896 if (di != NULL)
6897 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006898 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006899 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900 copy_tv(&org->di_tv, &di->di_tv);
6901 }
6902 return di;
6903}
6904
6905/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006906 * Remove item "item" from Dictionary "dict" and free it.
6907 */
6908 static void
6909dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006910 dict_T *dict;
6911 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006912{
Bram Moolenaar33570922005-01-25 22:26:29 +00006913 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006914
Bram Moolenaar33570922005-01-25 22:26:29 +00006915 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006916 if (HASHITEM_EMPTY(hi))
6917 EMSG2(_(e_intern2), "dictitem_remove()");
6918 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006920 dictitem_free(item);
6921}
6922
6923/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006924 * Free a dict item. Also clears the value.
6925 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006926 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006927dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006928 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006929{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006930 clear_tv(&item->di_tv);
6931 vim_free(item);
6932}
6933
6934/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006935 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6936 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006937 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006938 * Returns NULL when out of memory.
6939 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006941dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006942 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006943 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006944 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006945{
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 dict_T *copy;
6947 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006948 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006949 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006950
6951 if (orig == NULL)
6952 return NULL;
6953
6954 copy = dict_alloc();
6955 if (copy != NULL)
6956 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006957 if (copyID != 0)
6958 {
6959 orig->dv_copyID = copyID;
6960 orig->dv_copydict = copy;
6961 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006962 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006963 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006964 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006965 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006966 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006967 --todo;
6968
6969 di = dictitem_alloc(hi->hi_key);
6970 if (di == NULL)
6971 break;
6972 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006973 {
6974 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6975 copyID) == FAIL)
6976 {
6977 vim_free(di);
6978 break;
6979 }
6980 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006981 else
6982 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6983 if (dict_add(copy, di) == FAIL)
6984 {
6985 dictitem_free(di);
6986 break;
6987 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006988 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006989 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006990
Bram Moolenaare9a41262005-01-15 22:18:47 +00006991 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006992 if (todo > 0)
6993 {
6994 dict_unref(copy);
6995 copy = NULL;
6996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006997 }
6998
6999 return copy;
7000}
7001
7002/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007003 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007004 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007005 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007006 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007007dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007008 dict_T *d;
7009 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010{
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007012}
7013
Bram Moolenaar8c711452005-01-14 21:53:12 +00007014/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007015 * Add a number or string entry to dictionary "d".
7016 * When "str" is NULL use number "nr", otherwise use "str".
7017 * Returns FAIL when out of memory and when key already exists.
7018 */
7019 int
7020dict_add_nr_str(d, key, nr, str)
7021 dict_T *d;
7022 char *key;
7023 long nr;
7024 char_u *str;
7025{
7026 dictitem_T *item;
7027
7028 item = dictitem_alloc((char_u *)key);
7029 if (item == NULL)
7030 return FAIL;
7031 item->di_tv.v_lock = 0;
7032 if (str == NULL)
7033 {
7034 item->di_tv.v_type = VAR_NUMBER;
7035 item->di_tv.vval.v_number = nr;
7036 }
7037 else
7038 {
7039 item->di_tv.v_type = VAR_STRING;
7040 item->di_tv.vval.v_string = vim_strsave(str);
7041 }
7042 if (dict_add(d, item) == FAIL)
7043 {
7044 dictitem_free(item);
7045 return FAIL;
7046 }
7047 return OK;
7048}
7049
7050/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007051 * Add a list entry to dictionary "d".
7052 * Returns FAIL when out of memory and when key already exists.
7053 */
7054 int
7055dict_add_list(d, key, list)
7056 dict_T *d;
7057 char *key;
7058 list_T *list;
7059{
7060 dictitem_T *item;
7061
7062 item = dictitem_alloc((char_u *)key);
7063 if (item == NULL)
7064 return FAIL;
7065 item->di_tv.v_lock = 0;
7066 item->di_tv.v_type = VAR_LIST;
7067 item->di_tv.vval.v_list = list;
7068 if (dict_add(d, item) == FAIL)
7069 {
7070 dictitem_free(item);
7071 return FAIL;
7072 }
7073 return OK;
7074}
7075
7076/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007077 * Get the number of items in a Dictionary.
7078 */
7079 static long
7080dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007081 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007082{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007083 if (d == NULL)
7084 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007085 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007086}
7087
7088/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007089 * Find item "key[len]" in Dictionary "d".
7090 * If "len" is negative use strlen(key).
7091 * Returns NULL when not found.
7092 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007093 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007094dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096 char_u *key;
7097 int len;
7098{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007099#define AKEYLEN 200
7100 char_u buf[AKEYLEN];
7101 char_u *akey;
7102 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007103 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007105 if (len < 0)
7106 akey = key;
7107 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007108 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007109 tofree = akey = vim_strnsave(key, len);
7110 if (akey == NULL)
7111 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007112 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 else
7114 {
7115 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007116 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 akey = buf;
7118 }
7119
Bram Moolenaar33570922005-01-25 22:26:29 +00007120 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121 vim_free(tofree);
7122 if (HASHITEM_EMPTY(hi))
7123 return NULL;
7124 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007125}
7126
7127/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007128 * Get a string item from a dictionary.
7129 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007130 * Returns NULL if the entry doesn't exist or out of memory.
7131 */
7132 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007133get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007134 dict_T *d;
7135 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007136 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007137{
7138 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007139 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007140
7141 di = dict_find(d, key, -1);
7142 if (di == NULL)
7143 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007144 s = get_tv_string(&di->di_tv);
7145 if (save && s != NULL)
7146 s = vim_strsave(s);
7147 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007148}
7149
7150/*
7151 * Get a number item from a dictionary.
7152 * Returns 0 if the entry doesn't exist or out of memory.
7153 */
7154 long
7155get_dict_number(d, key)
7156 dict_T *d;
7157 char_u *key;
7158{
7159 dictitem_T *di;
7160
7161 di = dict_find(d, key, -1);
7162 if (di == NULL)
7163 return 0;
7164 return get_tv_number(&di->di_tv);
7165}
7166
7167/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168 * Return an allocated string with the string representation of a Dictionary.
7169 * May return NULL.
7170 */
7171 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007172dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007174 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175{
7176 garray_T ga;
7177 int first = TRUE;
7178 char_u *tofree;
7179 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007180 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007185 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007186 return NULL;
7187 ga_init2(&ga, (int)sizeof(char), 80);
7188 ga_append(&ga, '{');
7189
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007190 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007191 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007192 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007194 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 --todo;
7196
7197 if (first)
7198 first = FALSE;
7199 else
7200 ga_concat(&ga, (char_u *)", ");
7201
7202 tofree = string_quote(hi->hi_key, FALSE);
7203 if (tofree != NULL)
7204 {
7205 ga_concat(&ga, tofree);
7206 vim_free(tofree);
7207 }
7208 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007209 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 if (s != NULL)
7211 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007213 if (s == NULL)
7214 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007216 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007217 if (todo > 0)
7218 {
7219 vim_free(ga.ga_data);
7220 return NULL;
7221 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222
7223 ga_append(&ga, '}');
7224 ga_append(&ga, NUL);
7225 return (char_u *)ga.ga_data;
7226}
7227
7228/*
7229 * Allocate a variable for a Dictionary and fill it from "*arg".
7230 * Return OK or FAIL. Returns NOTDONE for {expr}.
7231 */
7232 static int
7233get_dict_tv(arg, rettv, evaluate)
7234 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007235 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236 int evaluate;
7237{
Bram Moolenaar33570922005-01-25 22:26:29 +00007238 dict_T *d = NULL;
7239 typval_T tvkey;
7240 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007241 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007242 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007244 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245
7246 /*
7247 * First check if it's not a curly-braces thing: {expr}.
7248 * Must do this without evaluating, otherwise a function may be called
7249 * twice. Unfortunately this means we need to call eval1() twice for the
7250 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007251 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007253 if (*start != '}')
7254 {
7255 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7256 return FAIL;
7257 if (*start == '}')
7258 return NOTDONE;
7259 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260
7261 if (evaluate)
7262 {
7263 d = dict_alloc();
7264 if (d == NULL)
7265 return FAIL;
7266 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007267 tvkey.v_type = VAR_UNKNOWN;
7268 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269
7270 *arg = skipwhite(*arg + 1);
7271 while (**arg != '}' && **arg != NUL)
7272 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274 goto failret;
7275 if (**arg != ':')
7276 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007277 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007279 goto failret;
7280 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007281 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007283 key = get_tv_string_buf_chk(&tvkey, buf);
7284 if (key == NULL || *key == NUL)
7285 {
7286 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7287 if (key != NULL)
7288 EMSG(_(e_emptykey));
7289 clear_tv(&tvkey);
7290 goto failret;
7291 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293
7294 *arg = skipwhite(*arg + 1);
7295 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7296 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007297 if (evaluate)
7298 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299 goto failret;
7300 }
7301 if (evaluate)
7302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 if (item != NULL)
7305 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007306 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308 clear_tv(&tv);
7309 goto failret;
7310 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311 item = dictitem_alloc(key);
7312 clear_tv(&tvkey);
7313 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007316 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 if (dict_add(d, item) == FAIL)
7318 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319 }
7320 }
7321
7322 if (**arg == '}')
7323 break;
7324 if (**arg != ',')
7325 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007326 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007327 goto failret;
7328 }
7329 *arg = skipwhite(*arg + 1);
7330 }
7331
7332 if (**arg != '}')
7333 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007334 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335failret:
7336 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007337 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 return FAIL;
7339 }
7340
7341 *arg = skipwhite(*arg + 1);
7342 if (evaluate)
7343 {
7344 rettv->v_type = VAR_DICT;
7345 rettv->vval.v_dict = d;
7346 ++d->dv_refcount;
7347 }
7348
7349 return OK;
7350}
7351
Bram Moolenaar8c711452005-01-14 21:53:12 +00007352/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007353 * Return a string with the string representation of a variable.
7354 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007355 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007356 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007357 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007358 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007359 */
7360 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007361echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007363 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007364 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007365 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007366{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367 static int recurse = 0;
7368 char_u *r = NULL;
7369
Bram Moolenaar33570922005-01-25 22:26:29 +00007370 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007372 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373 *tofree = NULL;
7374 return NULL;
7375 }
7376 ++recurse;
7377
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007378 switch (tv->v_type)
7379 {
7380 case VAR_FUNC:
7381 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 r = tv->vval.v_string;
7383 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007384
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007385 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007386 if (tv->vval.v_list == NULL)
7387 {
7388 *tofree = NULL;
7389 r = NULL;
7390 }
7391 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7392 {
7393 *tofree = NULL;
7394 r = (char_u *)"[...]";
7395 }
7396 else
7397 {
7398 tv->vval.v_list->lv_copyID = copyID;
7399 *tofree = list2string(tv, copyID);
7400 r = *tofree;
7401 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007402 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007403
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007405 if (tv->vval.v_dict == NULL)
7406 {
7407 *tofree = NULL;
7408 r = NULL;
7409 }
7410 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7411 {
7412 *tofree = NULL;
7413 r = (char_u *)"{...}";
7414 }
7415 else
7416 {
7417 tv->vval.v_dict->dv_copyID = copyID;
7418 *tofree = dict2string(tv, copyID);
7419 r = *tofree;
7420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007421 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007422
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007423 case VAR_STRING:
7424 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 *tofree = NULL;
7426 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007427 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007428
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007429#ifdef FEAT_FLOAT
7430 case VAR_FLOAT:
7431 *tofree = NULL;
7432 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7433 r = numbuf;
7434 break;
7435#endif
7436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007437 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007438 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007439 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007440 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007441
7442 --recurse;
7443 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007444}
7445
7446/*
7447 * Return a string with the string representation of a variable.
7448 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7449 * "numbuf" is used for a number.
7450 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007451 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007452 */
7453 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007454tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007456 char_u **tofree;
7457 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007458 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007459{
7460 switch (tv->v_type)
7461 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007462 case VAR_FUNC:
7463 *tofree = string_quote(tv->vval.v_string, TRUE);
7464 return *tofree;
7465 case VAR_STRING:
7466 *tofree = string_quote(tv->vval.v_string, FALSE);
7467 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007468#ifdef FEAT_FLOAT
7469 case VAR_FLOAT:
7470 *tofree = NULL;
7471 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7472 return numbuf;
7473#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007474 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007475 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007478 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007479 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007480 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007481 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007482}
7483
7484/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007485 * Return string "str" in ' quotes, doubling ' characters.
7486 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007488 */
7489 static char_u *
7490string_quote(str, function)
7491 char_u *str;
7492 int function;
7493{
Bram Moolenaar33570922005-01-25 22:26:29 +00007494 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007495 char_u *p, *r, *s;
7496
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 len = (function ? 13 : 3);
7498 if (str != NULL)
7499 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007500 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 for (p = str; *p != NUL; mb_ptr_adv(p))
7502 if (*p == '\'')
7503 ++len;
7504 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007505 s = r = alloc(len);
7506 if (r != NULL)
7507 {
7508 if (function)
7509 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007511 r += 10;
7512 }
7513 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007515 if (str != NULL)
7516 for (p = str; *p != NUL; )
7517 {
7518 if (*p == '\'')
7519 *r++ = '\'';
7520 MB_COPY_CHAR(p, r);
7521 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007523 if (function)
7524 *r++ = ')';
7525 *r++ = NUL;
7526 }
7527 return s;
7528}
7529
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007530#ifdef FEAT_FLOAT
7531/*
7532 * Convert the string "text" to a floating point number.
7533 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7534 * this always uses a decimal point.
7535 * Returns the length of the text that was consumed.
7536 */
7537 static int
7538string2float(text, value)
7539 char_u *text;
7540 float_T *value; /* result stored here */
7541{
7542 char *s = (char *)text;
7543 float_T f;
7544
7545 f = strtod(s, &s);
7546 *value = f;
7547 return (int)((char_u *)s - text);
7548}
7549#endif
7550
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 * Get the value of an environment variable.
7553 * "arg" is pointing to the '$'. It is advanced to after the name.
7554 * If the environment variable was not set, silently assume it is empty.
7555 * Always return OK.
7556 */
7557 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007558get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 int evaluate;
7562{
7563 char_u *string = NULL;
7564 int len;
7565 int cc;
7566 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007567 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568
7569 ++*arg;
7570 name = *arg;
7571 len = get_env_len(arg);
7572 if (evaluate)
7573 {
7574 if (len != 0)
7575 {
7576 cc = name[len];
7577 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007578 /* first try vim_getenv(), fast for normal environment vars */
7579 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007581 {
7582 if (!mustfree)
7583 string = vim_strsave(string);
7584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 else
7586 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007587 if (mustfree)
7588 vim_free(string);
7589
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 /* next try expanding things like $VIM and ${HOME} */
7591 string = expand_env_save(name - 1);
7592 if (string != NULL && *string == '$')
7593 {
7594 vim_free(string);
7595 string = NULL;
7596 }
7597 }
7598 name[len] = cc;
7599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 rettv->v_type = VAR_STRING;
7601 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 }
7603
7604 return OK;
7605}
7606
7607/*
7608 * Array with names and number of arguments of all internal functions
7609 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7610 */
7611static struct fst
7612{
7613 char *f_name; /* function name */
7614 char f_min_argc; /* minimal number of arguments */
7615 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007616 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007617 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618} functions[] =
7619{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007620#ifdef FEAT_FLOAT
7621 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007622 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007623#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007624 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 {"append", 2, 2, f_append},
7626 {"argc", 0, 0, f_argc},
7627 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007628 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007629#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007630 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007631 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007632 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007635 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 {"bufexists", 1, 1, f_bufexists},
7637 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7638 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7639 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7640 {"buflisted", 1, 1, f_buflisted},
7641 {"bufloaded", 1, 1, f_bufloaded},
7642 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007643 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 {"bufwinnr", 1, 1, f_bufwinnr},
7645 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007646 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007648#ifdef FEAT_FLOAT
7649 {"ceil", 1, 1, f_ceil},
7650#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007651 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {"char2nr", 1, 1, f_char2nr},
7653 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007654 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007656#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007657 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007658 {"complete_add", 1, 1, f_complete_add},
7659 {"complete_check", 0, 0, f_complete_check},
7660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007662 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007663#ifdef FEAT_FLOAT
7664 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007665 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007666#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007667 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007669 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007670 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {"delete", 1, 1, f_delete},
7672 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007673 {"diff_filler", 1, 1, f_diff_filler},
7674 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007675 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007677 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 {"eventhandler", 0, 0, f_eventhandler},
7679 {"executable", 1, 1, f_executable},
7680 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007681#ifdef FEAT_FLOAT
7682 {"exp", 1, 1, f_exp},
7683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007685 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007686 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7688 {"filereadable", 1, 1, f_filereadable},
7689 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007691 {"finddir", 1, 3, f_finddir},
7692 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007693#ifdef FEAT_FLOAT
7694 {"float2nr", 1, 1, f_float2nr},
7695 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007696 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007697#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007698 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 {"fnamemodify", 2, 2, f_fnamemodify},
7700 {"foldclosed", 1, 1, f_foldclosed},
7701 {"foldclosedend", 1, 1, f_foldclosedend},
7702 {"foldlevel", 1, 1, f_foldlevel},
7703 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007704 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007706 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007707 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007708 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007709 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 {"getbufvar", 2, 2, f_getbufvar},
7711 {"getchar", 0, 1, f_getchar},
7712 {"getcharmod", 0, 0, f_getcharmod},
7713 {"getcmdline", 0, 0, f_getcmdline},
7714 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007715 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007717 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007718 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {"getfsize", 1, 1, f_getfsize},
7720 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007721 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007722 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007723 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007724 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007725 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007726 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007727 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007728 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007730 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007731 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {"getwinposx", 0, 0, f_getwinposx},
7733 {"getwinposy", 0, 0, f_getwinposy},
7734 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007735 {"glob", 1, 2, f_glob},
7736 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007738 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007739 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007740 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7742 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7743 {"histadd", 2, 2, f_histadd},
7744 {"histdel", 1, 2, f_histdel},
7745 {"histget", 1, 2, f_histget},
7746 {"histnr", 1, 1, f_histnr},
7747 {"hlID", 1, 1, f_hlID},
7748 {"hlexists", 1, 1, f_hlexists},
7749 {"hostname", 0, 0, f_hostname},
7750 {"iconv", 3, 3, f_iconv},
7751 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007752 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007753 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007755 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {"inputrestore", 0, 0, f_inputrestore},
7757 {"inputsave", 0, 0, f_inputsave},
7758 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007759 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007761 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007763 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007766 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 {"libcall", 3, 3, f_libcall},
7768 {"libcallnr", 3, 3, f_libcallnr},
7769 {"line", 1, 1, f_line},
7770 {"line2byte", 1, 1, f_line2byte},
7771 {"lispindent", 1, 1, f_lispindent},
7772 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007773#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007774 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007775 {"log10", 1, 1, f_log10},
7776#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007777 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007778 {"maparg", 1, 3, f_maparg},
7779 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007780 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007781 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007782 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007783 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007784 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007785 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007786 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007787 {"max", 1, 1, f_max},
7788 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007789#ifdef vim_mkdir
7790 {"mkdir", 1, 3, f_mkdir},
7791#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007792 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007793#ifdef FEAT_MZSCHEME
7794 {"mzeval", 1, 1, f_mzeval},
7795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 {"nextnonblank", 1, 1, f_nextnonblank},
7797 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007798 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007799#ifdef FEAT_FLOAT
7800 {"pow", 2, 2, f_pow},
7801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007803 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007804 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007805 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007806 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007807 {"reltime", 0, 2, f_reltime},
7808 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 {"remote_expr", 2, 3, f_remote_expr},
7810 {"remote_foreground", 1, 1, f_remote_foreground},
7811 {"remote_peek", 1, 2, f_remote_peek},
7812 {"remote_read", 1, 1, f_remote_read},
7813 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007814 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007816 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007818 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007819#ifdef FEAT_FLOAT
7820 {"round", 1, 1, f_round},
7821#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007822 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007823 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007824 {"searchpair", 3, 7, f_searchpair},
7825 {"searchpairpos", 3, 7, f_searchpairpos},
7826 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 {"server2client", 2, 2, f_server2client},
7828 {"serverlist", 0, 0, f_serverlist},
7829 {"setbufvar", 3, 3, f_setbufvar},
7830 {"setcmdpos", 1, 1, f_setcmdpos},
7831 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007832 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007833 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007834 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007835 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007837 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007838 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007840 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007842#ifdef FEAT_FLOAT
7843 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007844 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007846 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007847 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007848 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007849 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007850 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007851#ifdef FEAT_FLOAT
7852 {"sqrt", 1, 1, f_sqrt},
7853 {"str2float", 1, 1, f_str2float},
7854#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007855 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856#ifdef HAVE_STRFTIME
7857 {"strftime", 1, 2, f_strftime},
7858#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007859 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007860 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {"strlen", 1, 1, f_strlen},
7862 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007863 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {"strtrans", 1, 1, f_strtrans},
7865 {"submatch", 1, 1, f_submatch},
7866 {"substitute", 4, 4, f_substitute},
7867 {"synID", 3, 3, f_synID},
7868 {"synIDattr", 2, 3, f_synIDattr},
7869 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007870 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007871 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007872 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007873 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007874 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007875 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007876 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007877#ifdef FEAT_FLOAT
7878 {"tan", 1, 1, f_tan},
7879 {"tanh", 1, 1, f_tanh},
7880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007882 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {"tolower", 1, 1, f_tolower},
7884 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007885 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886#ifdef FEAT_FLOAT
7887 {"trunc", 1, 1, f_trunc},
7888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007890 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007891 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007892 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"virtcol", 1, 1, f_virtcol},
7894 {"visualmode", 0, 1, f_visualmode},
7895 {"winbufnr", 1, 1, f_winbufnr},
7896 {"wincol", 0, 0, f_wincol},
7897 {"winheight", 1, 1, f_winheight},
7898 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007899 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007901 {"winrestview", 1, 1, f_winrestview},
7902 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007904 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905};
7906
7907#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7908
7909/*
7910 * Function given to ExpandGeneric() to obtain the list of internal
7911 * or user defined function names.
7912 */
7913 char_u *
7914get_function_name(xp, idx)
7915 expand_T *xp;
7916 int idx;
7917{
7918 static int intidx = -1;
7919 char_u *name;
7920
7921 if (idx == 0)
7922 intidx = -1;
7923 if (intidx < 0)
7924 {
7925 name = get_user_func_name(xp, idx);
7926 if (name != NULL)
7927 return name;
7928 }
7929 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7930 {
7931 STRCPY(IObuff, functions[intidx].f_name);
7932 STRCAT(IObuff, "(");
7933 if (functions[intidx].f_max_argc == 0)
7934 STRCAT(IObuff, ")");
7935 return IObuff;
7936 }
7937
7938 return NULL;
7939}
7940
7941/*
7942 * Function given to ExpandGeneric() to obtain the list of internal or
7943 * user defined variable or function names.
7944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 char_u *
7946get_expr_name(xp, idx)
7947 expand_T *xp;
7948 int idx;
7949{
7950 static int intidx = -1;
7951 char_u *name;
7952
7953 if (idx == 0)
7954 intidx = -1;
7955 if (intidx < 0)
7956 {
7957 name = get_function_name(xp, idx);
7958 if (name != NULL)
7959 return name;
7960 }
7961 return get_user_var_name(xp, ++intidx);
7962}
7963
7964#endif /* FEAT_CMDL_COMPL */
7965
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007966#if defined(EBCDIC) || defined(PROTO)
7967/*
7968 * Compare struct fst by function name.
7969 */
7970 static int
7971compare_func_name(s1, s2)
7972 const void *s1;
7973 const void *s2;
7974{
7975 struct fst *p1 = (struct fst *)s1;
7976 struct fst *p2 = (struct fst *)s2;
7977
7978 return STRCMP(p1->f_name, p2->f_name);
7979}
7980
7981/*
7982 * Sort the function table by function name.
7983 * The sorting of the table above is ASCII dependant.
7984 * On machines using EBCDIC we have to sort it.
7985 */
7986 static void
7987sortFunctions()
7988{
7989 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7990
7991 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
7992}
7993#endif
7994
7995
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996/*
7997 * Find internal function in table above.
7998 * Return index, or -1 if not found
7999 */
8000 static int
8001find_internal_func(name)
8002 char_u *name; /* name of the function */
8003{
8004 int first = 0;
8005 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8006 int cmp;
8007 int x;
8008
8009 /*
8010 * Find the function name in the table. Binary search.
8011 */
8012 while (first <= last)
8013 {
8014 x = first + ((unsigned)(last - first) >> 1);
8015 cmp = STRCMP(name, functions[x].f_name);
8016 if (cmp < 0)
8017 last = x - 1;
8018 else if (cmp > 0)
8019 first = x + 1;
8020 else
8021 return x;
8022 }
8023 return -1;
8024}
8025
8026/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008027 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8028 * name it contains, otherwise return "name".
8029 */
8030 static char_u *
8031deref_func_name(name, lenp)
8032 char_u *name;
8033 int *lenp;
8034{
Bram Moolenaar33570922005-01-25 22:26:29 +00008035 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008036 int cc;
8037
8038 cc = name[*lenp];
8039 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008040 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008041 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008042 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008043 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008045 {
8046 *lenp = 0;
8047 return (char_u *)""; /* just in case */
8048 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008049 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008050 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008051 }
8052
8053 return name;
8054}
8055
8056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 * Allocate a variable for the result of a function.
8058 * Return OK or FAIL.
8059 */
8060 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008061get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8062 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 char_u *name; /* name of the function */
8064 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 char_u **arg; /* argument, pointing to the '(' */
8067 linenr_T firstline; /* first line of range */
8068 linenr_T lastline; /* last line of range */
8069 int *doesrange; /* return: function handled range */
8070 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008071 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072{
8073 char_u *argp;
8074 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008075 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 int argcount = 0; /* number of arguments found */
8077
8078 /*
8079 * Get the arguments.
8080 */
8081 argp = *arg;
8082 while (argcount < MAX_FUNC_ARGS)
8083 {
8084 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8085 if (*argp == ')' || *argp == ',' || *argp == NUL)
8086 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8088 {
8089 ret = FAIL;
8090 break;
8091 }
8092 ++argcount;
8093 if (*argp != ',')
8094 break;
8095 }
8096 if (*argp == ')')
8097 ++argp;
8098 else
8099 ret = FAIL;
8100
8101 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008102 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008103 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008105 {
8106 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008107 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008108 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008109 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111
8112 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008113 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114
8115 *arg = skipwhite(argp);
8116 return ret;
8117}
8118
8119
8120/*
8121 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008122 * Return OK when the function can't be called, FAIL otherwise.
8123 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 */
8125 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008126call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008127 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008128 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008130 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008132 typval_T *argvars; /* vars for arguments, must have "argcount"
8133 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 linenr_T firstline; /* first line of range */
8135 linenr_T lastline; /* last line of range */
8136 int *doesrange; /* return: function handled range */
8137 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008138 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139{
8140 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141#define ERROR_UNKNOWN 0
8142#define ERROR_TOOMANY 1
8143#define ERROR_TOOFEW 2
8144#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008145#define ERROR_DICT 4
8146#define ERROR_NONE 5
8147#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 int error = ERROR_NONE;
8149 int i;
8150 int llen;
8151 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152#define FLEN_FIXED 40
8153 char_u fname_buf[FLEN_FIXED + 1];
8154 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008155 char_u *name;
8156
8157 /* Make a copy of the name, if it comes from a funcref variable it could
8158 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008159 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008160 if (name == NULL)
8161 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162
8163 /*
8164 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8165 * Change <SNR>123_name() to K_SNR 123_name().
8166 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 llen = eval_fname_script(name);
8169 if (llen > 0)
8170 {
8171 fname_buf[0] = K_SPECIAL;
8172 fname_buf[1] = KS_EXTRA;
8173 fname_buf[2] = (int)KE_SNR;
8174 i = 3;
8175 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8176 {
8177 if (current_SID <= 0)
8178 error = ERROR_SCRIPT;
8179 else
8180 {
8181 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8182 i = (int)STRLEN(fname_buf);
8183 }
8184 }
8185 if (i + STRLEN(name + llen) < FLEN_FIXED)
8186 {
8187 STRCPY(fname_buf + i, name + llen);
8188 fname = fname_buf;
8189 }
8190 else
8191 {
8192 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8193 if (fname == NULL)
8194 error = ERROR_OTHER;
8195 else
8196 {
8197 mch_memmove(fname, fname_buf, (size_t)i);
8198 STRCPY(fname + i, name + llen);
8199 }
8200 }
8201 }
8202 else
8203 fname = name;
8204
8205 *doesrange = FALSE;
8206
8207
8208 /* execute the function if no errors detected and executing */
8209 if (evaluate && error == ERROR_NONE)
8210 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008211 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8212 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 error = ERROR_UNKNOWN;
8214
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008215 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {
8217 /*
8218 * User defined function.
8219 */
8220 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008221
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008223 /* Trigger FuncUndefined event, may load the function. */
8224 if (fp == NULL
8225 && apply_autocmds(EVENT_FUNCUNDEFINED,
8226 fname, fname, TRUE, NULL)
8227 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008229 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 fp = find_func(fname);
8231 }
8232#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008233 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008234 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008235 {
8236 /* loaded a package, search for the function again */
8237 fp = find_func(fname);
8238 }
8239
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 if (fp != NULL)
8241 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008242 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008244 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008246 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008248 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008249 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 else
8251 {
8252 /*
8253 * Call the user function.
8254 * Save and restore search patterns, script variables and
8255 * redo buffer.
8256 */
8257 save_search_patterns();
8258 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008259 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008260 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008261 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008262 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8263 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8264 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008265 /* Function was unreferenced while being used, free it
8266 * now. */
8267 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 restoreRedobuff();
8269 restore_search_patterns();
8270 error = ERROR_NONE;
8271 }
8272 }
8273 }
8274 else
8275 {
8276 /*
8277 * Find the function name in the table, call its implementation.
8278 */
8279 i = find_internal_func(fname);
8280 if (i >= 0)
8281 {
8282 if (argcount < functions[i].f_min_argc)
8283 error = ERROR_TOOFEW;
8284 else if (argcount > functions[i].f_max_argc)
8285 error = ERROR_TOOMANY;
8286 else
8287 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008288 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008289 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 error = ERROR_NONE;
8291 }
8292 }
8293 }
8294 /*
8295 * The function call (or "FuncUndefined" autocommand sequence) might
8296 * have been aborted by an error, an interrupt, or an explicitly thrown
8297 * exception that has not been caught so far. This situation can be
8298 * tested for by calling aborting(). For an error in an internal
8299 * function or for the "E132" error in call_user_func(), however, the
8300 * throw point at which the "force_abort" flag (temporarily reset by
8301 * emsg()) is normally updated has not been reached yet. We need to
8302 * update that flag first to make aborting() reliable.
8303 */
8304 update_force_abort();
8305 }
8306 if (error == ERROR_NONE)
8307 ret = OK;
8308
8309 /*
8310 * Report an error unless the argument evaluation or function call has been
8311 * cancelled due to an aborting error, an interrupt, or an exception.
8312 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008313 if (!aborting())
8314 {
8315 switch (error)
8316 {
8317 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008318 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008319 break;
8320 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008321 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008322 break;
8323 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008324 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008325 name);
8326 break;
8327 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008328 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008329 name);
8330 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008331 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008332 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008333 name);
8334 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008335 }
8336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 if (fname != name && fname != fname_buf)
8339 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008340 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341
8342 return ret;
8343}
8344
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008345/*
8346 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008347 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008348 */
8349 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008350emsg_funcname(ermsg, name)
8351 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008352 char_u *name;
8353{
8354 char_u *p;
8355
8356 if (*name == K_SPECIAL)
8357 p = concat_str((char_u *)"<SNR>", name + 3);
8358 else
8359 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008360 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008361 if (p != name)
8362 vim_free(p);
8363}
8364
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008365/*
8366 * Return TRUE for a non-zero Number and a non-empty String.
8367 */
8368 static int
8369non_zero_arg(argvars)
8370 typval_T *argvars;
8371{
8372 return ((argvars[0].v_type == VAR_NUMBER
8373 && argvars[0].vval.v_number != 0)
8374 || (argvars[0].v_type == VAR_STRING
8375 && argvars[0].vval.v_string != NULL
8376 && *argvars[0].vval.v_string != NUL));
8377}
8378
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379/*********************************************
8380 * Implementation of the built-in functions
8381 */
8382
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008383#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008384static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8385
8386/*
8387 * Get the float value of "argvars[0]" into "f".
8388 * Returns FAIL when the argument is not a Number or Float.
8389 */
8390 static int
8391get_float_arg(argvars, f)
8392 typval_T *argvars;
8393 float_T *f;
8394{
8395 if (argvars[0].v_type == VAR_FLOAT)
8396 {
8397 *f = argvars[0].vval.v_float;
8398 return OK;
8399 }
8400 if (argvars[0].v_type == VAR_NUMBER)
8401 {
8402 *f = (float_T)argvars[0].vval.v_number;
8403 return OK;
8404 }
8405 EMSG(_("E808: Number or Float required"));
8406 return FAIL;
8407}
8408
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008409/*
8410 * "abs(expr)" function
8411 */
8412 static void
8413f_abs(argvars, rettv)
8414 typval_T *argvars;
8415 typval_T *rettv;
8416{
8417 if (argvars[0].v_type == VAR_FLOAT)
8418 {
8419 rettv->v_type = VAR_FLOAT;
8420 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8421 }
8422 else
8423 {
8424 varnumber_T n;
8425 int error = FALSE;
8426
8427 n = get_tv_number_chk(&argvars[0], &error);
8428 if (error)
8429 rettv->vval.v_number = -1;
8430 else if (n > 0)
8431 rettv->vval.v_number = n;
8432 else
8433 rettv->vval.v_number = -n;
8434 }
8435}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008436
8437/*
8438 * "acos()" function
8439 */
8440 static void
8441f_acos(argvars, rettv)
8442 typval_T *argvars;
8443 typval_T *rettv;
8444{
8445 float_T f;
8446
8447 rettv->v_type = VAR_FLOAT;
8448 if (get_float_arg(argvars, &f) == OK)
8449 rettv->vval.v_float = acos(f);
8450 else
8451 rettv->vval.v_float = 0.0;
8452}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008453#endif
8454
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008456 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 */
8458 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008459f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008460 typval_T *argvars;
8461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462{
Bram Moolenaar33570922005-01-25 22:26:29 +00008463 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008465 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008466 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008468 if ((l = argvars[0].vval.v_list) != NULL
8469 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8470 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008471 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008472 }
8473 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008474 EMSG(_(e_listreq));
8475}
8476
8477/*
8478 * "append(lnum, string/list)" function
8479 */
8480 static void
8481f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008482 typval_T *argvars;
8483 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008484{
8485 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008486 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008487 list_T *l = NULL;
8488 listitem_T *li = NULL;
8489 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008490 long added = 0;
8491
Bram Moolenaar0d660222005-01-07 21:51:51 +00008492 lnum = get_tv_lnum(argvars);
8493 if (lnum >= 0
8494 && lnum <= curbuf->b_ml.ml_line_count
8495 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008496 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008497 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008498 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008499 l = argvars[1].vval.v_list;
8500 if (l == NULL)
8501 return;
8502 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008503 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008504 for (;;)
8505 {
8506 if (l == NULL)
8507 tv = &argvars[1]; /* append a string */
8508 else if (li == NULL)
8509 break; /* end of list */
8510 else
8511 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008512 line = get_tv_string_chk(tv);
8513 if (line == NULL) /* type error */
8514 {
8515 rettv->vval.v_number = 1; /* Failed */
8516 break;
8517 }
8518 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008519 ++added;
8520 if (l == NULL)
8521 break;
8522 li = li->li_next;
8523 }
8524
8525 appended_lines_mark(lnum, added);
8526 if (curwin->w_cursor.lnum > lnum)
8527 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008529 else
8530 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531}
8532
8533/*
8534 * "argc()" function
8535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008537f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008538 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008541 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542}
8543
8544/*
8545 * "argidx()" function
8546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008548f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008549 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008552 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553}
8554
8555/*
8556 * "argv(nr)" function
8557 */
8558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008559f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008560 typval_T *argvars;
8561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562{
8563 int idx;
8564
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008565 if (argvars[0].v_type != VAR_UNKNOWN)
8566 {
8567 idx = get_tv_number_chk(&argvars[0], NULL);
8568 if (idx >= 0 && idx < ARGCOUNT)
8569 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8570 else
8571 rettv->vval.v_string = NULL;
8572 rettv->v_type = VAR_STRING;
8573 }
8574 else if (rettv_list_alloc(rettv) == OK)
8575 for (idx = 0; idx < ARGCOUNT; ++idx)
8576 list_append_string(rettv->vval.v_list,
8577 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578}
8579
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008580#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008581/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008582 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008583 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008584 static void
8585f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008586 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008587 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008588{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008589 float_T f;
8590
8591 rettv->v_type = VAR_FLOAT;
8592 if (get_float_arg(argvars, &f) == OK)
8593 rettv->vval.v_float = asin(f);
8594 else
8595 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008596}
8597
8598/*
8599 * "atan()" function
8600 */
8601 static void
8602f_atan(argvars, rettv)
8603 typval_T *argvars;
8604 typval_T *rettv;
8605{
8606 float_T f;
8607
8608 rettv->v_type = VAR_FLOAT;
8609 if (get_float_arg(argvars, &f) == OK)
8610 rettv->vval.v_float = atan(f);
8611 else
8612 rettv->vval.v_float = 0.0;
8613}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008614
8615/*
8616 * "atan2()" function
8617 */
8618 static void
8619f_atan2(argvars, rettv)
8620 typval_T *argvars;
8621 typval_T *rettv;
8622{
8623 float_T fx, fy;
8624
8625 rettv->v_type = VAR_FLOAT;
8626 if (get_float_arg(argvars, &fx) == OK
8627 && get_float_arg(&argvars[1], &fy) == OK)
8628 rettv->vval.v_float = atan2(fx, fy);
8629 else
8630 rettv->vval.v_float = 0.0;
8631}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008632#endif
8633
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634/*
8635 * "browse(save, title, initdir, default)" function
8636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008639 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641{
8642#ifdef FEAT_BROWSE
8643 int save;
8644 char_u *title;
8645 char_u *initdir;
8646 char_u *defname;
8647 char_u buf[NUMBUFLEN];
8648 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008649 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008651 save = get_tv_number_chk(&argvars[0], &error);
8652 title = get_tv_string_chk(&argvars[1]);
8653 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8654 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008656 if (error || title == NULL || initdir == NULL || defname == NULL)
8657 rettv->vval.v_string = NULL;
8658 else
8659 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008660 do_browse(save ? BROWSE_SAVE : 0,
8661 title, defname, NULL, initdir, NULL, curbuf);
8662#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008663 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008664#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008666}
8667
8668/*
8669 * "browsedir(title, initdir)" function
8670 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008672f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008673 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008674 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008675{
8676#ifdef FEAT_BROWSE
8677 char_u *title;
8678 char_u *initdir;
8679 char_u buf[NUMBUFLEN];
8680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008681 title = get_tv_string_chk(&argvars[0]);
8682 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008684 if (title == NULL || initdir == NULL)
8685 rettv->vval.v_string = NULL;
8686 else
8687 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008688 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008692 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693}
8694
Bram Moolenaar33570922005-01-25 22:26:29 +00008695static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697/*
8698 * Find a buffer by number or exact name.
8699 */
8700 static buf_T *
8701find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703{
8704 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008706 if (avar->v_type == VAR_NUMBER)
8707 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008708 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008710 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008711 if (buf == NULL)
8712 {
8713 /* No full path name match, try a match with a URL or a "nofile"
8714 * buffer, these don't use the full path. */
8715 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8716 if (buf->b_fname != NULL
8717 && (path_with_url(buf->b_fname)
8718#ifdef FEAT_QUICKFIX
8719 || bt_nofile(buf)
8720#endif
8721 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008722 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008723 break;
8724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 }
8726 return buf;
8727}
8728
8729/*
8730 * "bufexists(expr)" function
8731 */
8732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008733f_bufexists(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738}
8739
8740/*
8741 * "buflisted(expr)" function
8742 */
8743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008745 typval_T *argvars;
8746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747{
8748 buf_T *buf;
8749
8750 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752}
8753
8754/*
8755 * "bufloaded(expr)" function
8756 */
8757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008758f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008759 typval_T *argvars;
8760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 buf_T *buf;
8763
8764 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008765 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766}
8767
Bram Moolenaar33570922005-01-25 22:26:29 +00008768static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008769
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770/*
8771 * Get buffer by number or pattern.
8772 */
8773 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008775 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 int save_magic;
8779 char_u *save_cpo;
8780 buf_T *buf;
8781
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008782 if (tv->v_type == VAR_NUMBER)
8783 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008784 if (tv->v_type != VAR_STRING)
8785 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 if (name == NULL || *name == NUL)
8787 return curbuf;
8788 if (name[0] == '$' && name[1] == NUL)
8789 return lastbuf;
8790
8791 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8792 save_magic = p_magic;
8793 p_magic = TRUE;
8794 save_cpo = p_cpo;
8795 p_cpo = (char_u *)"";
8796
8797 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8798 TRUE, FALSE));
8799
8800 p_magic = save_magic;
8801 p_cpo = save_cpo;
8802
8803 /* If not found, try expanding the name, like done for bufexists(). */
8804 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806
8807 return buf;
8808}
8809
8810/*
8811 * "bufname(expr)" function
8812 */
8813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008815 typval_T *argvars;
8816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817{
8818 buf_T *buf;
8819
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008820 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008822 buf = get_buf_tv(&argvars[0]);
8823 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 --emsg_off;
8829}
8830
8831/*
8832 * "bufnr(expr)" function
8833 */
8834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008835f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008836 typval_T *argvars;
8837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
8839 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008840 int error = FALSE;
8841 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008843 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008846 --emsg_off;
8847
8848 /* If the buffer isn't found and the second argument is not zero create a
8849 * new buffer. */
8850 if (buf == NULL
8851 && argvars[1].v_type != VAR_UNKNOWN
8852 && get_tv_number_chk(&argvars[1], &error) != 0
8853 && !error
8854 && (name = get_tv_string_chk(&argvars[0])) != NULL
8855 && !error)
8856 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8857
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008859 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008861 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862}
8863
8864/*
8865 * "bufwinnr(nr)" function
8866 */
8867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008869 typval_T *argvars;
8870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
8872#ifdef FEAT_WINDOWS
8873 win_T *wp;
8874 int winnr = 0;
8875#endif
8876 buf_T *buf;
8877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008878 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881#ifdef FEAT_WINDOWS
8882 for (wp = firstwin; wp; wp = wp->w_next)
8883 {
8884 ++winnr;
8885 if (wp->w_buffer == buf)
8886 break;
8887 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008888 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008890 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891#endif
8892 --emsg_off;
8893}
8894
8895/*
8896 * "byte2line(byte)" function
8897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008900 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
8903#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905#else
8906 long boff = 0;
8907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008908 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008910 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008912 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 (linenr_T)0, &boff);
8914#endif
8915}
8916
8917/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008918 * "byteidx()" function
8919 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008922 typval_T *argvars;
8923 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008924{
8925#ifdef FEAT_MBYTE
8926 char_u *t;
8927#endif
8928 char_u *str;
8929 long idx;
8930
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008931 str = get_tv_string_chk(&argvars[0]);
8932 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008935 return;
8936
8937#ifdef FEAT_MBYTE
8938 t = str;
8939 for ( ; idx > 0; idx--)
8940 {
8941 if (*t == NUL) /* EOL reached */
8942 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008943 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008944 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008945 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008946#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008947 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008949#endif
8950}
8951
8952/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008953 * "call(func, arglist)" function
8954 */
8955 static void
8956f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008957 typval_T *argvars;
8958 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008959{
8960 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008961 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008962 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008963 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008964 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008965 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008966
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008967 if (argvars[1].v_type != VAR_LIST)
8968 {
8969 EMSG(_(e_listreq));
8970 return;
8971 }
8972 if (argvars[1].vval.v_list == NULL)
8973 return;
8974
8975 if (argvars[0].v_type == VAR_FUNC)
8976 func = argvars[0].vval.v_string;
8977 else
8978 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008979 if (*func == NUL)
8980 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008981
Bram Moolenaare9a41262005-01-15 22:18:47 +00008982 if (argvars[2].v_type != VAR_UNKNOWN)
8983 {
8984 if (argvars[2].v_type != VAR_DICT)
8985 {
8986 EMSG(_(e_dictreq));
8987 return;
8988 }
8989 selfdict = argvars[2].vval.v_dict;
8990 }
8991
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008992 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8993 item = item->li_next)
8994 {
8995 if (argc == MAX_FUNC_ARGS)
8996 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008997 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008998 break;
8999 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009000 /* Make a copy of each argument. This is needed to be able to set
9001 * v_lock to VAR_FIXED in the copy without changing the original list.
9002 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009003 copy_tv(&item->li_tv, &argv[argc++]);
9004 }
9005
9006 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009007 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009008 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9009 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009010
9011 /* Free the arguments. */
9012 while (argc > 0)
9013 clear_tv(&argv[--argc]);
9014}
9015
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009016#ifdef FEAT_FLOAT
9017/*
9018 * "ceil({float})" function
9019 */
9020 static void
9021f_ceil(argvars, rettv)
9022 typval_T *argvars;
9023 typval_T *rettv;
9024{
9025 float_T f;
9026
9027 rettv->v_type = VAR_FLOAT;
9028 if (get_float_arg(argvars, &f) == OK)
9029 rettv->vval.v_float = ceil(f);
9030 else
9031 rettv->vval.v_float = 0.0;
9032}
9033#endif
9034
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009035/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009036 * "changenr()" function
9037 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009038 static void
9039f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009040 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009041 typval_T *rettv;
9042{
9043 rettv->vval.v_number = curbuf->b_u_seq_cur;
9044}
9045
9046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 * "char2nr(string)" function
9048 */
9049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054#ifdef FEAT_MBYTE
9055 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009056 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 else
9058#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009059 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060}
9061
9062/*
9063 * "cindent(lnum)" function
9064 */
9065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009066f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009067 typval_T *argvars;
9068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069{
9070#ifdef FEAT_CINDENT
9071 pos_T pos;
9072 linenr_T lnum;
9073
9074 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9077 {
9078 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 curwin->w_cursor = pos;
9081 }
9082 else
9083#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085}
9086
9087/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009088 * "clearmatches()" function
9089 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009090 static void
9091f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009092 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009093 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009094{
9095#ifdef FEAT_SEARCH_EXTRA
9096 clear_matches(curwin);
9097#endif
9098}
9099
9100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 * "col(string)" function
9102 */
9103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009104f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009105 typval_T *argvars;
9106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
9108 colnr_T col = 0;
9109 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009110 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009112 fp = var2fpos(&argvars[0], FALSE, &fnum);
9113 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 {
9115 if (fp->col == MAXCOL)
9116 {
9117 /* '> can be MAXCOL, get the length of the line then */
9118 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009119 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 else
9121 col = MAXCOL;
9122 }
9123 else
9124 {
9125 col = fp->col + 1;
9126#ifdef FEAT_VIRTUALEDIT
9127 /* col(".") when the cursor is on the NUL at the end of the line
9128 * because of "coladd" can be seen as an extra column. */
9129 if (virtual_active() && fp == &curwin->w_cursor)
9130 {
9131 char_u *p = ml_get_cursor();
9132
9133 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9134 curwin->w_virtcol - curwin->w_cursor.coladd))
9135 {
9136# ifdef FEAT_MBYTE
9137 int l;
9138
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009139 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 col += l;
9141# else
9142 if (*p != NUL && p[1] == NUL)
9143 ++col;
9144# endif
9145 }
9146 }
9147#endif
9148 }
9149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009150 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151}
9152
Bram Moolenaar572cb562005-08-05 21:35:02 +00009153#if defined(FEAT_INS_EXPAND)
9154/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009155 * "complete()" function
9156 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009157 static void
9158f_complete(argvars, rettv)
9159 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009160 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009161{
9162 int startcol;
9163
9164 if ((State & INSERT) == 0)
9165 {
9166 EMSG(_("E785: complete() can only be used in Insert mode"));
9167 return;
9168 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009169
9170 /* Check for undo allowed here, because if something was already inserted
9171 * the line was already saved for undo and this check isn't done. */
9172 if (!undo_allowed())
9173 return;
9174
Bram Moolenaarade00832006-03-10 21:46:58 +00009175 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9176 {
9177 EMSG(_(e_invarg));
9178 return;
9179 }
9180
9181 startcol = get_tv_number_chk(&argvars[0], NULL);
9182 if (startcol <= 0)
9183 return;
9184
9185 set_completion(startcol - 1, argvars[1].vval.v_list);
9186}
9187
9188/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009189 * "complete_add()" function
9190 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009191 static void
9192f_complete_add(argvars, rettv)
9193 typval_T *argvars;
9194 typval_T *rettv;
9195{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009196 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009197}
9198
9199/*
9200 * "complete_check()" function
9201 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009202 static void
9203f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009204 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009205 typval_T *rettv;
9206{
9207 int saved = RedrawingDisabled;
9208
9209 RedrawingDisabled = 0;
9210 ins_compl_check_keys(0);
9211 rettv->vval.v_number = compl_interrupted;
9212 RedrawingDisabled = saved;
9213}
9214#endif
9215
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216/*
9217 * "confirm(message, buttons[, default [, type]])" function
9218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009220f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009221 typval_T *argvars UNUSED;
9222 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9225 char_u *message;
9226 char_u *buttons = NULL;
9227 char_u buf[NUMBUFLEN];
9228 char_u buf2[NUMBUFLEN];
9229 int def = 1;
9230 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009231 char_u *typestr;
9232 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234 message = get_tv_string_chk(&argvars[0]);
9235 if (message == NULL)
9236 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009237 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009239 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9240 if (buttons == NULL)
9241 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009242 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009244 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009245 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009247 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9248 if (typestr == NULL)
9249 error = TRUE;
9250 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009252 switch (TOUPPER_ASC(*typestr))
9253 {
9254 case 'E': type = VIM_ERROR; break;
9255 case 'Q': type = VIM_QUESTION; break;
9256 case 'I': type = VIM_INFO; break;
9257 case 'W': type = VIM_WARNING; break;
9258 case 'G': type = VIM_GENERIC; break;
9259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 }
9261 }
9262 }
9263 }
9264
9265 if (buttons == NULL || *buttons == NUL)
9266 buttons = (char_u *)_("&Ok");
9267
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009268 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009269 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271#endif
9272}
9273
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009274/*
9275 * "copy()" function
9276 */
9277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009278f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009279 typval_T *argvars;
9280 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009281{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009282 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009283}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009285#ifdef FEAT_FLOAT
9286/*
9287 * "cos()" function
9288 */
9289 static void
9290f_cos(argvars, rettv)
9291 typval_T *argvars;
9292 typval_T *rettv;
9293{
9294 float_T f;
9295
9296 rettv->v_type = VAR_FLOAT;
9297 if (get_float_arg(argvars, &f) == OK)
9298 rettv->vval.v_float = cos(f);
9299 else
9300 rettv->vval.v_float = 0.0;
9301}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009302
9303/*
9304 * "cosh()" function
9305 */
9306 static void
9307f_cosh(argvars, rettv)
9308 typval_T *argvars;
9309 typval_T *rettv;
9310{
9311 float_T f;
9312
9313 rettv->v_type = VAR_FLOAT;
9314 if (get_float_arg(argvars, &f) == OK)
9315 rettv->vval.v_float = cosh(f);
9316 else
9317 rettv->vval.v_float = 0.0;
9318}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009319#endif
9320
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009322 * "count()" function
9323 */
9324 static void
9325f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009326 typval_T *argvars;
9327 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009328{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009329 long n = 0;
9330 int ic = FALSE;
9331
Bram Moolenaare9a41262005-01-15 22:18:47 +00009332 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009333 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009334 listitem_T *li;
9335 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009336 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009337
Bram Moolenaare9a41262005-01-15 22:18:47 +00009338 if ((l = argvars[0].vval.v_list) != NULL)
9339 {
9340 li = l->lv_first;
9341 if (argvars[2].v_type != VAR_UNKNOWN)
9342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009343 int error = FALSE;
9344
9345 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009346 if (argvars[3].v_type != VAR_UNKNOWN)
9347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009348 idx = get_tv_number_chk(&argvars[3], &error);
9349 if (!error)
9350 {
9351 li = list_find(l, idx);
9352 if (li == NULL)
9353 EMSGN(_(e_listidx), idx);
9354 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009355 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009356 if (error)
9357 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009358 }
9359
9360 for ( ; li != NULL; li = li->li_next)
9361 if (tv_equal(&li->li_tv, &argvars[1], ic))
9362 ++n;
9363 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009364 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009365 else if (argvars[0].v_type == VAR_DICT)
9366 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009367 int todo;
9368 dict_T *d;
9369 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009370
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009371 if ((d = argvars[0].vval.v_dict) != NULL)
9372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009373 int error = FALSE;
9374
Bram Moolenaare9a41262005-01-15 22:18:47 +00009375 if (argvars[2].v_type != VAR_UNKNOWN)
9376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009377 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009378 if (argvars[3].v_type != VAR_UNKNOWN)
9379 EMSG(_(e_invarg));
9380 }
9381
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009382 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009384 {
9385 if (!HASHITEM_EMPTY(hi))
9386 {
9387 --todo;
9388 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9389 ++n;
9390 }
9391 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009392 }
9393 }
9394 else
9395 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009396 rettv->vval.v_number = n;
9397}
9398
9399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9401 *
9402 * Checks the existence of a cscope connection.
9403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009405f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009406 typval_T *argvars UNUSED;
9407 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408{
9409#ifdef FEAT_CSCOPE
9410 int num = 0;
9411 char_u *dbpath = NULL;
9412 char_u *prepend = NULL;
9413 char_u buf[NUMBUFLEN];
9414
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009415 if (argvars[0].v_type != VAR_UNKNOWN
9416 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 num = (int)get_tv_number(&argvars[0]);
9419 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009420 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 }
9423
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425#endif
9426}
9427
9428/*
9429 * "cursor(lnum, col)" function
9430 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009431 * Moves the cursor to the specified line and column.
9432 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009436 typval_T *argvars;
9437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439 long line, col;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009440#ifdef FEAT_CONCEAL
9441 linenr_T oldline = curwin->w_cursor.lnum;
9442#endif
Bram Moolenaara5525202006-03-02 22:52:09 +00009443#ifdef FEAT_VIRTUALEDIT
9444 long coladd = 0;
9445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009447 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009448 if (argvars[1].v_type == VAR_UNKNOWN)
9449 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009450 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009451
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009452 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009453 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009454 line = pos.lnum;
9455 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009456#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009457 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009458#endif
9459 }
9460 else
9461 {
9462 line = get_tv_lnum(argvars);
9463 col = get_tv_number_chk(&argvars[1], NULL);
9464#ifdef FEAT_VIRTUALEDIT
9465 if (argvars[2].v_type != VAR_UNKNOWN)
9466 coladd = get_tv_number_chk(&argvars[2], NULL);
9467#endif
9468 }
9469 if (line < 0 || col < 0
9470#ifdef FEAT_VIRTUALEDIT
9471 || coladd < 0
9472#endif
9473 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 if (line > 0)
9476 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 if (col > 0)
9478 curwin->w_cursor.col = col - 1;
9479#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009480 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481#endif
9482
9483 /* Make sure the cursor is in a valid position. */
9484 check_cursor();
9485#ifdef FEAT_MBYTE
9486 /* Correct cursor for multi-byte character. */
9487 if (has_mbyte)
9488 mb_adjust_cursor();
9489#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009490
9491 curwin->w_set_curswant = TRUE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009492#ifdef FEAT_CONCEAL
9493 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
9494 {
9495 update_single_line(curwin, oldline);
9496 update_single_line(curwin, curwin->w_cursor.lnum);
9497 }
9498#endif
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009499 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500}
9501
9502/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009503 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 */
9505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009507 typval_T *argvars;
9508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009510 int noref = 0;
9511
9512 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009514 if (noref < 0 || noref > 1)
9515 EMSG(_(e_invarg));
9516 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009517 {
9518 current_copyID += COPYID_INC;
9519 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521}
9522
9523/*
9524 * "delete()" function
9525 */
9526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009528 typval_T *argvars;
9529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
9531 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009532 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535}
9536
9537/*
9538 * "did_filetype()" function
9539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009542 typval_T *argvars UNUSED;
9543 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544{
9545#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009546 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547#endif
9548}
9549
9550/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009551 * "diff_filler()" function
9552 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009555 typval_T *argvars UNUSED;
9556 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009557{
9558#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009560#endif
9561}
9562
9563/*
9564 * "diff_hlID()" function
9565 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009568 typval_T *argvars UNUSED;
9569 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009570{
9571#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009573 static linenr_T prev_lnum = 0;
9574 static int changedtick = 0;
9575 static int fnum = 0;
9576 static int change_start = 0;
9577 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009578 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009579 int filler_lines;
9580 int col;
9581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009582 if (lnum < 0) /* ignore type error in {lnum} arg */
9583 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009584 if (lnum != prev_lnum
9585 || changedtick != curbuf->b_changedtick
9586 || fnum != curbuf->b_fnum)
9587 {
9588 /* New line, buffer, change: need to get the values. */
9589 filler_lines = diff_check(curwin, lnum);
9590 if (filler_lines < 0)
9591 {
9592 if (filler_lines == -1)
9593 {
9594 change_start = MAXCOL;
9595 change_end = -1;
9596 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9597 hlID = HLF_ADD; /* added line */
9598 else
9599 hlID = HLF_CHD; /* changed line */
9600 }
9601 else
9602 hlID = HLF_ADD; /* added line */
9603 }
9604 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009605 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009606 prev_lnum = lnum;
9607 changedtick = curbuf->b_changedtick;
9608 fnum = curbuf->b_fnum;
9609 }
9610
9611 if (hlID == HLF_CHD || hlID == HLF_TXD)
9612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009613 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009614 if (col >= change_start && col <= change_end)
9615 hlID = HLF_TXD; /* changed text */
9616 else
9617 hlID = HLF_CHD; /* changed line */
9618 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009619 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009620#endif
9621}
9622
9623/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009624 * "empty({expr})" function
9625 */
9626 static void
9627f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009628 typval_T *argvars;
9629 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009630{
9631 int n;
9632
9633 switch (argvars[0].v_type)
9634 {
9635 case VAR_STRING:
9636 case VAR_FUNC:
9637 n = argvars[0].vval.v_string == NULL
9638 || *argvars[0].vval.v_string == NUL;
9639 break;
9640 case VAR_NUMBER:
9641 n = argvars[0].vval.v_number == 0;
9642 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009643#ifdef FEAT_FLOAT
9644 case VAR_FLOAT:
9645 n = argvars[0].vval.v_float == 0.0;
9646 break;
9647#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009648 case VAR_LIST:
9649 n = argvars[0].vval.v_list == NULL
9650 || argvars[0].vval.v_list->lv_first == NULL;
9651 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009652 case VAR_DICT:
9653 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009654 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009655 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009656 default:
9657 EMSG2(_(e_intern2), "f_empty()");
9658 n = 0;
9659 }
9660
9661 rettv->vval.v_number = n;
9662}
9663
9664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 * "escape({string}, {chars})" function
9666 */
9667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009668f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009669 typval_T *argvars;
9670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671{
9672 char_u buf[NUMBUFLEN];
9673
Bram Moolenaar758711c2005-02-02 23:11:38 +00009674 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9675 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677}
9678
9679/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009680 * "eval()" function
9681 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009682 static void
9683f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009684 typval_T *argvars;
9685 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009686{
9687 char_u *s;
9688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009689 s = get_tv_string_chk(&argvars[0]);
9690 if (s != NULL)
9691 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9694 {
9695 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009696 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009697 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009698 else if (*s != NUL)
9699 EMSG(_(e_trailing));
9700}
9701
9702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 * "eventhandler()" function
9704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009707 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711}
9712
9713/*
9714 * "executable()" function
9715 */
9716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009718 typval_T *argvars;
9719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722}
9723
9724/*
9725 * "exists()" function
9726 */
9727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009728f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009729 typval_T *argvars;
9730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731{
9732 char_u *p;
9733 char_u *name;
9734 int n = FALSE;
9735 int len = 0;
9736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 if (*p == '$') /* environment variable */
9739 {
9740 /* first try "normal" environment variables (fast) */
9741 if (mch_getenv(p + 1) != NULL)
9742 n = TRUE;
9743 else
9744 {
9745 /* try expanding things like $VIM and ${HOME} */
9746 p = expand_env_save(p);
9747 if (p != NULL && *p != '$')
9748 n = TRUE;
9749 vim_free(p);
9750 }
9751 }
9752 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009755 if (*skipwhite(p) != NUL)
9756 n = FALSE; /* trailing garbage */
9757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 else if (*p == '*') /* internal or user defined function */
9759 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009760 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 }
9762 else if (*p == ':')
9763 {
9764 n = cmd_exists(p + 1);
9765 }
9766 else if (*p == '#')
9767 {
9768#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009769 if (p[1] == '#')
9770 n = autocmd_supported(p + 2);
9771 else
9772 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773#endif
9774 }
9775 else /* internal variable */
9776 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009777 char_u *tofree;
9778 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009780 /* get_name_len() takes care of expanding curly braces */
9781 name = p;
9782 len = get_name_len(&p, &tofree, TRUE, FALSE);
9783 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009785 if (tofree != NULL)
9786 name = tofree;
9787 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9788 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009790 /* handle d.key, l[idx], f(expr) */
9791 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9792 if (n)
9793 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 }
9795 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009796 if (*p != NUL)
9797 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009799 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 }
9801
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803}
9804
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009805#ifdef FEAT_FLOAT
9806/*
9807 * "exp()" function
9808 */
9809 static void
9810f_exp(argvars, rettv)
9811 typval_T *argvars;
9812 typval_T *rettv;
9813{
9814 float_T f;
9815
9816 rettv->v_type = VAR_FLOAT;
9817 if (get_float_arg(argvars, &f) == OK)
9818 rettv->vval.v_float = exp(f);
9819 else
9820 rettv->vval.v_float = 0.0;
9821}
9822#endif
9823
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824/*
9825 * "expand()" function
9826 */
9827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009829 typval_T *argvars;
9830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831{
9832 char_u *s;
9833 int len;
9834 char_u *errormsg;
9835 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9836 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009837 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009839 rettv->v_type = VAR_STRING;
9840 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 if (*s == '%' || *s == '#' || *s == '<')
9842 {
9843 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009844 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 --emsg_off;
9846 }
9847 else
9848 {
9849 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009850 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009851 if (argvars[1].v_type != VAR_UNKNOWN
9852 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009854 if (!error)
9855 {
9856 ExpandInit(&xpc);
9857 xpc.xp_context = EXPAND_FILES;
9858 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009859 }
9860 else
9861 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 }
9863}
9864
9865/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009866 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009867 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009868 */
9869 static void
9870f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009871 typval_T *argvars;
9872 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009873{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009874 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009875 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009876 list_T *l1, *l2;
9877 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009878 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009879 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009880
Bram Moolenaare9a41262005-01-15 22:18:47 +00009881 l1 = argvars[0].vval.v_list;
9882 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009883 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9884 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009885 {
9886 if (argvars[2].v_type != VAR_UNKNOWN)
9887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009888 before = get_tv_number_chk(&argvars[2], &error);
9889 if (error)
9890 return; /* type error; errmsg already given */
9891
Bram Moolenaar758711c2005-02-02 23:11:38 +00009892 if (before == l1->lv_len)
9893 item = NULL;
9894 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009895 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009896 item = list_find(l1, before);
9897 if (item == NULL)
9898 {
9899 EMSGN(_(e_listidx), before);
9900 return;
9901 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009902 }
9903 }
9904 else
9905 item = NULL;
9906 list_extend(l1, l2, item);
9907
Bram Moolenaare9a41262005-01-15 22:18:47 +00009908 copy_tv(&argvars[0], rettv);
9909 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009910 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009911 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9912 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009913 dict_T *d1, *d2;
9914 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009915 char_u *action;
9916 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009917 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009918 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009919
9920 d1 = argvars[0].vval.v_dict;
9921 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009922 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9923 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009924 {
9925 /* Check the third argument. */
9926 if (argvars[2].v_type != VAR_UNKNOWN)
9927 {
9928 static char *(av[]) = {"keep", "force", "error"};
9929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009930 action = get_tv_string_chk(&argvars[2]);
9931 if (action == NULL)
9932 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009933 for (i = 0; i < 3; ++i)
9934 if (STRCMP(action, av[i]) == 0)
9935 break;
9936 if (i == 3)
9937 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009938 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009939 return;
9940 }
9941 }
9942 else
9943 action = (char_u *)"force";
9944
9945 /* Go over all entries in the second dict and add them to the
9946 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009947 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009948 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009949 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009950 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009951 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009952 --todo;
9953 di1 = dict_find(d1, hi2->hi_key, -1);
9954 if (di1 == NULL)
9955 {
9956 di1 = dictitem_copy(HI2DI(hi2));
9957 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9958 dictitem_free(di1);
9959 }
9960 else if (*action == 'e')
9961 {
9962 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9963 break;
9964 }
9965 else if (*action == 'f')
9966 {
9967 clear_tv(&di1->di_tv);
9968 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9969 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 }
9971 }
9972
Bram Moolenaare9a41262005-01-15 22:18:47 +00009973 copy_tv(&argvars[0], rettv);
9974 }
9975 }
9976 else
9977 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009978}
9979
9980/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009981 * "feedkeys()" function
9982 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009983 static void
9984f_feedkeys(argvars, rettv)
9985 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009986 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009987{
9988 int remap = TRUE;
9989 char_u *keys, *flags;
9990 char_u nbuf[NUMBUFLEN];
9991 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009992 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009993
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009994 /* This is not allowed in the sandbox. If the commands would still be
9995 * executed in the sandbox it would be OK, but it probably happens later,
9996 * when "sandbox" is no longer set. */
9997 if (check_secure())
9998 return;
9999
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010000 keys = get_tv_string(&argvars[0]);
10001 if (*keys != NUL)
10002 {
10003 if (argvars[1].v_type != VAR_UNKNOWN)
10004 {
10005 flags = get_tv_string_buf(&argvars[1], nbuf);
10006 for ( ; *flags != NUL; ++flags)
10007 {
10008 switch (*flags)
10009 {
10010 case 'n': remap = FALSE; break;
10011 case 'm': remap = TRUE; break;
10012 case 't': typed = TRUE; break;
10013 }
10014 }
10015 }
10016
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010017 /* Need to escape K_SPECIAL and CSI before putting the string in the
10018 * typeahead buffer. */
10019 keys_esc = vim_strsave_escape_csi(keys);
10020 if (keys_esc != NULL)
10021 {
10022 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010023 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010024 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010025 if (vgetc_busy)
10026 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010027 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010028 }
10029}
10030
10031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 * "filereadable()" function
10033 */
10034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010035f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010036 typval_T *argvars;
10037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010039 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 char_u *p;
10041 int n;
10042
Bram Moolenaarc236c162008-07-13 17:41:49 +000010043#ifndef O_NONBLOCK
10044# define O_NONBLOCK 0
10045#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010047 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10048 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 {
10050 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010051 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 }
10053 else
10054 n = FALSE;
10055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057}
10058
10059/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010060 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 * rights to write into.
10062 */
10063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010065 typval_T *argvars;
10066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010068 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069}
10070
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010071static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010072
10073 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010074findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010075 typval_T *argvars;
10076 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010077 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010078{
10079#ifdef FEAT_SEARCHPATH
10080 char_u *fname;
10081 char_u *fresult = NULL;
10082 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10083 char_u *p;
10084 char_u pathbuf[NUMBUFLEN];
10085 int count = 1;
10086 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010087 int error = FALSE;
10088#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010089
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010090 rettv->vval.v_string = NULL;
10091 rettv->v_type = VAR_STRING;
10092
10093#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010095
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010096 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10099 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010100 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010101 else
10102 {
10103 if (*p != NUL)
10104 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010107 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010108 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010109 }
10110
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010111 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10112 error = TRUE;
10113
10114 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010116 do
10117 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010118 if (rettv->v_type == VAR_STRING)
10119 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010120 fresult = find_file_in_path_option(first ? fname : NULL,
10121 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010122 0, first, path,
10123 find_what,
10124 curbuf->b_ffname,
10125 find_what == FINDFILE_DIR
10126 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010128
10129 if (fresult != NULL && rettv->v_type == VAR_LIST)
10130 list_append_string(rettv->vval.v_list, fresult, -1);
10131
10132 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010133 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010134
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010135 if (rettv->v_type == VAR_STRING)
10136 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010137#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010138}
10139
Bram Moolenaar33570922005-01-25 22:26:29 +000010140static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10141static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010142
10143/*
10144 * Implementation of map() and filter().
10145 */
10146 static void
10147filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010148 typval_T *argvars;
10149 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010150 int map;
10151{
10152 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010153 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010154 listitem_T *li, *nli;
10155 list_T *l = NULL;
10156 dictitem_T *di;
10157 hashtab_T *ht;
10158 hashitem_T *hi;
10159 dict_T *d = NULL;
10160 typval_T save_val;
10161 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010162 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010163 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010164 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010165 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010166 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010167
Bram Moolenaare9a41262005-01-15 22:18:47 +000010168 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010169 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010170 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010171 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010172 return;
10173 }
10174 else if (argvars[0].v_type == VAR_DICT)
10175 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010176 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010177 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010178 return;
10179 }
10180 else
10181 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010182 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010183 return;
10184 }
10185
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010186 expr = get_tv_string_buf_chk(&argvars[1], buf);
10187 /* On type errors, the preceding call has already displayed an error
10188 * message. Avoid a misleading error message for an empty string that
10189 * was not passed as argument. */
10190 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010192 prepare_vimvar(VV_VAL, &save_val);
10193 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010194
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010195 /* We reset "did_emsg" to be able to detect whether an error
10196 * occurred during evaluation of the expression. */
10197 save_did_emsg = did_emsg;
10198 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010199
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010200 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010201 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010203 vimvars[VV_KEY].vv_type = VAR_STRING;
10204
10205 ht = &d->dv_hashtab;
10206 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010207 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010210 if (!HASHITEM_EMPTY(hi))
10211 {
10212 --todo;
10213 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010214 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 break;
10216 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010217 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010218 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 break;
10220 if (!map && rem)
10221 dictitem_remove(d, di);
10222 clear_tv(&vimvars[VV_KEY].vv_tv);
10223 }
10224 }
10225 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010226 }
10227 else
10228 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010229 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 for (li = l->lv_first; li != NULL; li = nli)
10232 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010233 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010234 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010236 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010237 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010238 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010239 break;
10240 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010242 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010243 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010244 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010245
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010246 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010247 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010248
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010249 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010250 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010251
10252 copy_tv(&argvars[0], rettv);
10253}
10254
10255 static int
10256filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010257 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010258 char_u *expr;
10259 int map;
10260 int *remp;
10261{
Bram Moolenaar33570922005-01-25 22:26:29 +000010262 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010263 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010264 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010265
Bram Moolenaar33570922005-01-25 22:26:29 +000010266 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010267 s = expr;
10268 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010269 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010270 if (*s != NUL) /* check for trailing chars after expr */
10271 {
10272 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010273 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010274 }
10275 if (map)
10276 {
10277 /* map(): replace the list item value */
10278 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010279 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010280 *tv = rettv;
10281 }
10282 else
10283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010284 int error = FALSE;
10285
Bram Moolenaare9a41262005-01-15 22:18:47 +000010286 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010287 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010288 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 /* On type error, nothing has been removed; return FAIL to stop the
10290 * loop. The error message was given by get_tv_number_chk(). */
10291 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010292 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010293 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010294 retval = OK;
10295theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010296 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010297 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010298}
10299
10300/*
10301 * "filter()" function
10302 */
10303 static void
10304f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010305 typval_T *argvars;
10306 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010307{
10308 filter_map(argvars, rettv, FALSE);
10309}
10310
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010311/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010312 * "finddir({fname}[, {path}[, {count}]])" function
10313 */
10314 static void
10315f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010316 typval_T *argvars;
10317 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010318{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010319 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010320}
10321
10322/*
10323 * "findfile({fname}[, {path}[, {count}]])" function
10324 */
10325 static void
10326f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010327 typval_T *argvars;
10328 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010329{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010330 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010331}
10332
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010333#ifdef FEAT_FLOAT
10334/*
10335 * "float2nr({float})" function
10336 */
10337 static void
10338f_float2nr(argvars, rettv)
10339 typval_T *argvars;
10340 typval_T *rettv;
10341{
10342 float_T f;
10343
10344 if (get_float_arg(argvars, &f) == OK)
10345 {
10346 if (f < -0x7fffffff)
10347 rettv->vval.v_number = -0x7fffffff;
10348 else if (f > 0x7fffffff)
10349 rettv->vval.v_number = 0x7fffffff;
10350 else
10351 rettv->vval.v_number = (varnumber_T)f;
10352 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010353}
10354
10355/*
10356 * "floor({float})" function
10357 */
10358 static void
10359f_floor(argvars, rettv)
10360 typval_T *argvars;
10361 typval_T *rettv;
10362{
10363 float_T f;
10364
10365 rettv->v_type = VAR_FLOAT;
10366 if (get_float_arg(argvars, &f) == OK)
10367 rettv->vval.v_float = floor(f);
10368 else
10369 rettv->vval.v_float = 0.0;
10370}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010371
10372/*
10373 * "fmod()" function
10374 */
10375 static void
10376f_fmod(argvars, rettv)
10377 typval_T *argvars;
10378 typval_T *rettv;
10379{
10380 float_T fx, fy;
10381
10382 rettv->v_type = VAR_FLOAT;
10383 if (get_float_arg(argvars, &fx) == OK
10384 && get_float_arg(&argvars[1], &fy) == OK)
10385 rettv->vval.v_float = fmod(fx, fy);
10386 else
10387 rettv->vval.v_float = 0.0;
10388}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010389#endif
10390
Bram Moolenaar0d660222005-01-07 21:51:51 +000010391/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010392 * "fnameescape({string})" function
10393 */
10394 static void
10395f_fnameescape(argvars, rettv)
10396 typval_T *argvars;
10397 typval_T *rettv;
10398{
10399 rettv->vval.v_string = vim_strsave_fnameescape(
10400 get_tv_string(&argvars[0]), FALSE);
10401 rettv->v_type = VAR_STRING;
10402}
10403
10404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 * "fnamemodify({fname}, {mods})" function
10406 */
10407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010408f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010409 typval_T *argvars;
10410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411{
10412 char_u *fname;
10413 char_u *mods;
10414 int usedlen = 0;
10415 int len;
10416 char_u *fbuf = NULL;
10417 char_u buf[NUMBUFLEN];
10418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010419 fname = get_tv_string_chk(&argvars[0]);
10420 mods = get_tv_string_buf_chk(&argvars[1], buf);
10421 if (fname == NULL || mods == NULL)
10422 fname = NULL;
10423 else
10424 {
10425 len = (int)STRLEN(fname);
10426 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010431 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010433 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 vim_free(fbuf);
10435}
10436
Bram Moolenaar33570922005-01-25 22:26:29 +000010437static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438
10439/*
10440 * "foldclosed()" function
10441 */
10442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010443foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010444 typval_T *argvars;
10445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 int end;
10447{
10448#ifdef FEAT_FOLDING
10449 linenr_T lnum;
10450 linenr_T first, last;
10451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10454 {
10455 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10456 {
10457 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010458 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010460 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 return;
10462 }
10463 }
10464#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010465 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466}
10467
10468/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010469 * "foldclosed()" function
10470 */
10471 static void
10472f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010473 typval_T *argvars;
10474 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010475{
10476 foldclosed_both(argvars, rettv, FALSE);
10477}
10478
10479/*
10480 * "foldclosedend()" function
10481 */
10482 static void
10483f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010484 typval_T *argvars;
10485 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010486{
10487 foldclosed_both(argvars, rettv, TRUE);
10488}
10489
10490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 * "foldlevel()" function
10492 */
10493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010494f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010495 typval_T *argvars;
10496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497{
10498#ifdef FEAT_FOLDING
10499 linenr_T lnum;
10500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010501 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010503 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505}
10506
10507/*
10508 * "foldtext()" function
10509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010511f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010512 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514{
10515#ifdef FEAT_FOLDING
10516 linenr_T lnum;
10517 char_u *s;
10518 char_u *r;
10519 int len;
10520 char *txt;
10521#endif
10522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010523 rettv->v_type = VAR_STRING;
10524 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010526 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10527 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10528 <= curbuf->b_ml.ml_line_count
10529 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 {
10531 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010532 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10533 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 {
10535 if (!linewhite(lnum))
10536 break;
10537 ++lnum;
10538 }
10539
10540 /* Find interesting text in this line. */
10541 s = skipwhite(ml_get(lnum));
10542 /* skip C comment-start */
10543 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010544 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010546 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010547 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010548 {
10549 s = skipwhite(ml_get(lnum + 1));
10550 if (*s == '*')
10551 s = skipwhite(s + 1);
10552 }
10553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 txt = _("+-%s%3ld lines: ");
10555 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010556 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 + 20 /* for %3ld */
10558 + STRLEN(s))); /* concatenated */
10559 if (r != NULL)
10560 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010561 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10562 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10563 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 len = (int)STRLEN(r);
10565 STRCAT(r, s);
10566 /* remove 'foldmarker' and 'commentstring' */
10567 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010568 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 }
10570 }
10571#endif
10572}
10573
10574/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010575 * "foldtextresult(lnum)" function
10576 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010578f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010579 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010580 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010581{
10582#ifdef FEAT_FOLDING
10583 linenr_T lnum;
10584 char_u *text;
10585 char_u buf[51];
10586 foldinfo_T foldinfo;
10587 int fold_count;
10588#endif
10589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010590 rettv->v_type = VAR_STRING;
10591 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010592#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010594 /* treat illegal types and illegal string values for {lnum} the same */
10595 if (lnum < 0)
10596 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010597 fold_count = foldedCount(curwin, lnum, &foldinfo);
10598 if (fold_count > 0)
10599 {
10600 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10601 &foldinfo, buf);
10602 if (text == buf)
10603 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010604 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010605 }
10606#endif
10607}
10608
10609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 * "foreground()" function
10611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010613f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010614 typval_T *argvars UNUSED;
10615 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617#ifdef FEAT_GUI
10618 if (gui.in_use)
10619 gui_mch_set_foreground();
10620#else
10621# ifdef WIN32
10622 win32_set_foreground();
10623# endif
10624#endif
10625}
10626
10627/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010628 * "function()" function
10629 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010632 typval_T *argvars;
10633 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010634{
10635 char_u *s;
10636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010637 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010638 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010639 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010640 /* Don't check an autoload name for existence here. */
10641 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010642 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010643 else
10644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645 rettv->vval.v_string = vim_strsave(s);
10646 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010647 }
10648}
10649
10650/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010651 * "garbagecollect()" function
10652 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010653 static void
10654f_garbagecollect(argvars, rettv)
10655 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010656 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010657{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010658 /* This is postponed until we are back at the toplevel, because we may be
10659 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10660 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010661
10662 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10663 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010664}
10665
10666/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010667 * "get()" function
10668 */
10669 static void
10670f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T *argvars;
10672 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010673{
Bram Moolenaar33570922005-01-25 22:26:29 +000010674 listitem_T *li;
10675 list_T *l;
10676 dictitem_T *di;
10677 dict_T *d;
10678 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010679
Bram Moolenaare9a41262005-01-15 22:18:47 +000010680 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010681 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010682 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010684 int error = FALSE;
10685
10686 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10687 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010688 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010689 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010690 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010691 else if (argvars[0].v_type == VAR_DICT)
10692 {
10693 if ((d = argvars[0].vval.v_dict) != NULL)
10694 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010695 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010696 if (di != NULL)
10697 tv = &di->di_tv;
10698 }
10699 }
10700 else
10701 EMSG2(_(e_listdictarg), "get()");
10702
10703 if (tv == NULL)
10704 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010705 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010706 copy_tv(&argvars[2], rettv);
10707 }
10708 else
10709 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010710}
10711
Bram Moolenaar342337a2005-07-21 21:11:17 +000010712static 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 +000010713
10714/*
10715 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010716 * Return a range (from start to end) of lines in rettv from the specified
10717 * buffer.
10718 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010719 */
10720 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010721get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010722 buf_T *buf;
10723 linenr_T start;
10724 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010725 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010726 typval_T *rettv;
10727{
10728 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010729
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010730 if (retlist && rettv_list_alloc(rettv) == FAIL)
10731 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010732
10733 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10734 return;
10735
10736 if (!retlist)
10737 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010738 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10739 p = ml_get_buf(buf, start, FALSE);
10740 else
10741 p = (char_u *)"";
10742
10743 rettv->v_type = VAR_STRING;
10744 rettv->vval.v_string = vim_strsave(p);
10745 }
10746 else
10747 {
10748 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010749 return;
10750
10751 if (start < 1)
10752 start = 1;
10753 if (end > buf->b_ml.ml_line_count)
10754 end = buf->b_ml.ml_line_count;
10755 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010756 if (list_append_string(rettv->vval.v_list,
10757 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010758 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010759 }
10760}
10761
10762/*
10763 * "getbufline()" function
10764 */
10765 static void
10766f_getbufline(argvars, rettv)
10767 typval_T *argvars;
10768 typval_T *rettv;
10769{
10770 linenr_T lnum;
10771 linenr_T end;
10772 buf_T *buf;
10773
10774 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10775 ++emsg_off;
10776 buf = get_buf_tv(&argvars[0]);
10777 --emsg_off;
10778
Bram Moolenaar661b1822005-07-28 22:36:45 +000010779 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010780 if (argvars[2].v_type == VAR_UNKNOWN)
10781 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010782 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010783 end = get_tv_lnum_buf(&argvars[2], buf);
10784
Bram Moolenaar342337a2005-07-21 21:11:17 +000010785 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010786}
10787
Bram Moolenaar0d660222005-01-07 21:51:51 +000010788/*
10789 * "getbufvar()" function
10790 */
10791 static void
10792f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010793 typval_T *argvars;
10794 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010795{
10796 buf_T *buf;
10797 buf_T *save_curbuf;
10798 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010799 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010801 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10802 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010803 ++emsg_off;
10804 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010805
10806 rettv->v_type = VAR_STRING;
10807 rettv->vval.v_string = NULL;
10808
10809 if (buf != NULL && varname != NULL)
10810 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010811 /* set curbuf to be our buf, temporarily */
10812 save_curbuf = curbuf;
10813 curbuf = buf;
10814
Bram Moolenaar0d660222005-01-07 21:51:51 +000010815 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010816 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010817 else
10818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010819 if (*varname == NUL)
10820 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10821 * scope prefix before the NUL byte is required by
10822 * find_var_in_ht(). */
10823 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010824 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010825 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010826 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010827 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010828 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010829
10830 /* restore previous notion of curbuf */
10831 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010832 }
10833
10834 --emsg_off;
10835}
10836
10837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 * "getchar()" function
10839 */
10840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010841f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010842 typval_T *argvars;
10843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844{
10845 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010846 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010848 /* Position the cursor. Needed after a message that ends in a space. */
10849 windgoto(msg_row, msg_col);
10850
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 ++no_mapping;
10852 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010853 for (;;)
10854 {
10855 if (argvars[0].v_type == VAR_UNKNOWN)
10856 /* getchar(): blocking wait. */
10857 n = safe_vgetc();
10858 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10859 /* getchar(1): only check if char avail */
10860 n = vpeekc();
10861 else if (error || vpeekc() == NUL)
10862 /* illegal argument or getchar(0) and no char avail: return zero */
10863 n = 0;
10864 else
10865 /* getchar(0) and char avail: return char */
10866 n = safe_vgetc();
10867 if (n == K_IGNORE)
10868 continue;
10869 break;
10870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 --no_mapping;
10872 --allow_keys;
10873
Bram Moolenaar219b8702006-11-01 14:32:36 +000010874 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10875 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10876 vimvars[VV_MOUSE_COL].vv_nr = 0;
10877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 if (IS_SPECIAL(n) || mod_mask != 0)
10880 {
10881 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10882 int i = 0;
10883
10884 /* Turn a special key into three bytes, plus modifier. */
10885 if (mod_mask != 0)
10886 {
10887 temp[i++] = K_SPECIAL;
10888 temp[i++] = KS_MODIFIER;
10889 temp[i++] = mod_mask;
10890 }
10891 if (IS_SPECIAL(n))
10892 {
10893 temp[i++] = K_SPECIAL;
10894 temp[i++] = K_SECOND(n);
10895 temp[i++] = K_THIRD(n);
10896 }
10897#ifdef FEAT_MBYTE
10898 else if (has_mbyte)
10899 i += (*mb_char2bytes)(n, temp + i);
10900#endif
10901 else
10902 temp[i++] = n;
10903 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010904 rettv->v_type = VAR_STRING;
10905 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010906
10907#ifdef FEAT_MOUSE
10908 if (n == K_LEFTMOUSE
10909 || n == K_LEFTMOUSE_NM
10910 || n == K_LEFTDRAG
10911 || n == K_LEFTRELEASE
10912 || n == K_LEFTRELEASE_NM
10913 || n == K_MIDDLEMOUSE
10914 || n == K_MIDDLEDRAG
10915 || n == K_MIDDLERELEASE
10916 || n == K_RIGHTMOUSE
10917 || n == K_RIGHTDRAG
10918 || n == K_RIGHTRELEASE
10919 || n == K_X1MOUSE
10920 || n == K_X1DRAG
10921 || n == K_X1RELEASE
10922 || n == K_X2MOUSE
10923 || n == K_X2DRAG
10924 || n == K_X2RELEASE
10925 || n == K_MOUSEDOWN
10926 || n == K_MOUSEUP)
10927 {
10928 int row = mouse_row;
10929 int col = mouse_col;
10930 win_T *win;
10931 linenr_T lnum;
10932# ifdef FEAT_WINDOWS
10933 win_T *wp;
10934# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010935 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010936
10937 if (row >= 0 && col >= 0)
10938 {
10939 /* Find the window at the mouse coordinates and compute the
10940 * text position. */
10941 win = mouse_find_win(&row, &col);
10942 (void)mouse_comp_pos(win, &row, &col, &lnum);
10943# ifdef FEAT_WINDOWS
10944 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010945 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010946# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010947 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010948 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10949 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10950 }
10951 }
10952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 }
10954}
10955
10956/*
10957 * "getcharmod()" function
10958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010960f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010961 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965}
10966
10967/*
10968 * "getcmdline()" function
10969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010971f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010972 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975 rettv->v_type = VAR_STRING;
10976 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977}
10978
10979/*
10980 * "getcmdpos()" function
10981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010983f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010984 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988}
10989
10990/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010991 * "getcmdtype()" function
10992 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010993 static void
10994f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010995 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010996 typval_T *rettv;
10997{
10998 rettv->v_type = VAR_STRING;
10999 rettv->vval.v_string = alloc(2);
11000 if (rettv->vval.v_string != NULL)
11001 {
11002 rettv->vval.v_string[0] = get_cmdline_type();
11003 rettv->vval.v_string[1] = NUL;
11004 }
11005}
11006
11007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 * "getcwd()" function
11009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011011f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011012 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014{
11015 char_u cwd[MAXPATHL];
11016
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011019 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 else
11021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011024 if (rettv->vval.v_string != NULL)
11025 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026#endif
11027 }
11028}
11029
11030/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011031 * "getfontname()" function
11032 */
11033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011034f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011035 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011036 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011037{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011038 rettv->v_type = VAR_STRING;
11039 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011040#ifdef FEAT_GUI
11041 if (gui.in_use)
11042 {
11043 GuiFont font;
11044 char_u *name = NULL;
11045
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011046 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011047 {
11048 /* Get the "Normal" font. Either the name saved by
11049 * hl_set_font_name() or from the font ID. */
11050 font = gui.norm_font;
11051 name = hl_get_font_name();
11052 }
11053 else
11054 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011055 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011056 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11057 return;
11058 font = gui_mch_get_font(name, FALSE);
11059 if (font == NOFONT)
11060 return; /* Invalid font name, return empty string. */
11061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011063 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011064 gui_mch_free_font(font);
11065 }
11066#endif
11067}
11068
11069/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011070 * "getfperm({fname})" function
11071 */
11072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011074 typval_T *argvars;
11075 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011076{
11077 char_u *fname;
11078 struct stat st;
11079 char_u *perm = NULL;
11080 char_u flags[] = "rwx";
11081 int i;
11082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011086 if (mch_stat((char *)fname, &st) >= 0)
11087 {
11088 perm = vim_strsave((char_u *)"---------");
11089 if (perm != NULL)
11090 {
11091 for (i = 0; i < 9; i++)
11092 {
11093 if (st.st_mode & (1 << (8 - i)))
11094 perm[i] = flags[i % 3];
11095 }
11096 }
11097 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011099}
11100
11101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 * "getfsize({fname})" function
11103 */
11104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011105f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011106 typval_T *argvars;
11107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108{
11109 char_u *fname;
11110 struct stat st;
11111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115
11116 if (mch_stat((char *)fname, &st) >= 0)
11117 {
11118 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011123
11124 /* non-perfect check for overflow */
11125 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11126 rettv->vval.v_number = -2;
11127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 }
11129 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011130 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131}
11132
11133/*
11134 * "getftime({fname})" function
11135 */
11136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011138 typval_T *argvars;
11139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140{
11141 char_u *fname;
11142 struct stat st;
11143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145
11146 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150}
11151
11152/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011153 * "getftype({fname})" function
11154 */
11155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011156f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011157 typval_T *argvars;
11158 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011159{
11160 char_u *fname;
11161 struct stat st;
11162 char_u *type = NULL;
11163 char *t;
11164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011168 if (mch_lstat((char *)fname, &st) >= 0)
11169 {
11170#ifdef S_ISREG
11171 if (S_ISREG(st.st_mode))
11172 t = "file";
11173 else if (S_ISDIR(st.st_mode))
11174 t = "dir";
11175# ifdef S_ISLNK
11176 else if (S_ISLNK(st.st_mode))
11177 t = "link";
11178# endif
11179# ifdef S_ISBLK
11180 else if (S_ISBLK(st.st_mode))
11181 t = "bdev";
11182# endif
11183# ifdef S_ISCHR
11184 else if (S_ISCHR(st.st_mode))
11185 t = "cdev";
11186# endif
11187# ifdef S_ISFIFO
11188 else if (S_ISFIFO(st.st_mode))
11189 t = "fifo";
11190# endif
11191# ifdef S_ISSOCK
11192 else if (S_ISSOCK(st.st_mode))
11193 t = "fifo";
11194# endif
11195 else
11196 t = "other";
11197#else
11198# ifdef S_IFMT
11199 switch (st.st_mode & S_IFMT)
11200 {
11201 case S_IFREG: t = "file"; break;
11202 case S_IFDIR: t = "dir"; break;
11203# ifdef S_IFLNK
11204 case S_IFLNK: t = "link"; break;
11205# endif
11206# ifdef S_IFBLK
11207 case S_IFBLK: t = "bdev"; break;
11208# endif
11209# ifdef S_IFCHR
11210 case S_IFCHR: t = "cdev"; break;
11211# endif
11212# ifdef S_IFIFO
11213 case S_IFIFO: t = "fifo"; break;
11214# endif
11215# ifdef S_IFSOCK
11216 case S_IFSOCK: t = "socket"; break;
11217# endif
11218 default: t = "other";
11219 }
11220# else
11221 if (mch_isdir(fname))
11222 t = "dir";
11223 else
11224 t = "file";
11225# endif
11226#endif
11227 type = vim_strsave((char_u *)t);
11228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011230}
11231
11232/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011233 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011234 */
11235 static void
11236f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011237 typval_T *argvars;
11238 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011239{
11240 linenr_T lnum;
11241 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011242 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011243
11244 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011245 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011246 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011247 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011248 retlist = FALSE;
11249 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011250 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011251 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011252 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011253 retlist = TRUE;
11254 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011255
Bram Moolenaar342337a2005-07-21 21:11:17 +000011256 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011257}
11258
11259/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011260 * "getmatches()" function
11261 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011262 static void
11263f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011264 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011265 typval_T *rettv;
11266{
11267#ifdef FEAT_SEARCH_EXTRA
11268 dict_T *dict;
11269 matchitem_T *cur = curwin->w_match_head;
11270
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011271 if (rettv_list_alloc(rettv) == OK)
11272 {
11273 while (cur != NULL)
11274 {
11275 dict = dict_alloc();
11276 if (dict == NULL)
11277 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011278 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11279 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11280 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11281 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11282 list_append_dict(rettv->vval.v_list, dict);
11283 cur = cur->next;
11284 }
11285 }
11286#endif
11287}
11288
11289/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011290 * "getpid()" function
11291 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011292 static void
11293f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011294 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011295 typval_T *rettv;
11296{
11297 rettv->vval.v_number = mch_get_pid();
11298}
11299
11300/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011301 * "getpos(string)" function
11302 */
11303 static void
11304f_getpos(argvars, rettv)
11305 typval_T *argvars;
11306 typval_T *rettv;
11307{
11308 pos_T *fp;
11309 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011310 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011311
11312 if (rettv_list_alloc(rettv) == OK)
11313 {
11314 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011315 fp = var2fpos(&argvars[0], TRUE, &fnum);
11316 if (fnum != -1)
11317 list_append_number(l, (varnumber_T)fnum);
11318 else
11319 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011320 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11321 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011322 list_append_number(l, (fp != NULL)
11323 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011324 : (varnumber_T)0);
11325 list_append_number(l,
11326#ifdef FEAT_VIRTUALEDIT
11327 (fp != NULL) ? (varnumber_T)fp->coladd :
11328#endif
11329 (varnumber_T)0);
11330 }
11331 else
11332 rettv->vval.v_number = FALSE;
11333}
11334
11335/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011336 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011337 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011338 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011339f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011340 typval_T *argvars UNUSED;
11341 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011342{
11343#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011344 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011345#endif
11346
Bram Moolenaar2641f772005-03-25 21:58:17 +000011347#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011348 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011349 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011350 wp = NULL;
11351 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11352 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011353 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011354 if (wp == NULL)
11355 return;
11356 }
11357
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011358 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011359 }
11360#endif
11361}
11362
11363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 * "getreg()" function
11365 */
11366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011368 typval_T *argvars;
11369 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370{
11371 char_u *strregname;
11372 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011373 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011374 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011376 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011378 strregname = get_tv_string_chk(&argvars[0]);
11379 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011380 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011384 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385 regname = (strregname == NULL ? '"' : *strregname);
11386 if (regname == 0)
11387 regname = '"';
11388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011390 rettv->vval.v_string = error ? NULL :
11391 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392}
11393
11394/*
11395 * "getregtype()" function
11396 */
11397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011399 typval_T *argvars;
11400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401{
11402 char_u *strregname;
11403 int regname;
11404 char_u buf[NUMBUFLEN + 2];
11405 long reglen = 0;
11406
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011407 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011408 {
11409 strregname = get_tv_string_chk(&argvars[0]);
11410 if (strregname == NULL) /* type error; errmsg already given */
11411 {
11412 rettv->v_type = VAR_STRING;
11413 rettv->vval.v_string = NULL;
11414 return;
11415 }
11416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 else
11418 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011419 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420
11421 regname = (strregname == NULL ? '"' : *strregname);
11422 if (regname == 0)
11423 regname = '"';
11424
11425 buf[0] = NUL;
11426 buf[1] = NUL;
11427 switch (get_reg_type(regname, &reglen))
11428 {
11429 case MLINE: buf[0] = 'V'; break;
11430 case MCHAR: buf[0] = 'v'; break;
11431#ifdef FEAT_VISUAL
11432 case MBLOCK:
11433 buf[0] = Ctrl_V;
11434 sprintf((char *)buf + 1, "%ld", reglen + 1);
11435 break;
11436#endif
11437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011438 rettv->v_type = VAR_STRING;
11439 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440}
11441
11442/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011443 * "gettabvar()" function
11444 */
11445 static void
11446f_gettabvar(argvars, rettv)
11447 typval_T *argvars;
11448 typval_T *rettv;
11449{
11450 tabpage_T *tp;
11451 dictitem_T *v;
11452 char_u *varname;
11453
11454 rettv->v_type = VAR_STRING;
11455 rettv->vval.v_string = NULL;
11456
11457 varname = get_tv_string_chk(&argvars[1]);
11458 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11459 if (tp != NULL && varname != NULL)
11460 {
11461 /* look up the variable */
11462 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11463 if (v != NULL)
11464 copy_tv(&v->di_tv, rettv);
11465 }
11466}
11467
11468/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011469 * "gettabwinvar()" function
11470 */
11471 static void
11472f_gettabwinvar(argvars, rettv)
11473 typval_T *argvars;
11474 typval_T *rettv;
11475{
11476 getwinvar(argvars, rettv, 1);
11477}
11478
11479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 * "getwinposx()" function
11481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011484 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488#ifdef FEAT_GUI
11489 if (gui.in_use)
11490 {
11491 int x, y;
11492
11493 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 }
11496#endif
11497}
11498
11499/*
11500 * "getwinposy()" function
11501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011504 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011507 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508#ifdef FEAT_GUI
11509 if (gui.in_use)
11510 {
11511 int x, y;
11512
11513 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 }
11516#endif
11517}
11518
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011519/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011520 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011521 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011522 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011523find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011524 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011525 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011526{
11527#ifdef FEAT_WINDOWS
11528 win_T *wp;
11529#endif
11530 int nr;
11531
11532 nr = get_tv_number_chk(vp, NULL);
11533
11534#ifdef FEAT_WINDOWS
11535 if (nr < 0)
11536 return NULL;
11537 if (nr == 0)
11538 return curwin;
11539
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011540 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11541 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011542 if (--nr <= 0)
11543 break;
11544 return wp;
11545#else
11546 if (nr == 0 || nr == 1)
11547 return curwin;
11548 return NULL;
11549#endif
11550}
11551
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552/*
11553 * "getwinvar()" function
11554 */
11555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011557 typval_T *argvars;
11558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011560 getwinvar(argvars, rettv, 0);
11561}
11562
11563/*
11564 * getwinvar() and gettabwinvar()
11565 */
11566 static void
11567getwinvar(argvars, rettv, off)
11568 typval_T *argvars;
11569 typval_T *rettv;
11570 int off; /* 1 for gettabwinvar() */
11571{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572 win_T *win, *oldcurwin;
11573 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011574 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011575 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011577#ifdef FEAT_WINDOWS
11578 if (off == 1)
11579 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11580 else
11581 tp = curtab;
11582#endif
11583 win = find_win_by_nr(&argvars[off], tp);
11584 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011585 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 rettv->v_type = VAR_STRING;
11588 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589
11590 if (win != NULL && varname != NULL)
11591 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011592 /* Set curwin to be our win, temporarily. Also set curbuf, so
11593 * that we can get buffer-local options. */
11594 oldcurwin = curwin;
11595 curwin = win;
11596 curbuf = win->w_buffer;
11597
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011599 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 else
11601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011602 if (*varname == NUL)
11603 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11604 * scope prefix before the NUL byte is required by
11605 * find_var_in_ht(). */
11606 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011608 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011610 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011612
11613 /* restore previous notion of curwin */
11614 curwin = oldcurwin;
11615 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 }
11617
11618 --emsg_off;
11619}
11620
11621/*
11622 * "glob()" function
11623 */
11624 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011625f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011626 typval_T *argvars;
11627 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011629 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011631 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011633 /* When the optional second argument is non-zero, don't remove matches
11634 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11635 if (argvars[1].v_type != VAR_UNKNOWN
11636 && get_tv_number_chk(&argvars[1], &error))
11637 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011638 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011639 if (!error)
11640 {
11641 ExpandInit(&xpc);
11642 xpc.xp_context = EXPAND_FILES;
11643 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11644 NULL, flags, WILD_ALL);
11645 }
11646 else
11647 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648}
11649
11650/*
11651 * "globpath()" function
11652 */
11653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011654f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011655 typval_T *argvars;
11656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011658 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011660 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011661 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011663 /* When the optional second argument is non-zero, don't remove matches
11664 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11665 if (argvars[2].v_type != VAR_UNKNOWN
11666 && get_tv_number_chk(&argvars[2], &error))
11667 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011669 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011670 rettv->vval.v_string = NULL;
11671 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011672 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11673 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674}
11675
11676/*
11677 * "has()" function
11678 */
11679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011680f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011681 typval_T *argvars;
11682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683{
11684 int i;
11685 char_u *name;
11686 int n = FALSE;
11687 static char *(has_list[]) =
11688 {
11689#ifdef AMIGA
11690 "amiga",
11691# ifdef FEAT_ARP
11692 "arp",
11693# endif
11694#endif
11695#ifdef __BEOS__
11696 "beos",
11697#endif
11698#ifdef MSDOS
11699# ifdef DJGPP
11700 "dos32",
11701# else
11702 "dos16",
11703# endif
11704#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011705#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 "mac",
11707#endif
11708#if defined(MACOS_X_UNIX)
11709 "macunix",
11710#endif
11711#ifdef OS2
11712 "os2",
11713#endif
11714#ifdef __QNX__
11715 "qnx",
11716#endif
11717#ifdef RISCOS
11718 "riscos",
11719#endif
11720#ifdef UNIX
11721 "unix",
11722#endif
11723#ifdef VMS
11724 "vms",
11725#endif
11726#ifdef WIN16
11727 "win16",
11728#endif
11729#ifdef WIN32
11730 "win32",
11731#endif
11732#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11733 "win32unix",
11734#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011735#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 "win64",
11737#endif
11738#ifdef EBCDIC
11739 "ebcdic",
11740#endif
11741#ifndef CASE_INSENSITIVE_FILENAME
11742 "fname_case",
11743#endif
11744#ifdef FEAT_ARABIC
11745 "arabic",
11746#endif
11747#ifdef FEAT_AUTOCMD
11748 "autocmd",
11749#endif
11750#ifdef FEAT_BEVAL
11751 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011752# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11753 "balloon_multiline",
11754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755#endif
11756#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11757 "builtin_terms",
11758# ifdef ALL_BUILTIN_TCAPS
11759 "all_builtin_terms",
11760# endif
11761#endif
11762#ifdef FEAT_BYTEOFF
11763 "byte_offset",
11764#endif
11765#ifdef FEAT_CINDENT
11766 "cindent",
11767#endif
11768#ifdef FEAT_CLIENTSERVER
11769 "clientserver",
11770#endif
11771#ifdef FEAT_CLIPBOARD
11772 "clipboard",
11773#endif
11774#ifdef FEAT_CMDL_COMPL
11775 "cmdline_compl",
11776#endif
11777#ifdef FEAT_CMDHIST
11778 "cmdline_hist",
11779#endif
11780#ifdef FEAT_COMMENTS
11781 "comments",
11782#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011783#ifdef FEAT_CONCEAL
11784 "conceal",
11785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786#ifdef FEAT_CRYPT
11787 "cryptv",
11788#endif
11789#ifdef FEAT_CSCOPE
11790 "cscope",
11791#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011792#ifdef FEAT_CURSORBIND
11793 "cursorbind",
11794#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011795#ifdef CURSOR_SHAPE
11796 "cursorshape",
11797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798#ifdef DEBUG
11799 "debug",
11800#endif
11801#ifdef FEAT_CON_DIALOG
11802 "dialog_con",
11803#endif
11804#ifdef FEAT_GUI_DIALOG
11805 "dialog_gui",
11806#endif
11807#ifdef FEAT_DIFF
11808 "diff",
11809#endif
11810#ifdef FEAT_DIGRAPHS
11811 "digraphs",
11812#endif
11813#ifdef FEAT_DND
11814 "dnd",
11815#endif
11816#ifdef FEAT_EMACS_TAGS
11817 "emacs_tags",
11818#endif
11819 "eval", /* always present, of course! */
11820#ifdef FEAT_EX_EXTRA
11821 "ex_extra",
11822#endif
11823#ifdef FEAT_SEARCH_EXTRA
11824 "extra_search",
11825#endif
11826#ifdef FEAT_FKMAP
11827 "farsi",
11828#endif
11829#ifdef FEAT_SEARCHPATH
11830 "file_in_path",
11831#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011832#if defined(UNIX) && !defined(USE_SYSTEM)
11833 "filterpipe",
11834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835#ifdef FEAT_FIND_ID
11836 "find_in_path",
11837#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011838#ifdef FEAT_FLOAT
11839 "float",
11840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841#ifdef FEAT_FOLDING
11842 "folding",
11843#endif
11844#ifdef FEAT_FOOTER
11845 "footer",
11846#endif
11847#if !defined(USE_SYSTEM) && defined(UNIX)
11848 "fork",
11849#endif
11850#ifdef FEAT_GETTEXT
11851 "gettext",
11852#endif
11853#ifdef FEAT_GUI
11854 "gui",
11855#endif
11856#ifdef FEAT_GUI_ATHENA
11857# ifdef FEAT_GUI_NEXTAW
11858 "gui_neXtaw",
11859# else
11860 "gui_athena",
11861# endif
11862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863#ifdef FEAT_GUI_GTK
11864 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011867#ifdef FEAT_GUI_GNOME
11868 "gui_gnome",
11869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870#ifdef FEAT_GUI_MAC
11871 "gui_mac",
11872#endif
11873#ifdef FEAT_GUI_MOTIF
11874 "gui_motif",
11875#endif
11876#ifdef FEAT_GUI_PHOTON
11877 "gui_photon",
11878#endif
11879#ifdef FEAT_GUI_W16
11880 "gui_win16",
11881#endif
11882#ifdef FEAT_GUI_W32
11883 "gui_win32",
11884#endif
11885#ifdef FEAT_HANGULIN
11886 "hangul_input",
11887#endif
11888#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11889 "iconv",
11890#endif
11891#ifdef FEAT_INS_EXPAND
11892 "insert_expand",
11893#endif
11894#ifdef FEAT_JUMPLIST
11895 "jumplist",
11896#endif
11897#ifdef FEAT_KEYMAP
11898 "keymap",
11899#endif
11900#ifdef FEAT_LANGMAP
11901 "langmap",
11902#endif
11903#ifdef FEAT_LIBCALL
11904 "libcall",
11905#endif
11906#ifdef FEAT_LINEBREAK
11907 "linebreak",
11908#endif
11909#ifdef FEAT_LISP
11910 "lispindent",
11911#endif
11912#ifdef FEAT_LISTCMDS
11913 "listcmds",
11914#endif
11915#ifdef FEAT_LOCALMAP
11916 "localmap",
11917#endif
11918#ifdef FEAT_MENU
11919 "menu",
11920#endif
11921#ifdef FEAT_SESSION
11922 "mksession",
11923#endif
11924#ifdef FEAT_MODIFY_FNAME
11925 "modify_fname",
11926#endif
11927#ifdef FEAT_MOUSE
11928 "mouse",
11929#endif
11930#ifdef FEAT_MOUSESHAPE
11931 "mouseshape",
11932#endif
11933#if defined(UNIX) || defined(VMS)
11934# ifdef FEAT_MOUSE_DEC
11935 "mouse_dec",
11936# endif
11937# ifdef FEAT_MOUSE_GPM
11938 "mouse_gpm",
11939# endif
11940# ifdef FEAT_MOUSE_JSB
11941 "mouse_jsbterm",
11942# endif
11943# ifdef FEAT_MOUSE_NET
11944 "mouse_netterm",
11945# endif
11946# ifdef FEAT_MOUSE_PTERM
11947 "mouse_pterm",
11948# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011949# ifdef FEAT_SYSMOUSE
11950 "mouse_sysmouse",
11951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952# ifdef FEAT_MOUSE_XTERM
11953 "mouse_xterm",
11954# endif
11955#endif
11956#ifdef FEAT_MBYTE
11957 "multi_byte",
11958#endif
11959#ifdef FEAT_MBYTE_IME
11960 "multi_byte_ime",
11961#endif
11962#ifdef FEAT_MULTI_LANG
11963 "multi_lang",
11964#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011965#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011966#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011967 "mzscheme",
11968#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970#ifdef FEAT_OLE
11971 "ole",
11972#endif
11973#ifdef FEAT_OSFILETYPE
11974 "osfiletype",
11975#endif
11976#ifdef FEAT_PATH_EXTRA
11977 "path_extra",
11978#endif
11979#ifdef FEAT_PERL
11980#ifndef DYNAMIC_PERL
11981 "perl",
11982#endif
11983#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011984#ifdef FEAT_PERSISTENT_UNDO
11985 "persistent_undo",
11986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987#ifdef FEAT_PYTHON
11988#ifndef DYNAMIC_PYTHON
11989 "python",
11990#endif
11991#endif
11992#ifdef FEAT_POSTSCRIPT
11993 "postscript",
11994#endif
11995#ifdef FEAT_PRINTER
11996 "printer",
11997#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011998#ifdef FEAT_PROFILE
11999 "profile",
12000#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012001#ifdef FEAT_RELTIME
12002 "reltime",
12003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004#ifdef FEAT_QUICKFIX
12005 "quickfix",
12006#endif
12007#ifdef FEAT_RIGHTLEFT
12008 "rightleft",
12009#endif
12010#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12011 "ruby",
12012#endif
12013#ifdef FEAT_SCROLLBIND
12014 "scrollbind",
12015#endif
12016#ifdef FEAT_CMDL_INFO
12017 "showcmd",
12018 "cmdline_info",
12019#endif
12020#ifdef FEAT_SIGNS
12021 "signs",
12022#endif
12023#ifdef FEAT_SMARTINDENT
12024 "smartindent",
12025#endif
12026#ifdef FEAT_SNIFF
12027 "sniff",
12028#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012029#ifdef STARTUPTIME
12030 "startuptime",
12031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032#ifdef FEAT_STL_OPT
12033 "statusline",
12034#endif
12035#ifdef FEAT_SUN_WORKSHOP
12036 "sun_workshop",
12037#endif
12038#ifdef FEAT_NETBEANS_INTG
12039 "netbeans_intg",
12040#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012041#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012042 "spell",
12043#endif
12044#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 "syntax",
12046#endif
12047#if defined(USE_SYSTEM) || !defined(UNIX)
12048 "system",
12049#endif
12050#ifdef FEAT_TAG_BINS
12051 "tag_binary",
12052#endif
12053#ifdef FEAT_TAG_OLDSTATIC
12054 "tag_old_static",
12055#endif
12056#ifdef FEAT_TAG_ANYWHITE
12057 "tag_any_white",
12058#endif
12059#ifdef FEAT_TCL
12060# ifndef DYNAMIC_TCL
12061 "tcl",
12062# endif
12063#endif
12064#ifdef TERMINFO
12065 "terminfo",
12066#endif
12067#ifdef FEAT_TERMRESPONSE
12068 "termresponse",
12069#endif
12070#ifdef FEAT_TEXTOBJ
12071 "textobjects",
12072#endif
12073#ifdef HAVE_TGETENT
12074 "tgetent",
12075#endif
12076#ifdef FEAT_TITLE
12077 "title",
12078#endif
12079#ifdef FEAT_TOOLBAR
12080 "toolbar",
12081#endif
12082#ifdef FEAT_USR_CMDS
12083 "user-commands", /* was accidentally included in 5.4 */
12084 "user_commands",
12085#endif
12086#ifdef FEAT_VIMINFO
12087 "viminfo",
12088#endif
12089#ifdef FEAT_VERTSPLIT
12090 "vertsplit",
12091#endif
12092#ifdef FEAT_VIRTUALEDIT
12093 "virtualedit",
12094#endif
12095#ifdef FEAT_VISUAL
12096 "visual",
12097#endif
12098#ifdef FEAT_VISUALEXTRA
12099 "visualextra",
12100#endif
12101#ifdef FEAT_VREPLACE
12102 "vreplace",
12103#endif
12104#ifdef FEAT_WILDIGN
12105 "wildignore",
12106#endif
12107#ifdef FEAT_WILDMENU
12108 "wildmenu",
12109#endif
12110#ifdef FEAT_WINDOWS
12111 "windows",
12112#endif
12113#ifdef FEAT_WAK
12114 "winaltkeys",
12115#endif
12116#ifdef FEAT_WRITEBACKUP
12117 "writebackup",
12118#endif
12119#ifdef FEAT_XIM
12120 "xim",
12121#endif
12122#ifdef FEAT_XFONTSET
12123 "xfontset",
12124#endif
12125#ifdef USE_XSMP
12126 "xsmp",
12127#endif
12128#ifdef USE_XSMP_INTERACT
12129 "xsmp_interact",
12130#endif
12131#ifdef FEAT_XCLIPBOARD
12132 "xterm_clipboard",
12133#endif
12134#ifdef FEAT_XTERM_SAVE
12135 "xterm_save",
12136#endif
12137#if defined(UNIX) && defined(FEAT_X11)
12138 "X11",
12139#endif
12140 NULL
12141 };
12142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144 for (i = 0; has_list[i] != NULL; ++i)
12145 if (STRICMP(name, has_list[i]) == 0)
12146 {
12147 n = TRUE;
12148 break;
12149 }
12150
12151 if (n == FALSE)
12152 {
12153 if (STRNICMP(name, "patch", 5) == 0)
12154 n = has_patch(atoi((char *)name + 5));
12155 else if (STRICMP(name, "vim_starting") == 0)
12156 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012157#ifdef FEAT_MBYTE
12158 else if (STRICMP(name, "multi_byte_encoding") == 0)
12159 n = has_mbyte;
12160#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012161#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12162 else if (STRICMP(name, "balloon_multiline") == 0)
12163 n = multiline_balloon_available();
12164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165#ifdef DYNAMIC_TCL
12166 else if (STRICMP(name, "tcl") == 0)
12167 n = tcl_enabled(FALSE);
12168#endif
12169#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12170 else if (STRICMP(name, "iconv") == 0)
12171 n = iconv_enabled(FALSE);
12172#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012173#ifdef DYNAMIC_MZSCHEME
12174 else if (STRICMP(name, "mzscheme") == 0)
12175 n = mzscheme_enabled(FALSE);
12176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177#ifdef DYNAMIC_RUBY
12178 else if (STRICMP(name, "ruby") == 0)
12179 n = ruby_enabled(FALSE);
12180#endif
12181#ifdef DYNAMIC_PYTHON
12182 else if (STRICMP(name, "python") == 0)
12183 n = python_enabled(FALSE);
12184#endif
12185#ifdef DYNAMIC_PERL
12186 else if (STRICMP(name, "perl") == 0)
12187 n = perl_enabled(FALSE);
12188#endif
12189#ifdef FEAT_GUI
12190 else if (STRICMP(name, "gui_running") == 0)
12191 n = (gui.in_use || gui.starting);
12192# ifdef FEAT_GUI_W32
12193 else if (STRICMP(name, "gui_win32s") == 0)
12194 n = gui_is_win32s();
12195# endif
12196# ifdef FEAT_BROWSE
12197 else if (STRICMP(name, "browse") == 0)
12198 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12199# endif
12200#endif
12201#ifdef FEAT_SYN_HL
12202 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012203 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204#endif
12205#if defined(WIN3264)
12206 else if (STRICMP(name, "win95") == 0)
12207 n = mch_windows95();
12208#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012209#ifdef FEAT_NETBEANS_INTG
12210 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012211 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 }
12214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012215 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216}
12217
12218/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012219 * "has_key()" function
12220 */
12221 static void
12222f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012223 typval_T *argvars;
12224 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012225{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012226 if (argvars[0].v_type != VAR_DICT)
12227 {
12228 EMSG(_(e_dictreq));
12229 return;
12230 }
12231 if (argvars[0].vval.v_dict == NULL)
12232 return;
12233
12234 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012235 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012236}
12237
12238/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012239 * "haslocaldir()" function
12240 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012241 static void
12242f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012243 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012244 typval_T *rettv;
12245{
12246 rettv->vval.v_number = (curwin->w_localdir != NULL);
12247}
12248
12249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250 * "hasmapto()" function
12251 */
12252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012253f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012254 typval_T *argvars;
12255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256{
12257 char_u *name;
12258 char_u *mode;
12259 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012260 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012262 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012263 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 mode = (char_u *)"nvo";
12265 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012268 if (argvars[2].v_type != VAR_UNKNOWN)
12269 abbr = get_tv_number(&argvars[2]);
12270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271
Bram Moolenaar2c932302006-03-18 21:42:09 +000012272 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012273 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012275 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276}
12277
12278/*
12279 * "histadd()" function
12280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012282f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012283 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285{
12286#ifdef FEAT_CMDHIST
12287 int histype;
12288 char_u *str;
12289 char_u buf[NUMBUFLEN];
12290#endif
12291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012292 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 if (check_restricted() || check_secure())
12294 return;
12295#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012296 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12297 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298 if (histype >= 0)
12299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012300 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 if (*str != NUL)
12302 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012303 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012305 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306 return;
12307 }
12308 }
12309#endif
12310}
12311
12312/*
12313 * "histdel()" function
12314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012316f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012317 typval_T *argvars UNUSED;
12318 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319{
12320#ifdef FEAT_CMDHIST
12321 int n;
12322 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012323 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012325 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12326 if (str == NULL)
12327 n = 0;
12328 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012330 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012331 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012333 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012334 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335 else
12336 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012337 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 get_tv_string_buf(&argvars[1], buf));
12339 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340#endif
12341}
12342
12343/*
12344 * "histget()" function
12345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012348 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350{
12351#ifdef FEAT_CMDHIST
12352 int type;
12353 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012354 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012356 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12357 if (str == NULL)
12358 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012360 {
12361 type = get_histtype(str);
12362 if (argvars[1].v_type == VAR_UNKNOWN)
12363 idx = get_history_idx(type);
12364 else
12365 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12366 /* -1 on type error */
12367 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012370 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012372 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373}
12374
12375/*
12376 * "histnr()" function
12377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012379f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012380 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382{
12383 int i;
12384
12385#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012386 char_u *history = get_tv_string_chk(&argvars[0]);
12387
12388 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 if (i >= HIST_CMD && i < HIST_COUNT)
12390 i = get_history_idx(i);
12391 else
12392#endif
12393 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012394 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395}
12396
12397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 * "highlightID(name)" function
12399 */
12400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012401f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012402 typval_T *argvars;
12403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012405 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406}
12407
12408/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012409 * "highlight_exists()" function
12410 */
12411 static void
12412f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012413 typval_T *argvars;
12414 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012415{
12416 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12417}
12418
12419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420 * "hostname()" function
12421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012423f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012424 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426{
12427 char_u hostname[256];
12428
12429 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430 rettv->v_type = VAR_STRING;
12431 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432}
12433
12434/*
12435 * iconv() function
12436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012438f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012439 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441{
12442#ifdef FEAT_MBYTE
12443 char_u buf1[NUMBUFLEN];
12444 char_u buf2[NUMBUFLEN];
12445 char_u *from, *to, *str;
12446 vimconv_T vimconv;
12447#endif
12448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012449 rettv->v_type = VAR_STRING;
12450 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451
12452#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012453 str = get_tv_string(&argvars[0]);
12454 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12455 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456 vimconv.vc_type = CONV_NONE;
12457 convert_setup(&vimconv, from, to);
12458
12459 /* If the encodings are equal, no conversion needed. */
12460 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012461 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012462 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012463 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464
12465 convert_setup(&vimconv, NULL, NULL);
12466 vim_free(from);
12467 vim_free(to);
12468#endif
12469}
12470
12471/*
12472 * "indent()" function
12473 */
12474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012476 typval_T *argvars;
12477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478{
12479 linenr_T lnum;
12480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012483 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012485 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486}
12487
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012488/*
12489 * "index()" function
12490 */
12491 static void
12492f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012493 typval_T *argvars;
12494 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012495{
Bram Moolenaar33570922005-01-25 22:26:29 +000012496 list_T *l;
12497 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012498 long idx = 0;
12499 int ic = FALSE;
12500
12501 rettv->vval.v_number = -1;
12502 if (argvars[0].v_type != VAR_LIST)
12503 {
12504 EMSG(_(e_listreq));
12505 return;
12506 }
12507 l = argvars[0].vval.v_list;
12508 if (l != NULL)
12509 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012510 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012511 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012513 int error = FALSE;
12514
Bram Moolenaar758711c2005-02-02 23:11:38 +000012515 /* Start at specified item. Use the cached index that list_find()
12516 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012518 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012519 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012520 ic = get_tv_number_chk(&argvars[3], &error);
12521 if (error)
12522 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012523 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012524
Bram Moolenaar758711c2005-02-02 23:11:38 +000012525 for ( ; item != NULL; item = item->li_next, ++idx)
12526 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012527 {
12528 rettv->vval.v_number = idx;
12529 break;
12530 }
12531 }
12532}
12533
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534static int inputsecret_flag = 0;
12535
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012536static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12537
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012539 * This function is used by f_input() and f_inputdialog() functions. The third
12540 * argument to f_input() specifies the type of completion to use at the
12541 * prompt. The third argument to f_inputdialog() specifies the value to return
12542 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543 */
12544 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012545get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012546 typval_T *argvars;
12547 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012548 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012550 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551 char_u *p = NULL;
12552 int c;
12553 char_u buf[NUMBUFLEN];
12554 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012555 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012556 int xp_type = EXPAND_NOTHING;
12557 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012559 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012560 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561
12562#ifdef NO_CONSOLE_INPUT
12563 /* While starting up, there is no place to enter text. */
12564 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566#endif
12567
12568 cmd_silent = FALSE; /* Want to see the prompt. */
12569 if (prompt != NULL)
12570 {
12571 /* Only the part of the message after the last NL is considered as
12572 * prompt for the command line */
12573 p = vim_strrchr(prompt, '\n');
12574 if (p == NULL)
12575 p = prompt;
12576 else
12577 {
12578 ++p;
12579 c = *p;
12580 *p = NUL;
12581 msg_start();
12582 msg_clr_eos();
12583 msg_puts_attr(prompt, echo_attr);
12584 msg_didout = FALSE;
12585 msg_starthere();
12586 *p = c;
12587 }
12588 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012590 if (argvars[1].v_type != VAR_UNKNOWN)
12591 {
12592 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12593 if (defstr != NULL)
12594 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012596 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012597 {
12598 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012599 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012600 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012601
Bram Moolenaar4463f292005-09-25 22:20:24 +000012602 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012603
Bram Moolenaar4463f292005-09-25 22:20:24 +000012604 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12605 if (xp_name == NULL)
12606 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012607
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012608 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012609
Bram Moolenaar4463f292005-09-25 22:20:24 +000012610 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12611 &xp_arg) == FAIL)
12612 return;
12613 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012614 }
12615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012616 if (defstr != NULL)
12617 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012618 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12619 xp_type, xp_arg);
12620
12621 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012623 /* since the user typed this, no need to wait for return */
12624 need_wait_return = FALSE;
12625 msg_didout = FALSE;
12626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627 cmd_silent = cmd_silent_save;
12628}
12629
12630/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012631 * "input()" function
12632 * Also handles inputsecret() when inputsecret is set.
12633 */
12634 static void
12635f_input(argvars, rettv)
12636 typval_T *argvars;
12637 typval_T *rettv;
12638{
12639 get_user_input(argvars, rettv, FALSE);
12640}
12641
12642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643 * "inputdialog()" function
12644 */
12645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012646f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012647 typval_T *argvars;
12648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649{
12650#if defined(FEAT_GUI_TEXTDIALOG)
12651 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12652 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12653 {
12654 char_u *message;
12655 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012656 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012658 message = get_tv_string_chk(&argvars[0]);
12659 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012660 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012661 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 else
12663 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012664 if (message != NULL && defstr != NULL
12665 && do_dialog(VIM_QUESTION, NULL, message,
12666 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012667 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668 else
12669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012670 if (message != NULL && defstr != NULL
12671 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012672 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673 rettv->vval.v_string = vim_strsave(
12674 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012678 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 }
12680 else
12681#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012682 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683}
12684
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012685/*
12686 * "inputlist()" function
12687 */
12688 static void
12689f_inputlist(argvars, rettv)
12690 typval_T *argvars;
12691 typval_T *rettv;
12692{
12693 listitem_T *li;
12694 int selected;
12695 int mouse_used;
12696
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012697#ifdef NO_CONSOLE_INPUT
12698 /* While starting up, there is no place to enter text. */
12699 if (no_console_input())
12700 return;
12701#endif
12702 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12703 {
12704 EMSG2(_(e_listarg), "inputlist()");
12705 return;
12706 }
12707
12708 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012709 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012710 lines_left = Rows; /* avoid more prompt */
12711 msg_scroll = TRUE;
12712 msg_clr_eos();
12713
12714 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12715 {
12716 msg_puts(get_tv_string(&li->li_tv));
12717 msg_putchar('\n');
12718 }
12719
12720 /* Ask for choice. */
12721 selected = prompt_for_number(&mouse_used);
12722 if (mouse_used)
12723 selected -= lines_left;
12724
12725 rettv->vval.v_number = selected;
12726}
12727
12728
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12730
12731/*
12732 * "inputrestore()" function
12733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012736 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738{
12739 if (ga_userinput.ga_len > 0)
12740 {
12741 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12743 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012744 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 }
12746 else if (p_verbose > 1)
12747 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012748 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 }
12751}
12752
12753/*
12754 * "inputsave()" function
12755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012757f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012758 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012761 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762 if (ga_grow(&ga_userinput, 1) == OK)
12763 {
12764 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12765 + ga_userinput.ga_len);
12766 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012767 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 }
12769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771}
12772
12773/*
12774 * "inputsecret()" function
12775 */
12776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012778 typval_T *argvars;
12779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780{
12781 ++cmdline_star;
12782 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012783 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 --cmdline_star;
12785 --inputsecret_flag;
12786}
12787
12788/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012789 * "insert()" function
12790 */
12791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012793 typval_T *argvars;
12794 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012795{
12796 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012797 listitem_T *item;
12798 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012799 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012800
12801 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012802 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012803 else if ((l = argvars[0].vval.v_list) != NULL
12804 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012805 {
12806 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012807 before = get_tv_number_chk(&argvars[2], &error);
12808 if (error)
12809 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012810
Bram Moolenaar758711c2005-02-02 23:11:38 +000012811 if (before == l->lv_len)
12812 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012813 else
12814 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012815 item = list_find(l, before);
12816 if (item == NULL)
12817 {
12818 EMSGN(_(e_listidx), before);
12819 l = NULL;
12820 }
12821 }
12822 if (l != NULL)
12823 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012824 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012825 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012826 }
12827 }
12828}
12829
12830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 * "isdirectory()" function
12832 */
12833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012835 typval_T *argvars;
12836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012838 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839}
12840
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012841/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012842 * "islocked()" function
12843 */
12844 static void
12845f_islocked(argvars, rettv)
12846 typval_T *argvars;
12847 typval_T *rettv;
12848{
12849 lval_T lv;
12850 char_u *end;
12851 dictitem_T *di;
12852
12853 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012854 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12855 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012856 if (end != NULL && lv.ll_name != NULL)
12857 {
12858 if (*end != NUL)
12859 EMSG(_(e_trailing));
12860 else
12861 {
12862 if (lv.ll_tv == NULL)
12863 {
12864 if (check_changedtick(lv.ll_name))
12865 rettv->vval.v_number = 1; /* always locked */
12866 else
12867 {
12868 di = find_var(lv.ll_name, NULL);
12869 if (di != NULL)
12870 {
12871 /* Consider a variable locked when:
12872 * 1. the variable itself is locked
12873 * 2. the value of the variable is locked.
12874 * 3. the List or Dict value is locked.
12875 */
12876 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12877 || tv_islocked(&di->di_tv));
12878 }
12879 }
12880 }
12881 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012882 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012883 else if (lv.ll_newkey != NULL)
12884 EMSG2(_(e_dictkey), lv.ll_newkey);
12885 else if (lv.ll_list != NULL)
12886 /* List item. */
12887 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12888 else
12889 /* Dictionary item. */
12890 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12891 }
12892 }
12893
12894 clear_lval(&lv);
12895}
12896
Bram Moolenaar33570922005-01-25 22:26:29 +000012897static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012898
12899/*
12900 * Turn a dict into a list:
12901 * "what" == 0: list of keys
12902 * "what" == 1: list of values
12903 * "what" == 2: list of items
12904 */
12905 static void
12906dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012907 typval_T *argvars;
12908 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012909 int what;
12910{
Bram Moolenaar33570922005-01-25 22:26:29 +000012911 list_T *l2;
12912 dictitem_T *di;
12913 hashitem_T *hi;
12914 listitem_T *li;
12915 listitem_T *li2;
12916 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012917 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012918
Bram Moolenaar8c711452005-01-14 21:53:12 +000012919 if (argvars[0].v_type != VAR_DICT)
12920 {
12921 EMSG(_(e_dictreq));
12922 return;
12923 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012924 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012925 return;
12926
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012927 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012928 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012929
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012930 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012931 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012932 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012933 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012934 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012935 --todo;
12936 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012937
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012938 li = listitem_alloc();
12939 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012940 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012941 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012942
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012943 if (what == 0)
12944 {
12945 /* keys() */
12946 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012947 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012948 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12949 }
12950 else if (what == 1)
12951 {
12952 /* values() */
12953 copy_tv(&di->di_tv, &li->li_tv);
12954 }
12955 else
12956 {
12957 /* items() */
12958 l2 = list_alloc();
12959 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012960 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012961 li->li_tv.vval.v_list = l2;
12962 if (l2 == NULL)
12963 break;
12964 ++l2->lv_refcount;
12965
12966 li2 = listitem_alloc();
12967 if (li2 == NULL)
12968 break;
12969 list_append(l2, li2);
12970 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012971 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012972 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12973
12974 li2 = listitem_alloc();
12975 if (li2 == NULL)
12976 break;
12977 list_append(l2, li2);
12978 copy_tv(&di->di_tv, &li2->li_tv);
12979 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012980 }
12981 }
12982}
12983
12984/*
12985 * "items(dict)" function
12986 */
12987 static void
12988f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012989 typval_T *argvars;
12990 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012991{
12992 dict_list(argvars, rettv, 2);
12993}
12994
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012996 * "join()" function
12997 */
12998 static void
12999f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013000 typval_T *argvars;
13001 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013002{
13003 garray_T ga;
13004 char_u *sep;
13005
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013006 if (argvars[0].v_type != VAR_LIST)
13007 {
13008 EMSG(_(e_listreq));
13009 return;
13010 }
13011 if (argvars[0].vval.v_list == NULL)
13012 return;
13013 if (argvars[1].v_type == VAR_UNKNOWN)
13014 sep = (char_u *)" ";
13015 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013016 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013017
13018 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013019
13020 if (sep != NULL)
13021 {
13022 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013023 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013024 ga_append(&ga, NUL);
13025 rettv->vval.v_string = (char_u *)ga.ga_data;
13026 }
13027 else
13028 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013029}
13030
13031/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013032 * "keys()" function
13033 */
13034 static void
13035f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013036 typval_T *argvars;
13037 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013038{
13039 dict_list(argvars, rettv, 0);
13040}
13041
13042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 * "last_buffer_nr()" function.
13044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013046f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013047 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049{
13050 int n = 0;
13051 buf_T *buf;
13052
13053 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13054 if (n < buf->b_fnum)
13055 n = buf->b_fnum;
13056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013058}
13059
13060/*
13061 * "len()" function
13062 */
13063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013065 typval_T *argvars;
13066 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013067{
13068 switch (argvars[0].v_type)
13069 {
13070 case VAR_STRING:
13071 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013072 rettv->vval.v_number = (varnumber_T)STRLEN(
13073 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013074 break;
13075 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013076 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013077 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013078 case VAR_DICT:
13079 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13080 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013081 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013082 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013083 break;
13084 }
13085}
13086
Bram Moolenaar33570922005-01-25 22:26:29 +000013087static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013088
13089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013090libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013091 typval_T *argvars;
13092 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013093 int type;
13094{
13095#ifdef FEAT_LIBCALL
13096 char_u *string_in;
13097 char_u **string_result;
13098 int nr_result;
13099#endif
13100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013101 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013102 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013103 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013104
13105 if (check_restricted() || check_secure())
13106 return;
13107
13108#ifdef FEAT_LIBCALL
13109 /* The first two args must be strings, otherwise its meaningless */
13110 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13111 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013112 string_in = NULL;
13113 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013114 string_in = argvars[2].vval.v_string;
13115 if (type == VAR_NUMBER)
13116 string_result = NULL;
13117 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013118 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013119 if (mch_libcall(argvars[0].vval.v_string,
13120 argvars[1].vval.v_string,
13121 string_in,
13122 argvars[2].vval.v_number,
13123 string_result,
13124 &nr_result) == OK
13125 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013126 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013127 }
13128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129}
13130
13131/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013132 * "libcall()" function
13133 */
13134 static void
13135f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013136 typval_T *argvars;
13137 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013138{
13139 libcall_common(argvars, rettv, VAR_STRING);
13140}
13141
13142/*
13143 * "libcallnr()" function
13144 */
13145 static void
13146f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013147 typval_T *argvars;
13148 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013149{
13150 libcall_common(argvars, rettv, VAR_NUMBER);
13151}
13152
13153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 * "line(string)" function
13155 */
13156 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013157f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013158 typval_T *argvars;
13159 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160{
13161 linenr_T lnum = 0;
13162 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013163 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013165 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 if (fp != NULL)
13167 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169}
13170
13171/*
13172 * "line2byte(lnum)" function
13173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013175f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013176 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178{
13179#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013180 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181#else
13182 linenr_T lnum;
13183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013184 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013186 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013188 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13189 if (rettv->vval.v_number >= 0)
13190 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191#endif
13192}
13193
13194/*
13195 * "lispindent(lnum)" function
13196 */
13197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013198f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013199 typval_T *argvars;
13200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201{
13202#ifdef FEAT_LISP
13203 pos_T pos;
13204 linenr_T lnum;
13205
13206 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13209 {
13210 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 curwin->w_cursor = pos;
13213 }
13214 else
13215#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013216 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217}
13218
13219/*
13220 * "localtime()" function
13221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013223f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013224 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013227 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228}
13229
Bram Moolenaar33570922005-01-25 22:26:29 +000013230static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231
13232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013234 typval_T *argvars;
13235 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 int exact;
13237{
13238 char_u *keys;
13239 char_u *which;
13240 char_u buf[NUMBUFLEN];
13241 char_u *keys_buf = NULL;
13242 char_u *rhs;
13243 int mode;
13244 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013245 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246
13247 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013248 rettv->v_type = VAR_STRING;
13249 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013251 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252 if (*keys == NUL)
13253 return;
13254
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013255 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013257 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013258 if (argvars[2].v_type != VAR_UNKNOWN)
13259 abbr = get_tv_number(&argvars[2]);
13260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 else
13262 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013263 if (which == NULL)
13264 return;
13265
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 mode = get_map_mode(&which, 0);
13267
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013268 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013269 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 vim_free(keys_buf);
13271 if (rhs != NULL)
13272 {
13273 ga_init(&ga);
13274 ga.ga_itemsize = 1;
13275 ga.ga_growsize = 40;
13276
13277 while (*rhs != NUL)
13278 ga_concat(&ga, str2special(&rhs, FALSE));
13279
13280 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013281 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282 }
13283}
13284
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013285#ifdef FEAT_FLOAT
13286/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013287 * "log()" function
13288 */
13289 static void
13290f_log(argvars, rettv)
13291 typval_T *argvars;
13292 typval_T *rettv;
13293{
13294 float_T f;
13295
13296 rettv->v_type = VAR_FLOAT;
13297 if (get_float_arg(argvars, &f) == OK)
13298 rettv->vval.v_float = log(f);
13299 else
13300 rettv->vval.v_float = 0.0;
13301}
13302
13303/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013304 * "log10()" function
13305 */
13306 static void
13307f_log10(argvars, rettv)
13308 typval_T *argvars;
13309 typval_T *rettv;
13310{
13311 float_T f;
13312
13313 rettv->v_type = VAR_FLOAT;
13314 if (get_float_arg(argvars, &f) == OK)
13315 rettv->vval.v_float = log10(f);
13316 else
13317 rettv->vval.v_float = 0.0;
13318}
13319#endif
13320
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013322 * "map()" function
13323 */
13324 static void
13325f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013326 typval_T *argvars;
13327 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013328{
13329 filter_map(argvars, rettv, TRUE);
13330}
13331
13332/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013333 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334 */
13335 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013336f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013337 typval_T *argvars;
13338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013340 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341}
13342
13343/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013344 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345 */
13346 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013347f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013348 typval_T *argvars;
13349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013351 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352}
13353
Bram Moolenaar33570922005-01-25 22:26:29 +000013354static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013355
13356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013357find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013358 typval_T *argvars;
13359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 int type;
13361{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013362 char_u *str = NULL;
13363 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364 char_u *pat;
13365 regmatch_T regmatch;
13366 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013367 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368 char_u *save_cpo;
13369 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013370 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013371 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013372 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013373 list_T *l = NULL;
13374 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013375 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013376 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377
13378 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13379 save_cpo = p_cpo;
13380 p_cpo = (char_u *)"";
13381
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013382 rettv->vval.v_number = -1;
13383 if (type == 3)
13384 {
13385 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013386 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013387 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013388 }
13389 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013391 rettv->v_type = VAR_STRING;
13392 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013395 if (argvars[0].v_type == VAR_LIST)
13396 {
13397 if ((l = argvars[0].vval.v_list) == NULL)
13398 goto theend;
13399 li = l->lv_first;
13400 }
13401 else
13402 expr = str = get_tv_string(&argvars[0]);
13403
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013404 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13405 if (pat == NULL)
13406 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013407
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013408 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013410 int error = FALSE;
13411
13412 start = get_tv_number_chk(&argvars[2], &error);
13413 if (error)
13414 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013415 if (l != NULL)
13416 {
13417 li = list_find(l, start);
13418 if (li == NULL)
13419 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013420 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013421 }
13422 else
13423 {
13424 if (start < 0)
13425 start = 0;
13426 if (start > (long)STRLEN(str))
13427 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013428 /* When "count" argument is there ignore matches before "start",
13429 * otherwise skip part of the string. Differs when pattern is "^"
13430 * or "\<". */
13431 if (argvars[3].v_type != VAR_UNKNOWN)
13432 startcol = start;
13433 else
13434 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013435 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013436
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013437 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013438 nth = get_tv_number_chk(&argvars[3], &error);
13439 if (error)
13440 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 }
13442
13443 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13444 if (regmatch.regprog != NULL)
13445 {
13446 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013447
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013448 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013449 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013450 if (l != NULL)
13451 {
13452 if (li == NULL)
13453 {
13454 match = FALSE;
13455 break;
13456 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013457 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013458 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013459 if (str == NULL)
13460 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013461 }
13462
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013463 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013464
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013465 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013466 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013467 if (l == NULL && !match)
13468 break;
13469
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013470 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013471 if (l != NULL)
13472 {
13473 li = li->li_next;
13474 ++idx;
13475 }
13476 else
13477 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013478#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013479 startcol = (colnr_T)(regmatch.startp[0]
13480 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013481#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013482 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013483#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013484 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013485 }
13486
13487 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013489 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013490 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013491 int i;
13492
13493 /* return list with matched string and submatches */
13494 for (i = 0; i < NSUBEXP; ++i)
13495 {
13496 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013497 {
13498 if (list_append_string(rettv->vval.v_list,
13499 (char_u *)"", 0) == FAIL)
13500 break;
13501 }
13502 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013503 regmatch.startp[i],
13504 (int)(regmatch.endp[i] - regmatch.startp[i]))
13505 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013506 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013507 }
13508 }
13509 else if (type == 2)
13510 {
13511 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013512 if (l != NULL)
13513 copy_tv(&li->li_tv, rettv);
13514 else
13515 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013517 }
13518 else if (l != NULL)
13519 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 else
13521 {
13522 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013523 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 (varnumber_T)(regmatch.startp[0] - str);
13525 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013526 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013528 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 }
13530 }
13531 vim_free(regmatch.regprog);
13532 }
13533
13534theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013535 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536 p_cpo = save_cpo;
13537}
13538
13539/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013540 * "match()" function
13541 */
13542 static void
13543f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013544 typval_T *argvars;
13545 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013546{
13547 find_some_match(argvars, rettv, 1);
13548}
13549
13550/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013551 * "matchadd()" function
13552 */
13553 static void
13554f_matchadd(argvars, rettv)
13555 typval_T *argvars;
13556 typval_T *rettv;
13557{
13558#ifdef FEAT_SEARCH_EXTRA
13559 char_u buf[NUMBUFLEN];
13560 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13561 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13562 int prio = 10; /* default priority */
13563 int id = -1;
13564 int error = FALSE;
13565
13566 rettv->vval.v_number = -1;
13567
13568 if (grp == NULL || pat == NULL)
13569 return;
13570 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013571 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013572 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013573 if (argvars[3].v_type != VAR_UNKNOWN)
13574 id = get_tv_number_chk(&argvars[3], &error);
13575 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013576 if (error == TRUE)
13577 return;
13578 if (id >= 1 && id <= 3)
13579 {
13580 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13581 return;
13582 }
13583
13584 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13585#endif
13586}
13587
13588/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013589 * "matcharg()" function
13590 */
13591 static void
13592f_matcharg(argvars, rettv)
13593 typval_T *argvars;
13594 typval_T *rettv;
13595{
13596 if (rettv_list_alloc(rettv) == OK)
13597 {
13598#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013599 int id = get_tv_number(&argvars[0]);
13600 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013601
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013602 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013603 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013604 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13605 {
13606 list_append_string(rettv->vval.v_list,
13607 syn_id2name(m->hlg_id), -1);
13608 list_append_string(rettv->vval.v_list, m->pattern, -1);
13609 }
13610 else
13611 {
13612 list_append_string(rettv->vval.v_list, NUL, -1);
13613 list_append_string(rettv->vval.v_list, NUL, -1);
13614 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013615 }
13616#endif
13617 }
13618}
13619
13620/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013621 * "matchdelete()" function
13622 */
13623 static void
13624f_matchdelete(argvars, rettv)
13625 typval_T *argvars;
13626 typval_T *rettv;
13627{
13628#ifdef FEAT_SEARCH_EXTRA
13629 rettv->vval.v_number = match_delete(curwin,
13630 (int)get_tv_number(&argvars[0]), TRUE);
13631#endif
13632}
13633
13634/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013635 * "matchend()" function
13636 */
13637 static void
13638f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013639 typval_T *argvars;
13640 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013641{
13642 find_some_match(argvars, rettv, 0);
13643}
13644
13645/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013646 * "matchlist()" function
13647 */
13648 static void
13649f_matchlist(argvars, rettv)
13650 typval_T *argvars;
13651 typval_T *rettv;
13652{
13653 find_some_match(argvars, rettv, 3);
13654}
13655
13656/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013657 * "matchstr()" function
13658 */
13659 static void
13660f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013661 typval_T *argvars;
13662 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013663{
13664 find_some_match(argvars, rettv, 2);
13665}
13666
Bram Moolenaar33570922005-01-25 22:26:29 +000013667static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013668
13669 static void
13670max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013671 typval_T *argvars;
13672 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013673 int domax;
13674{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013675 long n = 0;
13676 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013677 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013678
13679 if (argvars[0].v_type == VAR_LIST)
13680 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013681 list_T *l;
13682 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013683
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013684 l = argvars[0].vval.v_list;
13685 if (l != NULL)
13686 {
13687 li = l->lv_first;
13688 if (li != NULL)
13689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013690 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013691 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013692 {
13693 li = li->li_next;
13694 if (li == NULL)
13695 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013697 if (domax ? i > n : i < n)
13698 n = i;
13699 }
13700 }
13701 }
13702 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013703 else if (argvars[0].v_type == VAR_DICT)
13704 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013705 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013706 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013707 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013708 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013709
13710 d = argvars[0].vval.v_dict;
13711 if (d != NULL)
13712 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013713 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013714 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013716 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013717 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013718 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013720 if (first)
13721 {
13722 n = i;
13723 first = FALSE;
13724 }
13725 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013726 n = i;
13727 }
13728 }
13729 }
13730 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013731 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013732 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013733 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013734}
13735
13736/*
13737 * "max()" function
13738 */
13739 static void
13740f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013741 typval_T *argvars;
13742 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013743{
13744 max_min(argvars, rettv, TRUE);
13745}
13746
13747/*
13748 * "min()" function
13749 */
13750 static void
13751f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013752 typval_T *argvars;
13753 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013754{
13755 max_min(argvars, rettv, FALSE);
13756}
13757
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013758static int mkdir_recurse __ARGS((char_u *dir, int prot));
13759
13760/*
13761 * Create the directory in which "dir" is located, and higher levels when
13762 * needed.
13763 */
13764 static int
13765mkdir_recurse(dir, prot)
13766 char_u *dir;
13767 int prot;
13768{
13769 char_u *p;
13770 char_u *updir;
13771 int r = FAIL;
13772
13773 /* Get end of directory name in "dir".
13774 * We're done when it's "/" or "c:/". */
13775 p = gettail_sep(dir);
13776 if (p <= get_past_head(dir))
13777 return OK;
13778
13779 /* If the directory exists we're done. Otherwise: create it.*/
13780 updir = vim_strnsave(dir, (int)(p - dir));
13781 if (updir == NULL)
13782 return FAIL;
13783 if (mch_isdir(updir))
13784 r = OK;
13785 else if (mkdir_recurse(updir, prot) == OK)
13786 r = vim_mkdir_emsg(updir, prot);
13787 vim_free(updir);
13788 return r;
13789}
13790
13791#ifdef vim_mkdir
13792/*
13793 * "mkdir()" function
13794 */
13795 static void
13796f_mkdir(argvars, rettv)
13797 typval_T *argvars;
13798 typval_T *rettv;
13799{
13800 char_u *dir;
13801 char_u buf[NUMBUFLEN];
13802 int prot = 0755;
13803
13804 rettv->vval.v_number = FAIL;
13805 if (check_restricted() || check_secure())
13806 return;
13807
13808 dir = get_tv_string_buf(&argvars[0], buf);
13809 if (argvars[1].v_type != VAR_UNKNOWN)
13810 {
13811 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013812 prot = get_tv_number_chk(&argvars[2], NULL);
13813 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013814 mkdir_recurse(dir, prot);
13815 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013816 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013817}
13818#endif
13819
Bram Moolenaar0d660222005-01-07 21:51:51 +000013820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 * "mode()" function
13822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013824f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013825 typval_T *argvars;
13826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013828 char_u buf[3];
13829
13830 buf[1] = NUL;
13831 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832
13833#ifdef FEAT_VISUAL
13834 if (VIsual_active)
13835 {
13836 if (VIsual_select)
13837 buf[0] = VIsual_mode + 's' - 'v';
13838 else
13839 buf[0] = VIsual_mode;
13840 }
13841 else
13842#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013843 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13844 || State == CONFIRM)
13845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013847 if (State == ASKMORE)
13848 buf[1] = 'm';
13849 else if (State == CONFIRM)
13850 buf[1] = '?';
13851 }
13852 else if (State == EXTERNCMD)
13853 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854 else if (State & INSERT)
13855 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013856#ifdef FEAT_VREPLACE
13857 if (State & VREPLACE_FLAG)
13858 {
13859 buf[0] = 'R';
13860 buf[1] = 'v';
13861 }
13862 else
13863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 if (State & REPLACE_FLAG)
13865 buf[0] = 'R';
13866 else
13867 buf[0] = 'i';
13868 }
13869 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013870 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013872 if (exmode_active)
13873 buf[1] = 'v';
13874 }
13875 else if (exmode_active)
13876 {
13877 buf[0] = 'c';
13878 buf[1] = 'e';
13879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013883 if (finish_op)
13884 buf[1] = 'o';
13885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013887 /* Clear out the minor mode when the argument is not a non-zero number or
13888 * non-empty string. */
13889 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013890 buf[1] = NUL;
13891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013892 rettv->vval.v_string = vim_strsave(buf);
13893 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894}
13895
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013896#ifdef FEAT_MZSCHEME
13897/*
13898 * "mzeval()" function
13899 */
13900 static void
13901f_mzeval(argvars, rettv)
13902 typval_T *argvars;
13903 typval_T *rettv;
13904{
13905 char_u *str;
13906 char_u buf[NUMBUFLEN];
13907
13908 str = get_tv_string_buf(&argvars[0], buf);
13909 do_mzeval(str, rettv);
13910}
13911#endif
13912
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013914 * "nextnonblank()" function
13915 */
13916 static void
13917f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013918 typval_T *argvars;
13919 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013920{
13921 linenr_T lnum;
13922
13923 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013925 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013926 {
13927 lnum = 0;
13928 break;
13929 }
13930 if (*skipwhite(ml_get(lnum)) != NUL)
13931 break;
13932 }
13933 rettv->vval.v_number = lnum;
13934}
13935
13936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937 * "nr2char()" function
13938 */
13939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013941 typval_T *argvars;
13942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943{
13944 char_u buf[NUMBUFLEN];
13945
13946#ifdef FEAT_MBYTE
13947 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013948 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 else
13950#endif
13951 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013952 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 buf[1] = NUL;
13954 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955 rettv->v_type = VAR_STRING;
13956 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013957}
13958
13959/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013960 * "pathshorten()" function
13961 */
13962 static void
13963f_pathshorten(argvars, rettv)
13964 typval_T *argvars;
13965 typval_T *rettv;
13966{
13967 char_u *p;
13968
13969 rettv->v_type = VAR_STRING;
13970 p = get_tv_string_chk(&argvars[0]);
13971 if (p == NULL)
13972 rettv->vval.v_string = NULL;
13973 else
13974 {
13975 p = vim_strsave(p);
13976 rettv->vval.v_string = p;
13977 if (p != NULL)
13978 shorten_dir(p);
13979 }
13980}
13981
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013982#ifdef FEAT_FLOAT
13983/*
13984 * "pow()" function
13985 */
13986 static void
13987f_pow(argvars, rettv)
13988 typval_T *argvars;
13989 typval_T *rettv;
13990{
13991 float_T fx, fy;
13992
13993 rettv->v_type = VAR_FLOAT;
13994 if (get_float_arg(argvars, &fx) == OK
13995 && get_float_arg(&argvars[1], &fy) == OK)
13996 rettv->vval.v_float = pow(fx, fy);
13997 else
13998 rettv->vval.v_float = 0.0;
13999}
14000#endif
14001
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014002/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014003 * "prevnonblank()" function
14004 */
14005 static void
14006f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014007 typval_T *argvars;
14008 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014009{
14010 linenr_T lnum;
14011
14012 lnum = get_tv_lnum(argvars);
14013 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14014 lnum = 0;
14015 else
14016 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14017 --lnum;
14018 rettv->vval.v_number = lnum;
14019}
14020
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014021#ifdef HAVE_STDARG_H
14022/* This dummy va_list is here because:
14023 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14024 * - locally in the function results in a "used before set" warning
14025 * - using va_start() to initialize it gives "function with fixed args" error */
14026static va_list ap;
14027#endif
14028
Bram Moolenaar8c711452005-01-14 21:53:12 +000014029/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014030 * "printf()" function
14031 */
14032 static void
14033f_printf(argvars, rettv)
14034 typval_T *argvars;
14035 typval_T *rettv;
14036{
14037 rettv->v_type = VAR_STRING;
14038 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014039#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014040 {
14041 char_u buf[NUMBUFLEN];
14042 int len;
14043 char_u *s;
14044 int saved_did_emsg = did_emsg;
14045 char *fmt;
14046
14047 /* Get the required length, allocate the buffer and do it for real. */
14048 did_emsg = FALSE;
14049 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014050 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014051 if (!did_emsg)
14052 {
14053 s = alloc(len + 1);
14054 if (s != NULL)
14055 {
14056 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014057 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014058 }
14059 }
14060 did_emsg |= saved_did_emsg;
14061 }
14062#endif
14063}
14064
14065/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014066 * "pumvisible()" function
14067 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014068 static void
14069f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014070 typval_T *argvars UNUSED;
14071 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014072{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014073#ifdef FEAT_INS_EXPAND
14074 if (pum_visible())
14075 rettv->vval.v_number = 1;
14076#endif
14077}
14078
14079/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014080 * "range()" function
14081 */
14082 static void
14083f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014084 typval_T *argvars;
14085 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014086{
14087 long start;
14088 long end;
14089 long stride = 1;
14090 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014091 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014092
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014093 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014094 if (argvars[1].v_type == VAR_UNKNOWN)
14095 {
14096 end = start - 1;
14097 start = 0;
14098 }
14099 else
14100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014101 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014102 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014103 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014104 }
14105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014106 if (error)
14107 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014108 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014109 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014110 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014111 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014112 else
14113 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014114 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014115 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014116 if (list_append_number(rettv->vval.v_list,
14117 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014118 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014119 }
14120}
14121
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014122/*
14123 * "readfile()" function
14124 */
14125 static void
14126f_readfile(argvars, rettv)
14127 typval_T *argvars;
14128 typval_T *rettv;
14129{
14130 int binary = FALSE;
14131 char_u *fname;
14132 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014133 listitem_T *li;
14134#define FREAD_SIZE 200 /* optimized for text lines */
14135 char_u buf[FREAD_SIZE];
14136 int readlen; /* size of last fread() */
14137 int buflen; /* nr of valid chars in buf[] */
14138 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14139 int tolist; /* first byte in buf[] still to be put in list */
14140 int chop; /* how many CR to chop off */
14141 char_u *prev = NULL; /* previously read bytes, if any */
14142 int prevlen = 0; /* length of "prev" if not NULL */
14143 char_u *s;
14144 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014145 long maxline = MAXLNUM;
14146 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014147
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014148 if (argvars[1].v_type != VAR_UNKNOWN)
14149 {
14150 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14151 binary = TRUE;
14152 if (argvars[2].v_type != VAR_UNKNOWN)
14153 maxline = get_tv_number(&argvars[2]);
14154 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014155
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014156 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014157 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014158
14159 /* Always open the file in binary mode, library functions have a mind of
14160 * their own about CR-LF conversion. */
14161 fname = get_tv_string(&argvars[0]);
14162 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14163 {
14164 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14165 return;
14166 }
14167
14168 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014169 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014170 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014171 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014172 buflen = filtd + readlen;
14173 tolist = 0;
14174 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14175 {
14176 if (buf[filtd] == '\n' || readlen <= 0)
14177 {
14178 /* Only when in binary mode add an empty list item when the
14179 * last line ends in a '\n'. */
14180 if (!binary && readlen == 0 && filtd == 0)
14181 break;
14182
14183 /* Found end-of-line or end-of-file: add a text line to the
14184 * list. */
14185 chop = 0;
14186 if (!binary)
14187 while (filtd - chop - 1 >= tolist
14188 && buf[filtd - chop - 1] == '\r')
14189 ++chop;
14190 len = filtd - tolist - chop;
14191 if (prev == NULL)
14192 s = vim_strnsave(buf + tolist, len);
14193 else
14194 {
14195 s = alloc((unsigned)(prevlen + len + 1));
14196 if (s != NULL)
14197 {
14198 mch_memmove(s, prev, prevlen);
14199 vim_free(prev);
14200 prev = NULL;
14201 mch_memmove(s + prevlen, buf + tolist, len);
14202 s[prevlen + len] = NUL;
14203 }
14204 }
14205 tolist = filtd + 1;
14206
14207 li = listitem_alloc();
14208 if (li == NULL)
14209 {
14210 vim_free(s);
14211 break;
14212 }
14213 li->li_tv.v_type = VAR_STRING;
14214 li->li_tv.v_lock = 0;
14215 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014216 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014217
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014218 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014219 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014220 if (readlen <= 0)
14221 break;
14222 }
14223 else if (buf[filtd] == NUL)
14224 buf[filtd] = '\n';
14225 }
14226 if (readlen <= 0)
14227 break;
14228
14229 if (tolist == 0)
14230 {
14231 /* "buf" is full, need to move text to an allocated buffer */
14232 if (prev == NULL)
14233 {
14234 prev = vim_strnsave(buf, buflen);
14235 prevlen = buflen;
14236 }
14237 else
14238 {
14239 s = alloc((unsigned)(prevlen + buflen));
14240 if (s != NULL)
14241 {
14242 mch_memmove(s, prev, prevlen);
14243 mch_memmove(s + prevlen, buf, buflen);
14244 vim_free(prev);
14245 prev = s;
14246 prevlen += buflen;
14247 }
14248 }
14249 filtd = 0;
14250 }
14251 else
14252 {
14253 mch_memmove(buf, buf + tolist, buflen - tolist);
14254 filtd -= tolist;
14255 }
14256 }
14257
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014258 /*
14259 * For a negative line count use only the lines at the end of the file,
14260 * free the rest.
14261 */
14262 if (maxline < 0)
14263 while (cnt > -maxline)
14264 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014265 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014266 --cnt;
14267 }
14268
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014269 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014270 fclose(fd);
14271}
14272
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014273#if defined(FEAT_RELTIME)
14274static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14275
14276/*
14277 * Convert a List to proftime_T.
14278 * Return FAIL when there is something wrong.
14279 */
14280 static int
14281list2proftime(arg, tm)
14282 typval_T *arg;
14283 proftime_T *tm;
14284{
14285 long n1, n2;
14286 int error = FALSE;
14287
14288 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14289 || arg->vval.v_list->lv_len != 2)
14290 return FAIL;
14291 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14292 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14293# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014294 tm->HighPart = n1;
14295 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014296# else
14297 tm->tv_sec = n1;
14298 tm->tv_usec = n2;
14299# endif
14300 return error ? FAIL : OK;
14301}
14302#endif /* FEAT_RELTIME */
14303
14304/*
14305 * "reltime()" function
14306 */
14307 static void
14308f_reltime(argvars, rettv)
14309 typval_T *argvars;
14310 typval_T *rettv;
14311{
14312#ifdef FEAT_RELTIME
14313 proftime_T res;
14314 proftime_T start;
14315
14316 if (argvars[0].v_type == VAR_UNKNOWN)
14317 {
14318 /* No arguments: get current time. */
14319 profile_start(&res);
14320 }
14321 else if (argvars[1].v_type == VAR_UNKNOWN)
14322 {
14323 if (list2proftime(&argvars[0], &res) == FAIL)
14324 return;
14325 profile_end(&res);
14326 }
14327 else
14328 {
14329 /* Two arguments: compute the difference. */
14330 if (list2proftime(&argvars[0], &start) == FAIL
14331 || list2proftime(&argvars[1], &res) == FAIL)
14332 return;
14333 profile_sub(&res, &start);
14334 }
14335
14336 if (rettv_list_alloc(rettv) == OK)
14337 {
14338 long n1, n2;
14339
14340# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014341 n1 = res.HighPart;
14342 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014343# else
14344 n1 = res.tv_sec;
14345 n2 = res.tv_usec;
14346# endif
14347 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14348 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14349 }
14350#endif
14351}
14352
14353/*
14354 * "reltimestr()" function
14355 */
14356 static void
14357f_reltimestr(argvars, rettv)
14358 typval_T *argvars;
14359 typval_T *rettv;
14360{
14361#ifdef FEAT_RELTIME
14362 proftime_T tm;
14363#endif
14364
14365 rettv->v_type = VAR_STRING;
14366 rettv->vval.v_string = NULL;
14367#ifdef FEAT_RELTIME
14368 if (list2proftime(&argvars[0], &tm) == OK)
14369 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14370#endif
14371}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014372
Bram Moolenaar0d660222005-01-07 21:51:51 +000014373#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14374static void make_connection __ARGS((void));
14375static int check_connection __ARGS((void));
14376
14377 static void
14378make_connection()
14379{
14380 if (X_DISPLAY == NULL
14381# ifdef FEAT_GUI
14382 && !gui.in_use
14383# endif
14384 )
14385 {
14386 x_force_connect = TRUE;
14387 setup_term_clip();
14388 x_force_connect = FALSE;
14389 }
14390}
14391
14392 static int
14393check_connection()
14394{
14395 make_connection();
14396 if (X_DISPLAY == NULL)
14397 {
14398 EMSG(_("E240: No connection to Vim server"));
14399 return FAIL;
14400 }
14401 return OK;
14402}
14403#endif
14404
14405#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014406static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014407
14408 static void
14409remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014410 typval_T *argvars;
14411 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014412 int expr;
14413{
14414 char_u *server_name;
14415 char_u *keys;
14416 char_u *r = NULL;
14417 char_u buf[NUMBUFLEN];
14418# ifdef WIN32
14419 HWND w;
14420# else
14421 Window w;
14422# endif
14423
14424 if (check_restricted() || check_secure())
14425 return;
14426
14427# ifdef FEAT_X11
14428 if (check_connection() == FAIL)
14429 return;
14430# endif
14431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014432 server_name = get_tv_string_chk(&argvars[0]);
14433 if (server_name == NULL)
14434 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014435 keys = get_tv_string_buf(&argvars[1], buf);
14436# ifdef WIN32
14437 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14438# else
14439 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14440 < 0)
14441# endif
14442 {
14443 if (r != NULL)
14444 EMSG(r); /* sending worked but evaluation failed */
14445 else
14446 EMSG2(_("E241: Unable to send to %s"), server_name);
14447 return;
14448 }
14449
14450 rettv->vval.v_string = r;
14451
14452 if (argvars[2].v_type != VAR_UNKNOWN)
14453 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014454 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014455 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014456 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014457
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014458 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014459 v.di_tv.v_type = VAR_STRING;
14460 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014461 idvar = get_tv_string_chk(&argvars[2]);
14462 if (idvar != NULL)
14463 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014464 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014465 }
14466}
14467#endif
14468
14469/*
14470 * "remote_expr()" function
14471 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014472 static void
14473f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014474 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014475 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014476{
14477 rettv->v_type = VAR_STRING;
14478 rettv->vval.v_string = NULL;
14479#ifdef FEAT_CLIENTSERVER
14480 remote_common(argvars, rettv, TRUE);
14481#endif
14482}
14483
14484/*
14485 * "remote_foreground()" function
14486 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014487 static void
14488f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014489 typval_T *argvars UNUSED;
14490 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014491{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014492#ifdef FEAT_CLIENTSERVER
14493# ifdef WIN32
14494 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014495 {
14496 char_u *server_name = get_tv_string_chk(&argvars[0]);
14497
14498 if (server_name != NULL)
14499 serverForeground(server_name);
14500 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014501# else
14502 /* Send a foreground() expression to the server. */
14503 argvars[1].v_type = VAR_STRING;
14504 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14505 argvars[2].v_type = VAR_UNKNOWN;
14506 remote_common(argvars, rettv, TRUE);
14507 vim_free(argvars[1].vval.v_string);
14508# endif
14509#endif
14510}
14511
Bram Moolenaar0d660222005-01-07 21:51:51 +000014512 static void
14513f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014514 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014515 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014516{
14517#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014518 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014519 char_u *s = NULL;
14520# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014521 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014522# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014523 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014524
14525 if (check_restricted() || check_secure())
14526 {
14527 rettv->vval.v_number = -1;
14528 return;
14529 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014530 serverid = get_tv_string_chk(&argvars[0]);
14531 if (serverid == NULL)
14532 {
14533 rettv->vval.v_number = -1;
14534 return; /* type error; errmsg already given */
14535 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014536# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014537 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014538 if (n == 0)
14539 rettv->vval.v_number = -1;
14540 else
14541 {
14542 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14543 rettv->vval.v_number = (s != NULL);
14544 }
14545# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014546 if (check_connection() == FAIL)
14547 return;
14548
14549 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014550 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014551# endif
14552
14553 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014555 char_u *retvar;
14556
Bram Moolenaar33570922005-01-25 22:26:29 +000014557 v.di_tv.v_type = VAR_STRING;
14558 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014559 retvar = get_tv_string_chk(&argvars[1]);
14560 if (retvar != NULL)
14561 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014562 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014563 }
14564#else
14565 rettv->vval.v_number = -1;
14566#endif
14567}
14568
Bram Moolenaar0d660222005-01-07 21:51:51 +000014569 static void
14570f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014571 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014572 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014573{
14574 char_u *r = NULL;
14575
14576#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014577 char_u *serverid = get_tv_string_chk(&argvars[0]);
14578
14579 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014580 {
14581# ifdef WIN32
14582 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014583 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014584
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014585 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014586 if (n != 0)
14587 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14588 if (r == NULL)
14589# else
14590 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014591 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014592# endif
14593 EMSG(_("E277: Unable to read a server reply"));
14594 }
14595#endif
14596 rettv->v_type = VAR_STRING;
14597 rettv->vval.v_string = r;
14598}
14599
14600/*
14601 * "remote_send()" function
14602 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014603 static void
14604f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014605 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014606 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014607{
14608 rettv->v_type = VAR_STRING;
14609 rettv->vval.v_string = NULL;
14610#ifdef FEAT_CLIENTSERVER
14611 remote_common(argvars, rettv, FALSE);
14612#endif
14613}
14614
14615/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014616 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014617 */
14618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014619f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014620 typval_T *argvars;
14621 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014622{
Bram Moolenaar33570922005-01-25 22:26:29 +000014623 list_T *l;
14624 listitem_T *item, *item2;
14625 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014626 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014627 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014628 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014629 dict_T *d;
14630 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014631
Bram Moolenaar8c711452005-01-14 21:53:12 +000014632 if (argvars[0].v_type == VAR_DICT)
14633 {
14634 if (argvars[2].v_type != VAR_UNKNOWN)
14635 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014636 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014637 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014639 key = get_tv_string_chk(&argvars[1]);
14640 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014641 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014642 di = dict_find(d, key, -1);
14643 if (di == NULL)
14644 EMSG2(_(e_dictkey), key);
14645 else
14646 {
14647 *rettv = di->di_tv;
14648 init_tv(&di->di_tv);
14649 dictitem_remove(d, di);
14650 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014651 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014652 }
14653 }
14654 else if (argvars[0].v_type != VAR_LIST)
14655 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014656 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014657 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014659 int error = FALSE;
14660
14661 idx = get_tv_number_chk(&argvars[1], &error);
14662 if (error)
14663 ; /* type error: do nothing, errmsg already given */
14664 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014665 EMSGN(_(e_listidx), idx);
14666 else
14667 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014668 if (argvars[2].v_type == VAR_UNKNOWN)
14669 {
14670 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014671 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014672 *rettv = item->li_tv;
14673 vim_free(item);
14674 }
14675 else
14676 {
14677 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014678 end = get_tv_number_chk(&argvars[2], &error);
14679 if (error)
14680 ; /* type error: do nothing */
14681 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014682 EMSGN(_(e_listidx), end);
14683 else
14684 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014685 int cnt = 0;
14686
14687 for (li = item; li != NULL; li = li->li_next)
14688 {
14689 ++cnt;
14690 if (li == item2)
14691 break;
14692 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014693 if (li == NULL) /* didn't find "item2" after "item" */
14694 EMSG(_(e_invrange));
14695 else
14696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014697 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014698 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014699 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014700 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014701 l->lv_first = item;
14702 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014703 item->li_prev = NULL;
14704 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014705 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014706 }
14707 }
14708 }
14709 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014710 }
14711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712}
14713
14714/*
14715 * "rename({from}, {to})" function
14716 */
14717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014718f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014719 typval_T *argvars;
14720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721{
14722 char_u buf[NUMBUFLEN];
14723
14724 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014725 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014727 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14728 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729}
14730
14731/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014732 * "repeat()" function
14733 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014734 static void
14735f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014736 typval_T *argvars;
14737 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014738{
14739 char_u *p;
14740 int n;
14741 int slen;
14742 int len;
14743 char_u *r;
14744 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014745
14746 n = get_tv_number(&argvars[1]);
14747 if (argvars[0].v_type == VAR_LIST)
14748 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014749 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014750 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014751 if (list_extend(rettv->vval.v_list,
14752 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014753 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014754 }
14755 else
14756 {
14757 p = get_tv_string(&argvars[0]);
14758 rettv->v_type = VAR_STRING;
14759 rettv->vval.v_string = NULL;
14760
14761 slen = (int)STRLEN(p);
14762 len = slen * n;
14763 if (len <= 0)
14764 return;
14765
14766 r = alloc(len + 1);
14767 if (r != NULL)
14768 {
14769 for (i = 0; i < n; i++)
14770 mch_memmove(r + i * slen, p, (size_t)slen);
14771 r[len] = NUL;
14772 }
14773
14774 rettv->vval.v_string = r;
14775 }
14776}
14777
14778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779 * "resolve()" function
14780 */
14781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014782f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014783 typval_T *argvars;
14784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785{
14786 char_u *p;
14787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014788 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789#ifdef FEAT_SHORTCUT
14790 {
14791 char_u *v = NULL;
14792
14793 v = mch_resolve_shortcut(p);
14794 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014795 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014797 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 }
14799#else
14800# ifdef HAVE_READLINK
14801 {
14802 char_u buf[MAXPATHL + 1];
14803 char_u *cpy;
14804 int len;
14805 char_u *remain = NULL;
14806 char_u *q;
14807 int is_relative_to_current = FALSE;
14808 int has_trailing_pathsep = FALSE;
14809 int limit = 100;
14810
14811 p = vim_strsave(p);
14812
14813 if (p[0] == '.' && (vim_ispathsep(p[1])
14814 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14815 is_relative_to_current = TRUE;
14816
14817 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014818 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819 has_trailing_pathsep = TRUE;
14820
14821 q = getnextcomp(p);
14822 if (*q != NUL)
14823 {
14824 /* Separate the first path component in "p", and keep the
14825 * remainder (beginning with the path separator). */
14826 remain = vim_strsave(q - 1);
14827 q[-1] = NUL;
14828 }
14829
14830 for (;;)
14831 {
14832 for (;;)
14833 {
14834 len = readlink((char *)p, (char *)buf, MAXPATHL);
14835 if (len <= 0)
14836 break;
14837 buf[len] = NUL;
14838
14839 if (limit-- == 0)
14840 {
14841 vim_free(p);
14842 vim_free(remain);
14843 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014844 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 goto fail;
14846 }
14847
14848 /* Ensure that the result will have a trailing path separator
14849 * if the argument has one. */
14850 if (remain == NULL && has_trailing_pathsep)
14851 add_pathsep(buf);
14852
14853 /* Separate the first path component in the link value and
14854 * concatenate the remainders. */
14855 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14856 if (*q != NUL)
14857 {
14858 if (remain == NULL)
14859 remain = vim_strsave(q - 1);
14860 else
14861 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014862 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 if (cpy != NULL)
14864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865 vim_free(remain);
14866 remain = cpy;
14867 }
14868 }
14869 q[-1] = NUL;
14870 }
14871
14872 q = gettail(p);
14873 if (q > p && *q == NUL)
14874 {
14875 /* Ignore trailing path separator. */
14876 q[-1] = NUL;
14877 q = gettail(p);
14878 }
14879 if (q > p && !mch_isFullName(buf))
14880 {
14881 /* symlink is relative to directory of argument */
14882 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14883 if (cpy != NULL)
14884 {
14885 STRCPY(cpy, p);
14886 STRCPY(gettail(cpy), buf);
14887 vim_free(p);
14888 p = cpy;
14889 }
14890 }
14891 else
14892 {
14893 vim_free(p);
14894 p = vim_strsave(buf);
14895 }
14896 }
14897
14898 if (remain == NULL)
14899 break;
14900
14901 /* Append the first path component of "remain" to "p". */
14902 q = getnextcomp(remain + 1);
14903 len = q - remain - (*q != NUL);
14904 cpy = vim_strnsave(p, STRLEN(p) + len);
14905 if (cpy != NULL)
14906 {
14907 STRNCAT(cpy, remain, len);
14908 vim_free(p);
14909 p = cpy;
14910 }
14911 /* Shorten "remain". */
14912 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014913 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914 else
14915 {
14916 vim_free(remain);
14917 remain = NULL;
14918 }
14919 }
14920
14921 /* If the result is a relative path name, make it explicitly relative to
14922 * the current directory if and only if the argument had this form. */
14923 if (!vim_ispathsep(*p))
14924 {
14925 if (is_relative_to_current
14926 && *p != NUL
14927 && !(p[0] == '.'
14928 && (p[1] == NUL
14929 || vim_ispathsep(p[1])
14930 || (p[1] == '.'
14931 && (p[2] == NUL
14932 || vim_ispathsep(p[2]))))))
14933 {
14934 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014935 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936 if (cpy != NULL)
14937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938 vim_free(p);
14939 p = cpy;
14940 }
14941 }
14942 else if (!is_relative_to_current)
14943 {
14944 /* Strip leading "./". */
14945 q = p;
14946 while (q[0] == '.' && vim_ispathsep(q[1]))
14947 q += 2;
14948 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014949 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950 }
14951 }
14952
14953 /* Ensure that the result will have no trailing path separator
14954 * if the argument had none. But keep "/" or "//". */
14955 if (!has_trailing_pathsep)
14956 {
14957 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014958 if (after_pathsep(p, q))
14959 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 }
14961
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014962 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963 }
14964# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014965 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966# endif
14967#endif
14968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014969 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970
14971#ifdef HAVE_READLINK
14972fail:
14973#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014974 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975}
14976
14977/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014978 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 */
14980 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014981f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014982 typval_T *argvars;
14983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984{
Bram Moolenaar33570922005-01-25 22:26:29 +000014985 list_T *l;
14986 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987
Bram Moolenaar0d660222005-01-07 21:51:51 +000014988 if (argvars[0].v_type != VAR_LIST)
14989 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014990 else if ((l = argvars[0].vval.v_list) != NULL
14991 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014992 {
14993 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014994 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014995 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996 while (li != NULL)
14997 {
14998 ni = li->li_prev;
14999 list_append(l, li);
15000 li = ni;
15001 }
15002 rettv->vval.v_list = l;
15003 rettv->v_type = VAR_LIST;
15004 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015005 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007}
15008
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015009#define SP_NOMOVE 0x01 /* don't move cursor */
15010#define SP_REPEAT 0x02 /* repeat to find outer pair */
15011#define SP_RETCOUNT 0x04 /* return matchcount */
15012#define SP_SETPCMARK 0x08 /* set previous context mark */
15013#define SP_START 0x10 /* accept match at start position */
15014#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15015#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015016
Bram Moolenaar33570922005-01-25 22:26:29 +000015017static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015018
15019/*
15020 * Get flags for a search function.
15021 * Possibly sets "p_ws".
15022 * Returns BACKWARD, FORWARD or zero (for an error).
15023 */
15024 static int
15025get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015026 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015027 int *flagsp;
15028{
15029 int dir = FORWARD;
15030 char_u *flags;
15031 char_u nbuf[NUMBUFLEN];
15032 int mask;
15033
15034 if (varp->v_type != VAR_UNKNOWN)
15035 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015036 flags = get_tv_string_buf_chk(varp, nbuf);
15037 if (flags == NULL)
15038 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015039 while (*flags != NUL)
15040 {
15041 switch (*flags)
15042 {
15043 case 'b': dir = BACKWARD; break;
15044 case 'w': p_ws = TRUE; break;
15045 case 'W': p_ws = FALSE; break;
15046 default: mask = 0;
15047 if (flagsp != NULL)
15048 switch (*flags)
15049 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015050 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015051 case 'e': mask = SP_END; break;
15052 case 'm': mask = SP_RETCOUNT; break;
15053 case 'n': mask = SP_NOMOVE; break;
15054 case 'p': mask = SP_SUBPAT; break;
15055 case 'r': mask = SP_REPEAT; break;
15056 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015057 }
15058 if (mask == 0)
15059 {
15060 EMSG2(_(e_invarg2), flags);
15061 dir = 0;
15062 }
15063 else
15064 *flagsp |= mask;
15065 }
15066 if (dir == 0)
15067 break;
15068 ++flags;
15069 }
15070 }
15071 return dir;
15072}
15073
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015075 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015077 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015078search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015079 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015080 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015081 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015083 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084 char_u *pat;
15085 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015086 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087 int save_p_ws = p_ws;
15088 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015089 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015090 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015091 proftime_T tm;
15092#ifdef FEAT_RELTIME
15093 long time_limit = 0;
15094#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015095 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015096 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015098 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015099 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015100 if (dir == 0)
15101 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015102 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015103 if (flags & SP_START)
15104 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015105 if (flags & SP_END)
15106 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015107
Bram Moolenaar76929292008-01-06 19:07:36 +000015108 /* Optional arguments: line number to stop searching and timeout. */
15109 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015110 {
15111 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15112 if (lnum_stop < 0)
15113 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015114#ifdef FEAT_RELTIME
15115 if (argvars[3].v_type != VAR_UNKNOWN)
15116 {
15117 time_limit = get_tv_number_chk(&argvars[3], NULL);
15118 if (time_limit < 0)
15119 goto theend;
15120 }
15121#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015122 }
15123
Bram Moolenaar76929292008-01-06 19:07:36 +000015124#ifdef FEAT_RELTIME
15125 /* Set the time limit, if there is one. */
15126 profile_setlimit(time_limit, &tm);
15127#endif
15128
Bram Moolenaar231334e2005-07-25 20:46:57 +000015129 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015130 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015131 * Check to make sure only those flags are set.
15132 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15133 * flags cannot be set. Check for that condition also.
15134 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015135 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015136 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015137 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015138 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015139 goto theend;
15140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015142 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015143 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015144 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015145 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015146 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015147 if (flags & SP_SUBPAT)
15148 retval = subpatnum;
15149 else
15150 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015151 if (flags & SP_SETPCMARK)
15152 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015154 if (match_pos != NULL)
15155 {
15156 /* Store the match cursor position */
15157 match_pos->lnum = pos.lnum;
15158 match_pos->col = pos.col + 1;
15159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160 /* "/$" will put the cursor after the end of the line, may need to
15161 * correct that here */
15162 check_cursor();
15163 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015164
15165 /* If 'n' flag is used: restore cursor position. */
15166 if (flags & SP_NOMOVE)
15167 curwin->w_cursor = save_cursor;
Bram Moolenaar860cae12010-06-05 23:22:07 +020015168#ifdef FEAT_CONCEAL
15169 else if (curwin->w_p_conceal
15170 && save_cursor.lnum != curwin->w_cursor.lnum)
15171 {
15172 curwin->w_set_curswant = TRUE;
15173 update_single_line(curwin, save_cursor.lnum);
15174 update_single_line(curwin, curwin->w_cursor.lnum);
15175 }
15176#endif
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015177 else
15178 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015179theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015181
15182 return retval;
15183}
15184
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015185#ifdef FEAT_FLOAT
15186/*
15187 * "round({float})" function
15188 */
15189 static void
15190f_round(argvars, rettv)
15191 typval_T *argvars;
15192 typval_T *rettv;
15193{
15194 float_T f;
15195
15196 rettv->v_type = VAR_FLOAT;
15197 if (get_float_arg(argvars, &f) == OK)
15198 /* round() is not in C90, use ceil() or floor() instead. */
15199 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15200 else
15201 rettv->vval.v_float = 0.0;
15202}
15203#endif
15204
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015205/*
15206 * "search()" function
15207 */
15208 static void
15209f_search(argvars, rettv)
15210 typval_T *argvars;
15211 typval_T *rettv;
15212{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015213 int flags = 0;
15214
15215 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216}
15217
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015219 * "searchdecl()" function
15220 */
15221 static void
15222f_searchdecl(argvars, rettv)
15223 typval_T *argvars;
15224 typval_T *rettv;
15225{
15226 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015227 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015228 int error = FALSE;
15229 char_u *name;
15230
15231 rettv->vval.v_number = 1; /* default: FAIL */
15232
15233 name = get_tv_string_chk(&argvars[0]);
15234 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015235 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015236 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015237 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15238 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15239 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015240 if (!error && name != NULL)
15241 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015242 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015243}
15244
15245/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015246 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015248 static int
15249searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015250 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015251 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252{
15253 char_u *spat, *mpat, *epat;
15254 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256 int dir;
15257 int flags = 0;
15258 char_u nbuf1[NUMBUFLEN];
15259 char_u nbuf2[NUMBUFLEN];
15260 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015261 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015262 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015263 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015266 spat = get_tv_string_chk(&argvars[0]);
15267 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15268 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15269 if (spat == NULL || mpat == NULL || epat == NULL)
15270 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272 /* Handle the optional fourth argument: flags */
15273 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015274 if (dir == 0)
15275 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015276
15277 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015278 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15279 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015280 if ((flags & (SP_END | SP_SUBPAT)) != 0
15281 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015282 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015283 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015284 goto theend;
15285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015287 /* Using 'r' implies 'W', otherwise it doesn't work. */
15288 if (flags & SP_REPEAT)
15289 p_ws = FALSE;
15290
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015291 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015292 if (argvars[3].v_type == VAR_UNKNOWN
15293 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294 skip = (char_u *)"";
15295 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015297 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015298 if (argvars[5].v_type != VAR_UNKNOWN)
15299 {
15300 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15301 if (lnum_stop < 0)
15302 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015303#ifdef FEAT_RELTIME
15304 if (argvars[6].v_type != VAR_UNKNOWN)
15305 {
15306 time_limit = get_tv_number_chk(&argvars[6], NULL);
15307 if (time_limit < 0)
15308 goto theend;
15309 }
15310#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015311 }
15312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015313 if (skip == NULL)
15314 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015316 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015317 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015318
15319theend:
15320 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015321
15322 return retval;
15323}
15324
15325/*
15326 * "searchpair()" function
15327 */
15328 static void
15329f_searchpair(argvars, rettv)
15330 typval_T *argvars;
15331 typval_T *rettv;
15332{
15333 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15334}
15335
15336/*
15337 * "searchpairpos()" function
15338 */
15339 static void
15340f_searchpairpos(argvars, rettv)
15341 typval_T *argvars;
15342 typval_T *rettv;
15343{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015344 pos_T match_pos;
15345 int lnum = 0;
15346 int col = 0;
15347
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015348 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015349 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015350
15351 if (searchpair_cmn(argvars, &match_pos) > 0)
15352 {
15353 lnum = match_pos.lnum;
15354 col = match_pos.col;
15355 }
15356
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015357 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15358 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015359}
15360
15361/*
15362 * Search for a start/middle/end thing.
15363 * Used by searchpair(), see its documentation for the details.
15364 * Returns 0 or -1 for no match,
15365 */
15366 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015367do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15368 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015369 char_u *spat; /* start pattern */
15370 char_u *mpat; /* middle pattern */
15371 char_u *epat; /* end pattern */
15372 int dir; /* BACKWARD or FORWARD */
15373 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015374 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015375 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015376 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015377 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015378{
15379 char_u *save_cpo;
15380 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15381 long retval = 0;
15382 pos_T pos;
15383 pos_T firstpos;
15384 pos_T foundpos;
15385 pos_T save_cursor;
15386 pos_T save_pos;
15387 int n;
15388 int r;
15389 int nest = 1;
15390 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015391 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015392 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015393
15394 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15395 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015396 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015397
Bram Moolenaar76929292008-01-06 19:07:36 +000015398#ifdef FEAT_RELTIME
15399 /* Set the time limit, if there is one. */
15400 profile_setlimit(time_limit, &tm);
15401#endif
15402
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015403 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15404 * start/middle/end (pat3, for the top pair). */
15405 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15406 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15407 if (pat2 == NULL || pat3 == NULL)
15408 goto theend;
15409 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15410 if (*mpat == NUL)
15411 STRCPY(pat3, pat2);
15412 else
15413 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15414 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015415 if (flags & SP_START)
15416 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015417
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 save_cursor = curwin->w_cursor;
15419 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015420 clearpos(&firstpos);
15421 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422 pat = pat3;
15423 for (;;)
15424 {
15425 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015426 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15428 /* didn't find it or found the first match again: FAIL */
15429 break;
15430
15431 if (firstpos.lnum == 0)
15432 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015433 if (equalpos(pos, foundpos))
15434 {
15435 /* Found the same position again. Can happen with a pattern that
15436 * has "\zs" at the end and searching backwards. Advance one
15437 * character and try again. */
15438 if (dir == BACKWARD)
15439 decl(&pos);
15440 else
15441 incl(&pos);
15442 }
15443 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015445 /* clear the start flag to avoid getting stuck here */
15446 options &= ~SEARCH_START;
15447
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 /* If the skip pattern matches, ignore this match. */
15449 if (*skip != NUL)
15450 {
15451 save_pos = curwin->w_cursor;
15452 curwin->w_cursor = pos;
15453 r = eval_to_bool(skip, &err, NULL, FALSE);
15454 curwin->w_cursor = save_pos;
15455 if (err)
15456 {
15457 /* Evaluating {skip} caused an error, break here. */
15458 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015459 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 break;
15461 }
15462 if (r)
15463 continue;
15464 }
15465
15466 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15467 {
15468 /* Found end when searching backwards or start when searching
15469 * forward: nested pair. */
15470 ++nest;
15471 pat = pat2; /* nested, don't search for middle */
15472 }
15473 else
15474 {
15475 /* Found end when searching forward or start when searching
15476 * backward: end of (nested) pair; or found middle in outer pair. */
15477 if (--nest == 1)
15478 pat = pat3; /* outer level, search for middle */
15479 }
15480
15481 if (nest == 0)
15482 {
15483 /* Found the match: return matchcount or line number. */
15484 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015485 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015487 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015488 if (flags & SP_SETPCMARK)
15489 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 curwin->w_cursor = pos;
15491 if (!(flags & SP_REPEAT))
15492 break;
15493 nest = 1; /* search for next unmatched */
15494 }
15495 }
15496
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015497 if (match_pos != NULL)
15498 {
15499 /* Store the match cursor position */
15500 match_pos->lnum = curwin->w_cursor.lnum;
15501 match_pos->col = curwin->w_cursor.col + 1;
15502 }
15503
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015505 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506 curwin->w_cursor = save_cursor;
15507
15508theend:
15509 vim_free(pat2);
15510 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015511 if (p_cpo == empty_option)
15512 p_cpo = save_cpo;
15513 else
15514 /* Darn, evaluating the {skip} expression changed the value. */
15515 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015516
15517 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518}
15519
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015520/*
15521 * "searchpos()" function
15522 */
15523 static void
15524f_searchpos(argvars, rettv)
15525 typval_T *argvars;
15526 typval_T *rettv;
15527{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015528 pos_T match_pos;
15529 int lnum = 0;
15530 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015531 int n;
15532 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015533
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015534 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015535 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015536
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015537 n = search_cmn(argvars, &match_pos, &flags);
15538 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015539 {
15540 lnum = match_pos.lnum;
15541 col = match_pos.col;
15542 }
15543
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015544 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15545 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015546 if (flags & SP_SUBPAT)
15547 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015548}
15549
15550
Bram Moolenaar0d660222005-01-07 21:51:51 +000015551 static void
15552f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015553 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015556#ifdef FEAT_CLIENTSERVER
15557 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015558 char_u *server = get_tv_string_chk(&argvars[0]);
15559 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560
Bram Moolenaar0d660222005-01-07 21:51:51 +000015561 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015562 if (server == NULL || reply == NULL)
15563 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015564 if (check_restricted() || check_secure())
15565 return;
15566# ifdef FEAT_X11
15567 if (check_connection() == FAIL)
15568 return;
15569# endif
15570
15571 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015573 EMSG(_("E258: Unable to send to client"));
15574 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015576 rettv->vval.v_number = 0;
15577#else
15578 rettv->vval.v_number = -1;
15579#endif
15580}
15581
Bram Moolenaar0d660222005-01-07 21:51:51 +000015582 static void
15583f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015584 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015585 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015586{
15587 char_u *r = NULL;
15588
15589#ifdef FEAT_CLIENTSERVER
15590# ifdef WIN32
15591 r = serverGetVimNames();
15592# else
15593 make_connection();
15594 if (X_DISPLAY != NULL)
15595 r = serverGetVimNames(X_DISPLAY);
15596# endif
15597#endif
15598 rettv->v_type = VAR_STRING;
15599 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600}
15601
15602/*
15603 * "setbufvar()" function
15604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015606f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015607 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015608 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609{
15610 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015613 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 char_u nbuf[NUMBUFLEN];
15615
15616 if (check_restricted() || check_secure())
15617 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015618 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15619 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015620 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 varp = &argvars[2];
15622
15623 if (buf != NULL && varname != NULL && varp != NULL)
15624 {
15625 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627
15628 if (*varname == '&')
15629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015630 long numval;
15631 char_u *strval;
15632 int error = FALSE;
15633
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015635 numval = get_tv_number_chk(varp, &error);
15636 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015637 if (!error && strval != NULL)
15638 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 }
15640 else
15641 {
15642 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15643 if (bufvarname != NULL)
15644 {
15645 STRCPY(bufvarname, "b:");
15646 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015647 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 vim_free(bufvarname);
15649 }
15650 }
15651
15652 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655}
15656
15657/*
15658 * "setcmdpos()" function
15659 */
15660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015661f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015662 typval_T *argvars;
15663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015665 int pos = (int)get_tv_number(&argvars[0]) - 1;
15666
15667 if (pos >= 0)
15668 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669}
15670
15671/*
15672 * "setline()" function
15673 */
15674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015675f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015676 typval_T *argvars;
15677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678{
15679 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015680 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015681 list_T *l = NULL;
15682 listitem_T *li = NULL;
15683 long added = 0;
15684 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015686 lnum = get_tv_lnum(&argvars[0]);
15687 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015689 l = argvars[1].vval.v_list;
15690 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015692 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015693 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015694
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015695 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015696 for (;;)
15697 {
15698 if (l != NULL)
15699 {
15700 /* list argument, get next string */
15701 if (li == NULL)
15702 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015703 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015704 li = li->li_next;
15705 }
15706
15707 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015708 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015709 break;
15710 if (lnum <= curbuf->b_ml.ml_line_count)
15711 {
15712 /* existing line, replace it */
15713 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15714 {
15715 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015716 if (lnum == curwin->w_cursor.lnum)
15717 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015718 rettv->vval.v_number = 0; /* OK */
15719 }
15720 }
15721 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15722 {
15723 /* lnum is one past the last line, append the line */
15724 ++added;
15725 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15726 rettv->vval.v_number = 0; /* OK */
15727 }
15728
15729 if (l == NULL) /* only one string argument */
15730 break;
15731 ++lnum;
15732 }
15733
15734 if (added > 0)
15735 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736}
15737
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015738static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15739
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015741 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015742 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015743 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015744set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015745 win_T *wp UNUSED;
15746 typval_T *list_arg UNUSED;
15747 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015748 typval_T *rettv;
15749{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015750#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015751 char_u *act;
15752 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015753#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015754
Bram Moolenaar2641f772005-03-25 21:58:17 +000015755 rettv->vval.v_number = -1;
15756
15757#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015758 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015759 EMSG(_(e_listreq));
15760 else
15761 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015762 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015763
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015764 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015765 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015766 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015767 if (act == NULL)
15768 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015769 if (*act == 'a' || *act == 'r')
15770 action = *act;
15771 }
15772
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015773 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015774 rettv->vval.v_number = 0;
15775 }
15776#endif
15777}
15778
15779/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015780 * "setloclist()" function
15781 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015782 static void
15783f_setloclist(argvars, rettv)
15784 typval_T *argvars;
15785 typval_T *rettv;
15786{
15787 win_T *win;
15788
15789 rettv->vval.v_number = -1;
15790
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015791 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015792 if (win != NULL)
15793 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15794}
15795
15796/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015797 * "setmatches()" function
15798 */
15799 static void
15800f_setmatches(argvars, rettv)
15801 typval_T *argvars;
15802 typval_T *rettv;
15803{
15804#ifdef FEAT_SEARCH_EXTRA
15805 list_T *l;
15806 listitem_T *li;
15807 dict_T *d;
15808
15809 rettv->vval.v_number = -1;
15810 if (argvars[0].v_type != VAR_LIST)
15811 {
15812 EMSG(_(e_listreq));
15813 return;
15814 }
15815 if ((l = argvars[0].vval.v_list) != NULL)
15816 {
15817
15818 /* To some extent make sure that we are dealing with a list from
15819 * "getmatches()". */
15820 li = l->lv_first;
15821 while (li != NULL)
15822 {
15823 if (li->li_tv.v_type != VAR_DICT
15824 || (d = li->li_tv.vval.v_dict) == NULL)
15825 {
15826 EMSG(_(e_invarg));
15827 return;
15828 }
15829 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15830 && dict_find(d, (char_u *)"pattern", -1) != NULL
15831 && dict_find(d, (char_u *)"priority", -1) != NULL
15832 && dict_find(d, (char_u *)"id", -1) != NULL))
15833 {
15834 EMSG(_(e_invarg));
15835 return;
15836 }
15837 li = li->li_next;
15838 }
15839
15840 clear_matches(curwin);
15841 li = l->lv_first;
15842 while (li != NULL)
15843 {
15844 d = li->li_tv.vval.v_dict;
15845 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15846 get_dict_string(d, (char_u *)"pattern", FALSE),
15847 (int)get_dict_number(d, (char_u *)"priority"),
15848 (int)get_dict_number(d, (char_u *)"id"));
15849 li = li->li_next;
15850 }
15851 rettv->vval.v_number = 0;
15852 }
15853#endif
15854}
15855
15856/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015857 * "setpos()" function
15858 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015859 static void
15860f_setpos(argvars, rettv)
15861 typval_T *argvars;
15862 typval_T *rettv;
15863{
15864 pos_T pos;
15865 int fnum;
15866 char_u *name;
15867
Bram Moolenaar08250432008-02-13 11:42:46 +000015868 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015869 name = get_tv_string_chk(argvars);
15870 if (name != NULL)
15871 {
15872 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15873 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015874 if (--pos.col < 0)
15875 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015876 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015877 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015878 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015879 if (fnum == curbuf->b_fnum)
15880 {
15881 curwin->w_cursor = pos;
15882 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015883 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015884 }
15885 else
15886 EMSG(_(e_invarg));
15887 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015888 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15889 {
15890 /* set mark */
15891 if (setmark_pos(name[1], &pos, fnum) == OK)
15892 rettv->vval.v_number = 0;
15893 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015894 else
15895 EMSG(_(e_invarg));
15896 }
15897 }
15898}
15899
15900/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015901 * "setqflist()" function
15902 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015903 static void
15904f_setqflist(argvars, rettv)
15905 typval_T *argvars;
15906 typval_T *rettv;
15907{
15908 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15909}
15910
15911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015912 * "setreg()" function
15913 */
15914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015915f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015916 typval_T *argvars;
15917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918{
15919 int regname;
15920 char_u *strregname;
15921 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015922 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015923 int append;
15924 char_u yank_type;
15925 long block_len;
15926
15927 block_len = -1;
15928 yank_type = MAUTO;
15929 append = FALSE;
15930
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015931 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015932 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015934 if (strregname == NULL)
15935 return; /* type error; errmsg already given */
15936 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937 if (regname == 0 || regname == '@')
15938 regname = '"';
15939 else if (regname == '=')
15940 return;
15941
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015942 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015944 stropt = get_tv_string_chk(&argvars[2]);
15945 if (stropt == NULL)
15946 return; /* type error */
15947 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948 switch (*stropt)
15949 {
15950 case 'a': case 'A': /* append */
15951 append = TRUE;
15952 break;
15953 case 'v': case 'c': /* character-wise selection */
15954 yank_type = MCHAR;
15955 break;
15956 case 'V': case 'l': /* line-wise selection */
15957 yank_type = MLINE;
15958 break;
15959#ifdef FEAT_VISUAL
15960 case 'b': case Ctrl_V: /* block-wise selection */
15961 yank_type = MBLOCK;
15962 if (VIM_ISDIGIT(stropt[1]))
15963 {
15964 ++stropt;
15965 block_len = getdigits(&stropt) - 1;
15966 --stropt;
15967 }
15968 break;
15969#endif
15970 }
15971 }
15972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015973 strval = get_tv_string_chk(&argvars[1]);
15974 if (strval != NULL)
15975 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015976 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015977 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978}
15979
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015980/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020015981 * "settabvar()" function
15982 */
15983 static void
15984f_settabvar(argvars, rettv)
15985 typval_T *argvars;
15986 typval_T *rettv;
15987{
15988 tabpage_T *save_curtab;
15989 char_u *varname, *tabvarname;
15990 typval_T *varp;
15991 tabpage_T *tp;
15992
15993 rettv->vval.v_number = 0;
15994
15995 if (check_restricted() || check_secure())
15996 return;
15997
15998 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15999 varname = get_tv_string_chk(&argvars[1]);
16000 varp = &argvars[2];
16001
16002 if (tp != NULL && varname != NULL && varp != NULL)
16003 {
16004 save_curtab = curtab;
16005 goto_tabpage_tp(tp);
16006
16007 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16008 if (tabvarname != NULL)
16009 {
16010 STRCPY(tabvarname, "t:");
16011 STRCPY(tabvarname + 2, varname);
16012 set_var(tabvarname, varp, TRUE);
16013 vim_free(tabvarname);
16014 }
16015
16016 /* Restore current tabpage */
16017 if (valid_tabpage(save_curtab))
16018 goto_tabpage_tp(save_curtab);
16019 }
16020}
16021
16022/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016023 * "settabwinvar()" function
16024 */
16025 static void
16026f_settabwinvar(argvars, rettv)
16027 typval_T *argvars;
16028 typval_T *rettv;
16029{
16030 setwinvar(argvars, rettv, 1);
16031}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032
16033/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016034 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016037f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016038 typval_T *argvars;
16039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016041 setwinvar(argvars, rettv, 0);
16042}
16043
16044/*
16045 * "setwinvar()" and "settabwinvar()" functions
16046 */
16047 static void
16048setwinvar(argvars, rettv, off)
16049 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016050 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016051 int off;
16052{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 win_T *win;
16054#ifdef FEAT_WINDOWS
16055 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016056 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057#endif
16058 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016059 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016061 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062
16063 if (check_restricted() || check_secure())
16064 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016065
16066#ifdef FEAT_WINDOWS
16067 if (off == 1)
16068 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16069 else
16070 tp = curtab;
16071#endif
16072 win = find_win_by_nr(&argvars[off], tp);
16073 varname = get_tv_string_chk(&argvars[off + 1]);
16074 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075
16076 if (win != NULL && varname != NULL && varp != NULL)
16077 {
16078#ifdef FEAT_WINDOWS
16079 /* set curwin to be our win, temporarily */
16080 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016081 save_curtab = curtab;
16082 goto_tabpage_tp(tp);
16083 if (!win_valid(win))
16084 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 curwin = win;
16086 curbuf = curwin->w_buffer;
16087#endif
16088
16089 if (*varname == '&')
16090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016091 long numval;
16092 char_u *strval;
16093 int error = FALSE;
16094
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016096 numval = get_tv_number_chk(varp, &error);
16097 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016098 if (!error && strval != NULL)
16099 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 }
16101 else
16102 {
16103 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16104 if (winvarname != NULL)
16105 {
16106 STRCPY(winvarname, "w:");
16107 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016108 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 vim_free(winvarname);
16110 }
16111 }
16112
16113#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016114 /* Restore current tabpage and window, if still valid (autocomands can
16115 * make them invalid). */
16116 if (valid_tabpage(save_curtab))
16117 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 if (win_valid(save_curwin))
16119 {
16120 curwin = save_curwin;
16121 curbuf = curwin->w_buffer;
16122 }
16123#endif
16124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125}
16126
16127/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016128 * "shellescape({string})" function
16129 */
16130 static void
16131f_shellescape(argvars, rettv)
16132 typval_T *argvars;
16133 typval_T *rettv;
16134{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016135 rettv->vval.v_string = vim_strsave_shellescape(
16136 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016137 rettv->v_type = VAR_STRING;
16138}
16139
16140/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016141 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 */
16143 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016144f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016145 typval_T *argvars;
16146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016148 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149
Bram Moolenaar0d660222005-01-07 21:51:51 +000016150 p = get_tv_string(&argvars[0]);
16151 rettv->vval.v_string = vim_strsave(p);
16152 simplify_filename(rettv->vval.v_string); /* simplify in place */
16153 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154}
16155
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016156#ifdef FEAT_FLOAT
16157/*
16158 * "sin()" function
16159 */
16160 static void
16161f_sin(argvars, rettv)
16162 typval_T *argvars;
16163 typval_T *rettv;
16164{
16165 float_T f;
16166
16167 rettv->v_type = VAR_FLOAT;
16168 if (get_float_arg(argvars, &f) == OK)
16169 rettv->vval.v_float = sin(f);
16170 else
16171 rettv->vval.v_float = 0.0;
16172}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016173
16174/*
16175 * "sinh()" function
16176 */
16177 static void
16178f_sinh(argvars, rettv)
16179 typval_T *argvars;
16180 typval_T *rettv;
16181{
16182 float_T f;
16183
16184 rettv->v_type = VAR_FLOAT;
16185 if (get_float_arg(argvars, &f) == OK)
16186 rettv->vval.v_float = sinh(f);
16187 else
16188 rettv->vval.v_float = 0.0;
16189}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016190#endif
16191
Bram Moolenaar0d660222005-01-07 21:51:51 +000016192static int
16193#ifdef __BORLANDC__
16194 _RTLENTRYF
16195#endif
16196 item_compare __ARGS((const void *s1, const void *s2));
16197static int
16198#ifdef __BORLANDC__
16199 _RTLENTRYF
16200#endif
16201 item_compare2 __ARGS((const void *s1, const void *s2));
16202
16203static int item_compare_ic;
16204static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016205static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016206#define ITEM_COMPARE_FAIL 999
16207
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016209 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016211 static int
16212#ifdef __BORLANDC__
16213_RTLENTRYF
16214#endif
16215item_compare(s1, s2)
16216 const void *s1;
16217 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219 char_u *p1, *p2;
16220 char_u *tofree1, *tofree2;
16221 int res;
16222 char_u numbuf1[NUMBUFLEN];
16223 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016225 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16226 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016227 if (p1 == NULL)
16228 p1 = (char_u *)"";
16229 if (p2 == NULL)
16230 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016231 if (item_compare_ic)
16232 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016234 res = STRCMP(p1, p2);
16235 vim_free(tofree1);
16236 vim_free(tofree2);
16237 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238}
16239
16240 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016241#ifdef __BORLANDC__
16242_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244item_compare2(s1, s2)
16245 const void *s1;
16246 const void *s2;
16247{
16248 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016249 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016250 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016251 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016253 /* shortcut after failure in previous call; compare all items equal */
16254 if (item_compare_func_err)
16255 return 0;
16256
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016257 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16258 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016259 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16260 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016261
16262 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016263 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016264 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016265 clear_tv(&argv[0]);
16266 clear_tv(&argv[1]);
16267
16268 if (res == FAIL)
16269 res = ITEM_COMPARE_FAIL;
16270 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016271 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16272 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016273 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016274 clear_tv(&rettv);
16275 return res;
16276}
16277
16278/*
16279 * "sort({list})" function
16280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016283 typval_T *argvars;
16284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285{
Bram Moolenaar33570922005-01-25 22:26:29 +000016286 list_T *l;
16287 listitem_T *li;
16288 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016289 long len;
16290 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291
Bram Moolenaar0d660222005-01-07 21:51:51 +000016292 if (argvars[0].v_type != VAR_LIST)
16293 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 else
16295 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016296 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016297 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016298 return;
16299 rettv->vval.v_list = l;
16300 rettv->v_type = VAR_LIST;
16301 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302
Bram Moolenaar0d660222005-01-07 21:51:51 +000016303 len = list_len(l);
16304 if (len <= 1)
16305 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306
Bram Moolenaar0d660222005-01-07 21:51:51 +000016307 item_compare_ic = FALSE;
16308 item_compare_func = NULL;
16309 if (argvars[1].v_type != VAR_UNKNOWN)
16310 {
16311 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016312 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016313 else
16314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016315 int error = FALSE;
16316
16317 i = get_tv_number_chk(&argvars[1], &error);
16318 if (error)
16319 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016320 if (i == 1)
16321 item_compare_ic = TRUE;
16322 else
16323 item_compare_func = get_tv_string(&argvars[1]);
16324 }
16325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326
Bram Moolenaar0d660222005-01-07 21:51:51 +000016327 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016328 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016329 if (ptrs == NULL)
16330 return;
16331 i = 0;
16332 for (li = l->lv_first; li != NULL; li = li->li_next)
16333 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016335 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016336 /* test the compare function */
16337 if (item_compare_func != NULL
16338 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16339 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016340 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016342 {
16343 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016344 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016345 item_compare_func == NULL ? item_compare : item_compare2);
16346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016347 if (!item_compare_func_err)
16348 {
16349 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016350 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016351 l->lv_len = 0;
16352 for (i = 0; i < len; ++i)
16353 list_append(l, ptrs[i]);
16354 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016355 }
16356
16357 vim_free(ptrs);
16358 }
16359}
16360
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016361/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016362 * "soundfold({word})" function
16363 */
16364 static void
16365f_soundfold(argvars, rettv)
16366 typval_T *argvars;
16367 typval_T *rettv;
16368{
16369 char_u *s;
16370
16371 rettv->v_type = VAR_STRING;
16372 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016373#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016374 rettv->vval.v_string = eval_soundfold(s);
16375#else
16376 rettv->vval.v_string = vim_strsave(s);
16377#endif
16378}
16379
16380/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016381 * "spellbadword()" function
16382 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016383 static void
16384f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016385 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016386 typval_T *rettv;
16387{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016388 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016389 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016390 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016391
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016392 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016393 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016394
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016395#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016396 if (argvars[0].v_type == VAR_UNKNOWN)
16397 {
16398 /* Find the start and length of the badly spelled word. */
16399 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16400 if (len != 0)
16401 word = ml_get_cursor();
16402 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016403 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016404 {
16405 char_u *str = get_tv_string_chk(&argvars[0]);
16406 int capcol = -1;
16407
16408 if (str != NULL)
16409 {
16410 /* Check the argument for spelling. */
16411 while (*str != NUL)
16412 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016413 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016414 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016415 {
16416 word = str;
16417 break;
16418 }
16419 str += len;
16420 }
16421 }
16422 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016423#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016424
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016425 list_append_string(rettv->vval.v_list, word, len);
16426 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016427 attr == HLF_SPB ? "bad" :
16428 attr == HLF_SPR ? "rare" :
16429 attr == HLF_SPL ? "local" :
16430 attr == HLF_SPC ? "caps" :
16431 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016432}
16433
16434/*
16435 * "spellsuggest()" function
16436 */
16437 static void
16438f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016439 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016440 typval_T *rettv;
16441{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016442#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016443 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016444 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016445 int maxcount;
16446 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016447 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016448 listitem_T *li;
16449 int need_capital = FALSE;
16450#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016451
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016452 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016453 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016454
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016455#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016456 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016457 {
16458 str = get_tv_string(&argvars[0]);
16459 if (argvars[1].v_type != VAR_UNKNOWN)
16460 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016461 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016462 if (maxcount <= 0)
16463 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016464 if (argvars[2].v_type != VAR_UNKNOWN)
16465 {
16466 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16467 if (typeerr)
16468 return;
16469 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016470 }
16471 else
16472 maxcount = 25;
16473
Bram Moolenaar4770d092006-01-12 23:22:24 +000016474 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016475
16476 for (i = 0; i < ga.ga_len; ++i)
16477 {
16478 str = ((char_u **)ga.ga_data)[i];
16479
16480 li = listitem_alloc();
16481 if (li == NULL)
16482 vim_free(str);
16483 else
16484 {
16485 li->li_tv.v_type = VAR_STRING;
16486 li->li_tv.v_lock = 0;
16487 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016488 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016489 }
16490 }
16491 ga_clear(&ga);
16492 }
16493#endif
16494}
16495
Bram Moolenaar0d660222005-01-07 21:51:51 +000016496 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016497f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016498 typval_T *argvars;
16499 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500{
16501 char_u *str;
16502 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016503 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016504 regmatch_T regmatch;
16505 char_u patbuf[NUMBUFLEN];
16506 char_u *save_cpo;
16507 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016508 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016509 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016510 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016511
16512 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16513 save_cpo = p_cpo;
16514 p_cpo = (char_u *)"";
16515
16516 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016517 if (argvars[1].v_type != VAR_UNKNOWN)
16518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016519 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16520 if (pat == NULL)
16521 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016522 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016523 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016524 }
16525 if (pat == NULL || *pat == NUL)
16526 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016527
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016528 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016530 if (typeerr)
16531 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532
Bram Moolenaar0d660222005-01-07 21:51:51 +000016533 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16534 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016536 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016537 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016538 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016539 if (*str == NUL)
16540 match = FALSE; /* empty item at the end */
16541 else
16542 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016543 if (match)
16544 end = regmatch.startp[0];
16545 else
16546 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016547 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16548 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016549 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016550 if (list_append_string(rettv->vval.v_list, str,
16551 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016552 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016553 }
16554 if (!match)
16555 break;
16556 /* Advance to just after the match. */
16557 if (regmatch.endp[0] > str)
16558 col = 0;
16559 else
16560 {
16561 /* Don't get stuck at the same match. */
16562#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016563 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016564#else
16565 col = 1;
16566#endif
16567 }
16568 str = regmatch.endp[0];
16569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570
Bram Moolenaar0d660222005-01-07 21:51:51 +000016571 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573
Bram Moolenaar0d660222005-01-07 21:51:51 +000016574 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575}
16576
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016577#ifdef FEAT_FLOAT
16578/*
16579 * "sqrt()" function
16580 */
16581 static void
16582f_sqrt(argvars, rettv)
16583 typval_T *argvars;
16584 typval_T *rettv;
16585{
16586 float_T f;
16587
16588 rettv->v_type = VAR_FLOAT;
16589 if (get_float_arg(argvars, &f) == OK)
16590 rettv->vval.v_float = sqrt(f);
16591 else
16592 rettv->vval.v_float = 0.0;
16593}
16594
16595/*
16596 * "str2float()" function
16597 */
16598 static void
16599f_str2float(argvars, rettv)
16600 typval_T *argvars;
16601 typval_T *rettv;
16602{
16603 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16604
16605 if (*p == '+')
16606 p = skipwhite(p + 1);
16607 (void)string2float(p, &rettv->vval.v_float);
16608 rettv->v_type = VAR_FLOAT;
16609}
16610#endif
16611
Bram Moolenaar2c932302006-03-18 21:42:09 +000016612/*
16613 * "str2nr()" function
16614 */
16615 static void
16616f_str2nr(argvars, rettv)
16617 typval_T *argvars;
16618 typval_T *rettv;
16619{
16620 int base = 10;
16621 char_u *p;
16622 long n;
16623
16624 if (argvars[1].v_type != VAR_UNKNOWN)
16625 {
16626 base = get_tv_number(&argvars[1]);
16627 if (base != 8 && base != 10 && base != 16)
16628 {
16629 EMSG(_(e_invarg));
16630 return;
16631 }
16632 }
16633
16634 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016635 if (*p == '+')
16636 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016637 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16638 rettv->vval.v_number = n;
16639}
16640
Bram Moolenaar071d4272004-06-13 20:20:40 +000016641#ifdef HAVE_STRFTIME
16642/*
16643 * "strftime({format}[, {time}])" function
16644 */
16645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016646f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016647 typval_T *argvars;
16648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649{
16650 char_u result_buf[256];
16651 struct tm *curtime;
16652 time_t seconds;
16653 char_u *p;
16654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016655 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016657 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016658 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659 seconds = time(NULL);
16660 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016661 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662 curtime = localtime(&seconds);
16663 /* MSVC returns NULL for an invalid value of seconds. */
16664 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016665 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666 else
16667 {
16668# ifdef FEAT_MBYTE
16669 vimconv_T conv;
16670 char_u *enc;
16671
16672 conv.vc_type = CONV_NONE;
16673 enc = enc_locale();
16674 convert_setup(&conv, p_enc, enc);
16675 if (conv.vc_type != CONV_NONE)
16676 p = string_convert(&conv, p, NULL);
16677# endif
16678 if (p != NULL)
16679 (void)strftime((char *)result_buf, sizeof(result_buf),
16680 (char *)p, curtime);
16681 else
16682 result_buf[0] = NUL;
16683
16684# ifdef FEAT_MBYTE
16685 if (conv.vc_type != CONV_NONE)
16686 vim_free(p);
16687 convert_setup(&conv, enc, p_enc);
16688 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016689 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016690 else
16691# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016692 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693
16694# ifdef FEAT_MBYTE
16695 /* Release conversion descriptors */
16696 convert_setup(&conv, NULL, NULL);
16697 vim_free(enc);
16698# endif
16699 }
16700}
16701#endif
16702
16703/*
16704 * "stridx()" function
16705 */
16706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016707f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016708 typval_T *argvars;
16709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710{
16711 char_u buf[NUMBUFLEN];
16712 char_u *needle;
16713 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016714 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016716 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016718 needle = get_tv_string_chk(&argvars[1]);
16719 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016720 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016721 if (needle == NULL || haystack == NULL)
16722 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723
Bram Moolenaar33570922005-01-25 22:26:29 +000016724 if (argvars[2].v_type != VAR_UNKNOWN)
16725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016726 int error = FALSE;
16727
16728 start_idx = get_tv_number_chk(&argvars[2], &error);
16729 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016730 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016731 if (start_idx >= 0)
16732 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016733 }
16734
16735 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16736 if (pos != NULL)
16737 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738}
16739
16740/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016741 * "string()" function
16742 */
16743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016744f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016745 typval_T *argvars;
16746 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016747{
16748 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016749 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016751 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016752 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016753 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016754 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016755 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756}
16757
16758/*
16759 * "strlen()" function
16760 */
16761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016762f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016763 typval_T *argvars;
16764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016766 rettv->vval.v_number = (varnumber_T)(STRLEN(
16767 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768}
16769
16770/*
16771 * "strpart()" function
16772 */
16773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016774f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016775 typval_T *argvars;
16776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777{
16778 char_u *p;
16779 int n;
16780 int len;
16781 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016782 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016784 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785 slen = (int)STRLEN(p);
16786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016787 n = get_tv_number_chk(&argvars[1], &error);
16788 if (error)
16789 len = 0;
16790 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016791 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 else
16793 len = slen - n; /* default len: all bytes that are available. */
16794
16795 /*
16796 * Only return the overlap between the specified part and the actual
16797 * string.
16798 */
16799 if (n < 0)
16800 {
16801 len += n;
16802 n = 0;
16803 }
16804 else if (n > slen)
16805 n = slen;
16806 if (len < 0)
16807 len = 0;
16808 else if (n + len > slen)
16809 len = slen - n;
16810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016811 rettv->v_type = VAR_STRING;
16812 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813}
16814
16815/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016816 * "strridx()" function
16817 */
16818 static void
16819f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016820 typval_T *argvars;
16821 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016822{
16823 char_u buf[NUMBUFLEN];
16824 char_u *needle;
16825 char_u *haystack;
16826 char_u *rest;
16827 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016828 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016830 needle = get_tv_string_chk(&argvars[1]);
16831 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016832
16833 rettv->vval.v_number = -1;
16834 if (needle == NULL || haystack == NULL)
16835 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016836
16837 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016838 if (argvars[2].v_type != VAR_UNKNOWN)
16839 {
16840 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016841 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016842 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016843 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016844 }
16845 else
16846 end_idx = haystack_len;
16847
Bram Moolenaar0d660222005-01-07 21:51:51 +000016848 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016849 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016850 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016851 lastmatch = haystack + end_idx;
16852 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016853 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016854 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016855 for (rest = haystack; *rest != '\0'; ++rest)
16856 {
16857 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016858 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016859 break;
16860 lastmatch = rest;
16861 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016862 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016863
16864 if (lastmatch == NULL)
16865 rettv->vval.v_number = -1;
16866 else
16867 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16868}
16869
16870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 * "strtrans()" function
16872 */
16873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016874f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016875 typval_T *argvars;
16876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016878 rettv->v_type = VAR_STRING;
16879 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880}
16881
16882/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016883 * "submatch()" function
16884 */
16885 static void
16886f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016887 typval_T *argvars;
16888 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016889{
16890 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016891 rettv->vval.v_string =
16892 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016893}
16894
16895/*
16896 * "substitute()" function
16897 */
16898 static void
16899f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016900 typval_T *argvars;
16901 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016902{
16903 char_u patbuf[NUMBUFLEN];
16904 char_u subbuf[NUMBUFLEN];
16905 char_u flagsbuf[NUMBUFLEN];
16906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016907 char_u *str = get_tv_string_chk(&argvars[0]);
16908 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16909 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16910 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16911
Bram Moolenaar0d660222005-01-07 21:51:51 +000016912 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016913 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16914 rettv->vval.v_string = NULL;
16915 else
16916 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016917}
16918
16919/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016920 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016923f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016924 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926{
16927 int id = 0;
16928#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016929 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 long col;
16931 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016932 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016934 lnum = get_tv_lnum(argvars); /* -1 on type error */
16935 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16936 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016938 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016939 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016940 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941#endif
16942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016943 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944}
16945
16946/*
16947 * "synIDattr(id, what [, mode])" function
16948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016950f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016951 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953{
16954 char_u *p = NULL;
16955#ifdef FEAT_SYN_HL
16956 int id;
16957 char_u *what;
16958 char_u *mode;
16959 char_u modebuf[NUMBUFLEN];
16960 int modec;
16961
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016962 id = get_tv_number(&argvars[0]);
16963 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016964 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016966 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 modec = TOLOWER_ASC(mode[0]);
16968 if (modec != 't' && modec != 'c'
16969#ifdef FEAT_GUI
16970 && modec != 'g'
16971#endif
16972 )
16973 modec = 0; /* replace invalid with current */
16974 }
16975 else
16976 {
16977#ifdef FEAT_GUI
16978 if (gui.in_use)
16979 modec = 'g';
16980 else
16981#endif
16982 if (t_colors > 1)
16983 modec = 'c';
16984 else
16985 modec = 't';
16986 }
16987
16988
16989 switch (TOLOWER_ASC(what[0]))
16990 {
16991 case 'b':
16992 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16993 p = highlight_color(id, what, modec);
16994 else /* bold */
16995 p = highlight_has_attr(id, HL_BOLD, modec);
16996 break;
16997
Bram Moolenaar12682fd2010-03-10 13:43:49 +010016998 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 p = highlight_color(id, what, modec);
17000 break;
17001
17002 case 'i':
17003 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17004 p = highlight_has_attr(id, HL_INVERSE, modec);
17005 else /* italic */
17006 p = highlight_has_attr(id, HL_ITALIC, modec);
17007 break;
17008
17009 case 'n': /* name */
17010 p = get_highlight_name(NULL, id - 1);
17011 break;
17012
17013 case 'r': /* reverse */
17014 p = highlight_has_attr(id, HL_INVERSE, modec);
17015 break;
17016
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017017 case 's':
17018 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17019 p = highlight_color(id, what, modec);
17020 else /* standout */
17021 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 break;
17023
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017024 case 'u':
17025 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17026 /* underline */
17027 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17028 else
17029 /* undercurl */
17030 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031 break;
17032 }
17033
17034 if (p != NULL)
17035 p = vim_strsave(p);
17036#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017037 rettv->v_type = VAR_STRING;
17038 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039}
17040
17041/*
17042 * "synIDtrans(id)" function
17043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017045f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017046 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048{
17049 int id;
17050
17051#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017052 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053
17054 if (id > 0)
17055 id = syn_get_final_id(id);
17056 else
17057#endif
17058 id = 0;
17059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017060 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061}
17062
17063/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017064 * "synstack(lnum, col)" function
17065 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017066 static void
17067f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017068 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017069 typval_T *rettv;
17070{
17071#ifdef FEAT_SYN_HL
17072 long lnum;
17073 long col;
17074 int i;
17075 int id;
17076#endif
17077
17078 rettv->v_type = VAR_LIST;
17079 rettv->vval.v_list = NULL;
17080
17081#ifdef FEAT_SYN_HL
17082 lnum = get_tv_lnum(argvars); /* -1 on type error */
17083 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17084
17085 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000017086 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017087 && rettv_list_alloc(rettv) != FAIL)
17088 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017089 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017090 for (i = 0; ; ++i)
17091 {
17092 id = syn_get_stack_item(i);
17093 if (id < 0)
17094 break;
17095 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17096 break;
17097 }
17098 }
17099#endif
17100}
17101
17102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103 * "system()" function
17104 */
17105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017106f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017107 typval_T *argvars;
17108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017110 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017112 char_u *infile = NULL;
17113 char_u buf[NUMBUFLEN];
17114 int err = FALSE;
17115 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017116
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017117 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017118 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017119
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017120 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017121 {
17122 /*
17123 * Write the string to a temp file, to be used for input of the shell
17124 * command.
17125 */
17126 if ((infile = vim_tempname('i')) == NULL)
17127 {
17128 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017129 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017130 }
17131
17132 fd = mch_fopen((char *)infile, WRITEBIN);
17133 if (fd == NULL)
17134 {
17135 EMSG2(_(e_notopen), infile);
17136 goto done;
17137 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017138 p = get_tv_string_buf_chk(&argvars[1], buf);
17139 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017140 {
17141 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017142 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017143 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017144 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17145 err = TRUE;
17146 if (fclose(fd) != 0)
17147 err = TRUE;
17148 if (err)
17149 {
17150 EMSG(_("E677: Error writing temp file"));
17151 goto done;
17152 }
17153 }
17154
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017155 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17156 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017157
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158#ifdef USE_CR
17159 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017160 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161 {
17162 char_u *s;
17163
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017164 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165 {
17166 if (*s == CAR)
17167 *s = NL;
17168 }
17169 }
17170#else
17171# ifdef USE_CRNL
17172 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017173 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 {
17175 char_u *s, *d;
17176
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017177 d = res;
17178 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 {
17180 if (s[0] == CAR && s[1] == NL)
17181 ++s;
17182 *d++ = *s;
17183 }
17184 *d = NUL;
17185 }
17186# endif
17187#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017188
17189done:
17190 if (infile != NULL)
17191 {
17192 mch_remove(infile);
17193 vim_free(infile);
17194 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017195 rettv->v_type = VAR_STRING;
17196 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197}
17198
17199/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017200 * "tabpagebuflist()" function
17201 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017202 static void
17203f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017204 typval_T *argvars UNUSED;
17205 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017206{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017207#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017208 tabpage_T *tp;
17209 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017210
17211 if (argvars[0].v_type == VAR_UNKNOWN)
17212 wp = firstwin;
17213 else
17214 {
17215 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17216 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017217 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017218 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017219 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017220 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017221 for (; wp != NULL; wp = wp->w_next)
17222 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017223 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017224 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017225 }
17226#endif
17227}
17228
17229
17230/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017231 * "tabpagenr()" function
17232 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017233 static void
17234f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017235 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017236 typval_T *rettv;
17237{
17238 int nr = 1;
17239#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017240 char_u *arg;
17241
17242 if (argvars[0].v_type != VAR_UNKNOWN)
17243 {
17244 arg = get_tv_string_chk(&argvars[0]);
17245 nr = 0;
17246 if (arg != NULL)
17247 {
17248 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017249 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017250 else
17251 EMSG2(_(e_invexpr2), arg);
17252 }
17253 }
17254 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017255 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017256#endif
17257 rettv->vval.v_number = nr;
17258}
17259
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017260
17261#ifdef FEAT_WINDOWS
17262static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17263
17264/*
17265 * Common code for tabpagewinnr() and winnr().
17266 */
17267 static int
17268get_winnr(tp, argvar)
17269 tabpage_T *tp;
17270 typval_T *argvar;
17271{
17272 win_T *twin;
17273 int nr = 1;
17274 win_T *wp;
17275 char_u *arg;
17276
17277 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17278 if (argvar->v_type != VAR_UNKNOWN)
17279 {
17280 arg = get_tv_string_chk(argvar);
17281 if (arg == NULL)
17282 nr = 0; /* type error; errmsg already given */
17283 else if (STRCMP(arg, "$") == 0)
17284 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17285 else if (STRCMP(arg, "#") == 0)
17286 {
17287 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17288 if (twin == NULL)
17289 nr = 0;
17290 }
17291 else
17292 {
17293 EMSG2(_(e_invexpr2), arg);
17294 nr = 0;
17295 }
17296 }
17297
17298 if (nr > 0)
17299 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17300 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017301 {
17302 if (wp == NULL)
17303 {
17304 /* didn't find it in this tabpage */
17305 nr = 0;
17306 break;
17307 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017308 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017309 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017310 return nr;
17311}
17312#endif
17313
17314/*
17315 * "tabpagewinnr()" function
17316 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017317 static void
17318f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017319 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017320 typval_T *rettv;
17321{
17322 int nr = 1;
17323#ifdef FEAT_WINDOWS
17324 tabpage_T *tp;
17325
17326 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17327 if (tp == NULL)
17328 nr = 0;
17329 else
17330 nr = get_winnr(tp, &argvars[1]);
17331#endif
17332 rettv->vval.v_number = nr;
17333}
17334
17335
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017336/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017337 * "tagfiles()" function
17338 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017339 static void
17340f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017341 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017342 typval_T *rettv;
17343{
17344 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017345 tagname_T tn;
17346 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017347
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017348 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017349 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017350
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017351 for (first = TRUE; ; first = FALSE)
17352 if (get_tagfname(&tn, first, fname) == FAIL
17353 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017354 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017355 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017356}
17357
17358/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017359 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017360 */
17361 static void
17362f_taglist(argvars, rettv)
17363 typval_T *argvars;
17364 typval_T *rettv;
17365{
17366 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017367
17368 tag_pattern = get_tv_string(&argvars[0]);
17369
17370 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017371 if (*tag_pattern == NUL)
17372 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017373
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017374 if (rettv_list_alloc(rettv) == OK)
17375 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017376}
17377
17378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 * "tempname()" function
17380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017382f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017383 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385{
17386 static int x = 'A';
17387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388 rettv->v_type = VAR_STRING;
17389 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390
17391 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17392 * names. Skip 'I' and 'O', they are used for shell redirection. */
17393 do
17394 {
17395 if (x == 'Z')
17396 x = '0';
17397 else if (x == '9')
17398 x = 'A';
17399 else
17400 {
17401#ifdef EBCDIC
17402 if (x == 'I')
17403 x = 'J';
17404 else if (x == 'R')
17405 x = 'S';
17406 else
17407#endif
17408 ++x;
17409 }
17410 } while (x == 'I' || x == 'O');
17411}
17412
17413/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017414 * "test(list)" function: Just checking the walls...
17415 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017416 static void
17417f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017418 typval_T *argvars UNUSED;
17419 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017420{
17421 /* Used for unit testing. Change the code below to your liking. */
17422#if 0
17423 listitem_T *li;
17424 list_T *l;
17425 char_u *bad, *good;
17426
17427 if (argvars[0].v_type != VAR_LIST)
17428 return;
17429 l = argvars[0].vval.v_list;
17430 if (l == NULL)
17431 return;
17432 li = l->lv_first;
17433 if (li == NULL)
17434 return;
17435 bad = get_tv_string(&li->li_tv);
17436 li = li->li_next;
17437 if (li == NULL)
17438 return;
17439 good = get_tv_string(&li->li_tv);
17440 rettv->vval.v_number = test_edit_score(bad, good);
17441#endif
17442}
17443
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017444#ifdef FEAT_FLOAT
17445/*
17446 * "tan()" function
17447 */
17448 static void
17449f_tan(argvars, rettv)
17450 typval_T *argvars;
17451 typval_T *rettv;
17452{
17453 float_T f;
17454
17455 rettv->v_type = VAR_FLOAT;
17456 if (get_float_arg(argvars, &f) == OK)
17457 rettv->vval.v_float = tan(f);
17458 else
17459 rettv->vval.v_float = 0.0;
17460}
17461
17462/*
17463 * "tanh()" function
17464 */
17465 static void
17466f_tanh(argvars, rettv)
17467 typval_T *argvars;
17468 typval_T *rettv;
17469{
17470 float_T f;
17471
17472 rettv->v_type = VAR_FLOAT;
17473 if (get_float_arg(argvars, &f) == OK)
17474 rettv->vval.v_float = tanh(f);
17475 else
17476 rettv->vval.v_float = 0.0;
17477}
17478#endif
17479
Bram Moolenaard52d9742005-08-21 22:20:28 +000017480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 * "tolower(string)" function
17482 */
17483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017484f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017485 typval_T *argvars;
17486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487{
17488 char_u *p;
17489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490 p = vim_strsave(get_tv_string(&argvars[0]));
17491 rettv->v_type = VAR_STRING;
17492 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493
17494 if (p != NULL)
17495 while (*p != NUL)
17496 {
17497#ifdef FEAT_MBYTE
17498 int l;
17499
17500 if (enc_utf8)
17501 {
17502 int c, lc;
17503
17504 c = utf_ptr2char(p);
17505 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017506 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 /* TODO: reallocate string when byte count changes. */
17508 if (utf_char2len(lc) == l)
17509 utf_char2bytes(lc, p);
17510 p += l;
17511 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017512 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513 p += l; /* skip multi-byte character */
17514 else
17515#endif
17516 {
17517 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17518 ++p;
17519 }
17520 }
17521}
17522
17523/*
17524 * "toupper(string)" function
17525 */
17526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017527f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017528 typval_T *argvars;
17529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017531 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017532 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533}
17534
17535/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017536 * "tr(string, fromstr, tostr)" function
17537 */
17538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017539f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017540 typval_T *argvars;
17541 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017542{
17543 char_u *instr;
17544 char_u *fromstr;
17545 char_u *tostr;
17546 char_u *p;
17547#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017548 int inlen;
17549 int fromlen;
17550 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017551 int idx;
17552 char_u *cpstr;
17553 int cplen;
17554 int first = TRUE;
17555#endif
17556 char_u buf[NUMBUFLEN];
17557 char_u buf2[NUMBUFLEN];
17558 garray_T ga;
17559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017560 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017561 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17562 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017563
17564 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017565 rettv->v_type = VAR_STRING;
17566 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017567 if (fromstr == NULL || tostr == NULL)
17568 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017569 ga_init2(&ga, (int)sizeof(char), 80);
17570
17571#ifdef FEAT_MBYTE
17572 if (!has_mbyte)
17573#endif
17574 /* not multi-byte: fromstr and tostr must be the same length */
17575 if (STRLEN(fromstr) != STRLEN(tostr))
17576 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017577#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017578error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017579#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017580 EMSG2(_(e_invarg2), fromstr);
17581 ga_clear(&ga);
17582 return;
17583 }
17584
17585 /* fromstr and tostr have to contain the same number of chars */
17586 while (*instr != NUL)
17587 {
17588#ifdef FEAT_MBYTE
17589 if (has_mbyte)
17590 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017591 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017592 cpstr = instr;
17593 cplen = inlen;
17594 idx = 0;
17595 for (p = fromstr; *p != NUL; p += fromlen)
17596 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017597 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017598 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17599 {
17600 for (p = tostr; *p != NUL; p += tolen)
17601 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017602 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017603 if (idx-- == 0)
17604 {
17605 cplen = tolen;
17606 cpstr = p;
17607 break;
17608 }
17609 }
17610 if (*p == NUL) /* tostr is shorter than fromstr */
17611 goto error;
17612 break;
17613 }
17614 ++idx;
17615 }
17616
17617 if (first && cpstr == instr)
17618 {
17619 /* Check that fromstr and tostr have the same number of
17620 * (multi-byte) characters. Done only once when a character
17621 * of instr doesn't appear in fromstr. */
17622 first = FALSE;
17623 for (p = tostr; *p != NUL; p += tolen)
17624 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017625 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017626 --idx;
17627 }
17628 if (idx != 0)
17629 goto error;
17630 }
17631
17632 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017633 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017634 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017635
17636 instr += inlen;
17637 }
17638 else
17639#endif
17640 {
17641 /* When not using multi-byte chars we can do it faster. */
17642 p = vim_strchr(fromstr, *instr);
17643 if (p != NULL)
17644 ga_append(&ga, tostr[p - fromstr]);
17645 else
17646 ga_append(&ga, *instr);
17647 ++instr;
17648 }
17649 }
17650
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017651 /* add a terminating NUL */
17652 ga_grow(&ga, 1);
17653 ga_append(&ga, NUL);
17654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017655 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017656}
17657
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017658#ifdef FEAT_FLOAT
17659/*
17660 * "trunc({float})" function
17661 */
17662 static void
17663f_trunc(argvars, rettv)
17664 typval_T *argvars;
17665 typval_T *rettv;
17666{
17667 float_T f;
17668
17669 rettv->v_type = VAR_FLOAT;
17670 if (get_float_arg(argvars, &f) == OK)
17671 /* trunc() is not in C90, use floor() or ceil() instead. */
17672 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17673 else
17674 rettv->vval.v_float = 0.0;
17675}
17676#endif
17677
Bram Moolenaar8299df92004-07-10 09:47:34 +000017678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 * "type(expr)" function
17680 */
17681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017682f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017683 typval_T *argvars;
17684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017686 int n;
17687
17688 switch (argvars[0].v_type)
17689 {
17690 case VAR_NUMBER: n = 0; break;
17691 case VAR_STRING: n = 1; break;
17692 case VAR_FUNC: n = 2; break;
17693 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017694 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017695#ifdef FEAT_FLOAT
17696 case VAR_FLOAT: n = 5; break;
17697#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017698 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17699 }
17700 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701}
17702
17703/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017704 * "undofile(name)" function
17705 */
17706 static void
17707f_undofile(argvars, rettv)
17708 typval_T *argvars;
17709 typval_T *rettv;
17710{
17711 rettv->v_type = VAR_STRING;
17712#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017713 {
17714 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17715
17716 if (ffname != NULL)
17717 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17718 vim_free(ffname);
17719 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017720#else
17721 rettv->vval.v_string = NULL;
17722#endif
17723}
17724
17725/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017726 * "undotree()" function
17727 */
17728 static void
17729f_undotree(argvars, rettv)
17730 typval_T *argvars UNUSED;
17731 typval_T *rettv;
17732{
17733 if (rettv_dict_alloc(rettv) == OK)
17734 {
17735 dict_T *dict = rettv->vval.v_dict;
17736 list_T *list;
17737
17738 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
17739 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17740 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
17741 dict_add_nr_str(dict, "save_last",
17742 (long)curbuf->b_u_last_save_nr, NULL);
17743 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
17744
17745 list = list_alloc();
17746 if (list != NULL)
17747 {
17748 u_eval_tree(curbuf->b_u_oldhead, list);
17749 dict_add_list(dict, "entries", list);
17750 }
17751 }
17752}
17753
17754/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017755 * "values(dict)" function
17756 */
17757 static void
17758f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017759 typval_T *argvars;
17760 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017761{
17762 dict_list(argvars, rettv, 1);
17763}
17764
17765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766 * "virtcol(string)" function
17767 */
17768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017769f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017770 typval_T *argvars;
17771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772{
17773 colnr_T vcol = 0;
17774 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017775 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017777 fp = var2fpos(&argvars[0], FALSE, &fnum);
17778 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17779 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780 {
17781 getvvcol(curwin, fp, NULL, NULL, &vcol);
17782 ++vcol;
17783 }
17784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017785 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786}
17787
17788/*
17789 * "visualmode()" function
17790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017792f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017793 typval_T *argvars UNUSED;
17794 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795{
17796#ifdef FEAT_VISUAL
17797 char_u str[2];
17798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017799 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800 str[0] = curbuf->b_visual_mode_eval;
17801 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017802 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803
17804 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017805 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807#endif
17808}
17809
17810/*
17811 * "winbufnr(nr)" function
17812 */
17813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017814f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017815 typval_T *argvars;
17816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817{
17818 win_T *wp;
17819
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017820 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017822 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017824 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825}
17826
17827/*
17828 * "wincol()" function
17829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017831f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017832 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834{
17835 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017836 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837}
17838
17839/*
17840 * "winheight(nr)" function
17841 */
17842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017843f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017844 typval_T *argvars;
17845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846{
17847 win_T *wp;
17848
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017849 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017851 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017853 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854}
17855
17856/*
17857 * "winline()" function
17858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017860f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017861 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863{
17864 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017865 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866}
17867
17868/*
17869 * "winnr()" function
17870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017872f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017873 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875{
17876 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017877
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017879 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017881 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882}
17883
17884/*
17885 * "winrestcmd()" function
17886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017888f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017889 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891{
17892#ifdef FEAT_WINDOWS
17893 win_T *wp;
17894 int winnr = 1;
17895 garray_T ga;
17896 char_u buf[50];
17897
17898 ga_init2(&ga, (int)sizeof(char), 70);
17899 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17900 {
17901 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17902 ga_concat(&ga, buf);
17903# ifdef FEAT_VERTSPLIT
17904 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17905 ga_concat(&ga, buf);
17906# endif
17907 ++winnr;
17908 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017909 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017911 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017913 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017915 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916}
17917
17918/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017919 * "winrestview()" function
17920 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017921 static void
17922f_winrestview(argvars, rettv)
17923 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017924 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017925{
17926 dict_T *dict;
17927
17928 if (argvars[0].v_type != VAR_DICT
17929 || (dict = argvars[0].vval.v_dict) == NULL)
17930 EMSG(_(e_invarg));
17931 else
17932 {
17933 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17934 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17935#ifdef FEAT_VIRTUALEDIT
17936 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17937#endif
17938 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017939 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017940
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017941 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017942#ifdef FEAT_DIFF
17943 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17944#endif
17945 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17946 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17947
17948 check_cursor();
17949 changed_cline_bef_curs();
17950 invalidate_botline();
17951 redraw_later(VALID);
17952
17953 if (curwin->w_topline == 0)
17954 curwin->w_topline = 1;
17955 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17956 curwin->w_topline = curbuf->b_ml.ml_line_count;
17957#ifdef FEAT_DIFF
17958 check_topfill(curwin, TRUE);
17959#endif
17960 }
17961}
17962
17963/*
17964 * "winsaveview()" function
17965 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017966 static void
17967f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017968 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017969 typval_T *rettv;
17970{
17971 dict_T *dict;
17972
Bram Moolenaara800b422010-06-27 01:15:55 +020017973 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017974 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020017975 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017976
17977 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17978 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17979#ifdef FEAT_VIRTUALEDIT
17980 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17981#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017982 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017983 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17984
17985 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17986#ifdef FEAT_DIFF
17987 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17988#endif
17989 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17990 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17991}
17992
17993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 * "winwidth(nr)" function
17995 */
17996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017997f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017998 typval_T *argvars;
17999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000{
18001 win_T *wp;
18002
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018003 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018005 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 else
18007#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018008 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018010 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011#endif
18012}
18013
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018015 * "writefile()" function
18016 */
18017 static void
18018f_writefile(argvars, rettv)
18019 typval_T *argvars;
18020 typval_T *rettv;
18021{
18022 int binary = FALSE;
18023 char_u *fname;
18024 FILE *fd;
18025 listitem_T *li;
18026 char_u *s;
18027 int ret = 0;
18028 int c;
18029
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018030 if (check_restricted() || check_secure())
18031 return;
18032
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018033 if (argvars[0].v_type != VAR_LIST)
18034 {
18035 EMSG2(_(e_listarg), "writefile()");
18036 return;
18037 }
18038 if (argvars[0].vval.v_list == NULL)
18039 return;
18040
18041 if (argvars[2].v_type != VAR_UNKNOWN
18042 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18043 binary = TRUE;
18044
18045 /* Always open the file in binary mode, library functions have a mind of
18046 * their own about CR-LF conversion. */
18047 fname = get_tv_string(&argvars[1]);
18048 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18049 {
18050 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18051 ret = -1;
18052 }
18053 else
18054 {
18055 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18056 li = li->li_next)
18057 {
18058 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18059 {
18060 if (*s == '\n')
18061 c = putc(NUL, fd);
18062 else
18063 c = putc(*s, fd);
18064 if (c == EOF)
18065 {
18066 ret = -1;
18067 break;
18068 }
18069 }
18070 if (!binary || li->li_next != NULL)
18071 if (putc('\n', fd) == EOF)
18072 {
18073 ret = -1;
18074 break;
18075 }
18076 if (ret < 0)
18077 {
18078 EMSG(_(e_write));
18079 break;
18080 }
18081 }
18082 fclose(fd);
18083 }
18084
18085 rettv->vval.v_number = ret;
18086}
18087
18088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018090 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 */
18092 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018093var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018094 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018095 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018096 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018098 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018100 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101
Bram Moolenaara5525202006-03-02 22:52:09 +000018102 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018103 if (varp->v_type == VAR_LIST)
18104 {
18105 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018106 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018107 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018108 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018109
18110 l = varp->vval.v_list;
18111 if (l == NULL)
18112 return NULL;
18113
18114 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018115 pos.lnum = list_find_nr(l, 0L, &error);
18116 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018117 return NULL; /* invalid line number */
18118
18119 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018120 pos.col = list_find_nr(l, 1L, &error);
18121 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018122 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018123 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018124
18125 /* We accept "$" for the column number: last column. */
18126 li = list_find(l, 1L);
18127 if (li != NULL && li->li_tv.v_type == VAR_STRING
18128 && li->li_tv.vval.v_string != NULL
18129 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18130 pos.col = len + 1;
18131
Bram Moolenaara5525202006-03-02 22:52:09 +000018132 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018133 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018134 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018135 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018136
Bram Moolenaara5525202006-03-02 22:52:09 +000018137#ifdef FEAT_VIRTUALEDIT
18138 /* Get the virtual offset. Defaults to zero. */
18139 pos.coladd = list_find_nr(l, 2L, &error);
18140 if (error)
18141 pos.coladd = 0;
18142#endif
18143
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018144 return &pos;
18145 }
18146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018147 name = get_tv_string_chk(varp);
18148 if (name == NULL)
18149 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018150 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018152#ifdef FEAT_VISUAL
18153 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18154 {
18155 if (VIsual_active)
18156 return &VIsual;
18157 return &curwin->w_cursor;
18158 }
18159#endif
18160 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018162 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18164 return NULL;
18165 return pp;
18166 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018167
18168#ifdef FEAT_VIRTUALEDIT
18169 pos.coladd = 0;
18170#endif
18171
Bram Moolenaar477933c2007-07-17 14:32:23 +000018172 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018173 {
18174 pos.col = 0;
18175 if (name[1] == '0') /* "w0": first visible line */
18176 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018177 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018178 pos.lnum = curwin->w_topline;
18179 return &pos;
18180 }
18181 else if (name[1] == '$') /* "w$": last visible line */
18182 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018183 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018184 pos.lnum = curwin->w_botline - 1;
18185 return &pos;
18186 }
18187 }
18188 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018190 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 {
18192 pos.lnum = curbuf->b_ml.ml_line_count;
18193 pos.col = 0;
18194 }
18195 else
18196 {
18197 pos.lnum = curwin->w_cursor.lnum;
18198 pos.col = (colnr_T)STRLEN(ml_get_curline());
18199 }
18200 return &pos;
18201 }
18202 return NULL;
18203}
18204
18205/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018206 * Convert list in "arg" into a position and optional file number.
18207 * When "fnump" is NULL there is no file number, only 3 items.
18208 * Note that the column is passed on as-is, the caller may want to decrement
18209 * it to use 1 for the first column.
18210 * Return FAIL when conversion is not possible, doesn't check the position for
18211 * validity.
18212 */
18213 static int
18214list2fpos(arg, posp, fnump)
18215 typval_T *arg;
18216 pos_T *posp;
18217 int *fnump;
18218{
18219 list_T *l = arg->vval.v_list;
18220 long i = 0;
18221 long n;
18222
Bram Moolenaarbde35262006-07-23 20:12:24 +000018223 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18224 * when "fnump" isn't NULL and "coladd" is optional. */
18225 if (arg->v_type != VAR_LIST
18226 || l == NULL
18227 || l->lv_len < (fnump == NULL ? 2 : 3)
18228 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018229 return FAIL;
18230
18231 if (fnump != NULL)
18232 {
18233 n = list_find_nr(l, i++, NULL); /* fnum */
18234 if (n < 0)
18235 return FAIL;
18236 if (n == 0)
18237 n = curbuf->b_fnum; /* current buffer */
18238 *fnump = n;
18239 }
18240
18241 n = list_find_nr(l, i++, NULL); /* lnum */
18242 if (n < 0)
18243 return FAIL;
18244 posp->lnum = n;
18245
18246 n = list_find_nr(l, i++, NULL); /* col */
18247 if (n < 0)
18248 return FAIL;
18249 posp->col = n;
18250
18251#ifdef FEAT_VIRTUALEDIT
18252 n = list_find_nr(l, i, NULL);
18253 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018254 posp->coladd = 0;
18255 else
18256 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018257#endif
18258
18259 return OK;
18260}
18261
18262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 * Get the length of an environment variable name.
18264 * Advance "arg" to the first character after the name.
18265 * Return 0 for error.
18266 */
18267 static int
18268get_env_len(arg)
18269 char_u **arg;
18270{
18271 char_u *p;
18272 int len;
18273
18274 for (p = *arg; vim_isIDc(*p); ++p)
18275 ;
18276 if (p == *arg) /* no name found */
18277 return 0;
18278
18279 len = (int)(p - *arg);
18280 *arg = p;
18281 return len;
18282}
18283
18284/*
18285 * Get the length of the name of a function or internal variable.
18286 * "arg" is advanced to the first non-white character after the name.
18287 * Return 0 if something is wrong.
18288 */
18289 static int
18290get_id_len(arg)
18291 char_u **arg;
18292{
18293 char_u *p;
18294 int len;
18295
18296 /* Find the end of the name. */
18297 for (p = *arg; eval_isnamec(*p); ++p)
18298 ;
18299 if (p == *arg) /* no name found */
18300 return 0;
18301
18302 len = (int)(p - *arg);
18303 *arg = skipwhite(p);
18304
18305 return len;
18306}
18307
18308/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018309 * Get the length of the name of a variable or function.
18310 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018312 * Return -1 if curly braces expansion failed.
18313 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 * If the name contains 'magic' {}'s, expand them and return the
18315 * expanded name in an allocated string via 'alias' - caller must free.
18316 */
18317 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018318get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319 char_u **arg;
18320 char_u **alias;
18321 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018322 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323{
18324 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325 char_u *p;
18326 char_u *expr_start;
18327 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328
18329 *alias = NULL; /* default to no alias */
18330
18331 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18332 && (*arg)[2] == (int)KE_SNR)
18333 {
18334 /* hard coded <SNR>, already translated */
18335 *arg += 3;
18336 return get_id_len(arg) + 3;
18337 }
18338 len = eval_fname_script(*arg);
18339 if (len > 0)
18340 {
18341 /* literal "<SID>", "s:" or "<SNR>" */
18342 *arg += len;
18343 }
18344
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018346 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018348 p = find_name_end(*arg, &expr_start, &expr_end,
18349 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 if (expr_start != NULL)
18351 {
18352 char_u *temp_string;
18353
18354 if (!evaluate)
18355 {
18356 len += (int)(p - *arg);
18357 *arg = skipwhite(p);
18358 return len;
18359 }
18360
18361 /*
18362 * Include any <SID> etc in the expanded string:
18363 * Thus the -len here.
18364 */
18365 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18366 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018367 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 *alias = temp_string;
18369 *arg = skipwhite(p);
18370 return (int)STRLEN(temp_string);
18371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372
18373 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018374 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 EMSG2(_(e_invexpr2), *arg);
18376
18377 return len;
18378}
18379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018380/*
18381 * Find the end of a variable or function name, taking care of magic braces.
18382 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18383 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018384 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018385 * Return a pointer to just after the name. Equal to "arg" if there is no
18386 * valid name.
18387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018389find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390 char_u *arg;
18391 char_u **expr_start;
18392 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018393 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018395 int mb_nest = 0;
18396 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 char_u *p;
18398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018399 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018401 *expr_start = NULL;
18402 *expr_end = NULL;
18403 }
18404
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018405 /* Quick check for valid starting character. */
18406 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18407 return arg;
18408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018409 for (p = arg; *p != NUL
18410 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018411 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018412 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018413 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018414 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018415 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018416 if (*p == '\'')
18417 {
18418 /* skip over 'string' to avoid counting [ and ] inside it. */
18419 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18420 ;
18421 if (*p == NUL)
18422 break;
18423 }
18424 else if (*p == '"')
18425 {
18426 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18427 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18428 if (*p == '\\' && p[1] != NUL)
18429 ++p;
18430 if (*p == NUL)
18431 break;
18432 }
18433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018434 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018436 if (*p == '[')
18437 ++br_nest;
18438 else if (*p == ']')
18439 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018442 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018444 if (*p == '{')
18445 {
18446 mb_nest++;
18447 if (expr_start != NULL && *expr_start == NULL)
18448 *expr_start = p;
18449 }
18450 else if (*p == '}')
18451 {
18452 mb_nest--;
18453 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18454 *expr_end = p;
18455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457 }
18458
18459 return p;
18460}
18461
18462/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018463 * Expands out the 'magic' {}'s in a variable/function name.
18464 * Note that this can call itself recursively, to deal with
18465 * constructs like foo{bar}{baz}{bam}
18466 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18467 * "in_start" ^
18468 * "expr_start" ^
18469 * "expr_end" ^
18470 * "in_end" ^
18471 *
18472 * Returns a new allocated string, which the caller must free.
18473 * Returns NULL for failure.
18474 */
18475 static char_u *
18476make_expanded_name(in_start, expr_start, expr_end, in_end)
18477 char_u *in_start;
18478 char_u *expr_start;
18479 char_u *expr_end;
18480 char_u *in_end;
18481{
18482 char_u c1;
18483 char_u *retval = NULL;
18484 char_u *temp_result;
18485 char_u *nextcmd = NULL;
18486
18487 if (expr_end == NULL || in_end == NULL)
18488 return NULL;
18489 *expr_start = NUL;
18490 *expr_end = NUL;
18491 c1 = *in_end;
18492 *in_end = NUL;
18493
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018494 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018495 if (temp_result != NULL && nextcmd == NULL)
18496 {
18497 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18498 + (in_end - expr_end) + 1));
18499 if (retval != NULL)
18500 {
18501 STRCPY(retval, in_start);
18502 STRCAT(retval, temp_result);
18503 STRCAT(retval, expr_end + 1);
18504 }
18505 }
18506 vim_free(temp_result);
18507
18508 *in_end = c1; /* put char back for error messages */
18509 *expr_start = '{';
18510 *expr_end = '}';
18511
18512 if (retval != NULL)
18513 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018514 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018515 if (expr_start != NULL)
18516 {
18517 /* Further expansion! */
18518 temp_result = make_expanded_name(retval, expr_start,
18519 expr_end, temp_result);
18520 vim_free(retval);
18521 retval = temp_result;
18522 }
18523 }
18524
18525 return retval;
18526}
18527
18528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018530 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 */
18532 static int
18533eval_isnamec(c)
18534 int c;
18535{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018536 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18537}
18538
18539/*
18540 * Return TRUE if character "c" can be used as the first character in a
18541 * variable or function name (excluding '{' and '}').
18542 */
18543 static int
18544eval_isnamec1(c)
18545 int c;
18546{
18547 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548}
18549
18550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551 * Set number v: variable to "val".
18552 */
18553 void
18554set_vim_var_nr(idx, val)
18555 int idx;
18556 long val;
18557{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018558 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559}
18560
18561/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018562 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 */
18564 long
18565get_vim_var_nr(idx)
18566 int idx;
18567{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018568 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569}
18570
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018571/*
18572 * Get string v: variable value. Uses a static buffer, can only be used once.
18573 */
18574 char_u *
18575get_vim_var_str(idx)
18576 int idx;
18577{
18578 return get_tv_string(&vimvars[idx].vv_tv);
18579}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018580
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018582 * Get List v: variable value. Caller must take care of reference count when
18583 * needed.
18584 */
18585 list_T *
18586get_vim_var_list(idx)
18587 int idx;
18588{
18589 return vimvars[idx].vv_list;
18590}
18591
18592/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018593 * Set v:char to character "c".
18594 */
18595 void
18596set_vim_var_char(c)
18597 int c;
18598{
18599#ifdef FEAT_MBYTE
18600 char_u buf[MB_MAXBYTES];
18601#else
18602 char_u buf[2];
18603#endif
18604
18605#ifdef FEAT_MBYTE
18606 if (has_mbyte)
18607 buf[(*mb_char2bytes)(c, buf)] = NUL;
18608 else
18609#endif
18610 {
18611 buf[0] = c;
18612 buf[1] = NUL;
18613 }
18614 set_vim_var_string(VV_CHAR, buf, -1);
18615}
18616
18617/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018618 * Set v:count to "count" and v:count1 to "count1".
18619 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620 */
18621 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018622set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 long count;
18624 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018625 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018627 if (set_prevcount)
18628 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018629 vimvars[VV_COUNT].vv_nr = count;
18630 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631}
18632
18633/*
18634 * Set string v: variable to a copy of "val".
18635 */
18636 void
18637set_vim_var_string(idx, val, len)
18638 int idx;
18639 char_u *val;
18640 int len; /* length of "val" to use or -1 (whole string) */
18641{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018642 /* Need to do this (at least) once, since we can't initialize a union.
18643 * Will always be invoked when "v:progname" is set. */
18644 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18645
Bram Moolenaare9a41262005-01-15 22:18:47 +000018646 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018648 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018650 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018652 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653}
18654
18655/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018656 * Set List v: variable to "val".
18657 */
18658 void
18659set_vim_var_list(idx, val)
18660 int idx;
18661 list_T *val;
18662{
18663 list_unref(vimvars[idx].vv_list);
18664 vimvars[idx].vv_list = val;
18665 if (val != NULL)
18666 ++val->lv_refcount;
18667}
18668
18669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670 * Set v:register if needed.
18671 */
18672 void
18673set_reg_var(c)
18674 int c;
18675{
18676 char_u regname;
18677
18678 if (c == 0 || c == ' ')
18679 regname = '"';
18680 else
18681 regname = c;
18682 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018683 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 set_vim_var_string(VV_REG, &regname, 1);
18685}
18686
18687/*
18688 * Get or set v:exception. If "oldval" == NULL, return the current value.
18689 * Otherwise, restore the value to "oldval" and return NULL.
18690 * Must always be called in pairs to save and restore v:exception! Does not
18691 * take care of memory allocations.
18692 */
18693 char_u *
18694v_exception(oldval)
18695 char_u *oldval;
18696{
18697 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018698 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699
Bram Moolenaare9a41262005-01-15 22:18:47 +000018700 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701 return NULL;
18702}
18703
18704/*
18705 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18706 * Otherwise, restore the value to "oldval" and return NULL.
18707 * Must always be called in pairs to save and restore v:throwpoint! Does not
18708 * take care of memory allocations.
18709 */
18710 char_u *
18711v_throwpoint(oldval)
18712 char_u *oldval;
18713{
18714 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018715 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716
Bram Moolenaare9a41262005-01-15 22:18:47 +000018717 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718 return NULL;
18719}
18720
18721#if defined(FEAT_AUTOCMD) || defined(PROTO)
18722/*
18723 * Set v:cmdarg.
18724 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18725 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18726 * Must always be called in pairs!
18727 */
18728 char_u *
18729set_cmdarg(eap, oldarg)
18730 exarg_T *eap;
18731 char_u *oldarg;
18732{
18733 char_u *oldval;
18734 char_u *newval;
18735 unsigned len;
18736
Bram Moolenaare9a41262005-01-15 22:18:47 +000018737 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018738 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018740 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018741 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018742 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 }
18744
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018745 if (eap->force_bin == FORCE_BIN)
18746 len = 6;
18747 else if (eap->force_bin == FORCE_NOBIN)
18748 len = 8;
18749 else
18750 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018751
18752 if (eap->read_edit)
18753 len += 7;
18754
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018755 if (eap->force_ff != 0)
18756 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18757# ifdef FEAT_MBYTE
18758 if (eap->force_enc != 0)
18759 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018760 if (eap->bad_char != 0)
18761 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018762# endif
18763
18764 newval = alloc(len + 1);
18765 if (newval == NULL)
18766 return NULL;
18767
18768 if (eap->force_bin == FORCE_BIN)
18769 sprintf((char *)newval, " ++bin");
18770 else if (eap->force_bin == FORCE_NOBIN)
18771 sprintf((char *)newval, " ++nobin");
18772 else
18773 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018774
18775 if (eap->read_edit)
18776 STRCAT(newval, " ++edit");
18777
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018778 if (eap->force_ff != 0)
18779 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18780 eap->cmd + eap->force_ff);
18781# ifdef FEAT_MBYTE
18782 if (eap->force_enc != 0)
18783 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18784 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018785 if (eap->bad_char == BAD_KEEP)
18786 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18787 else if (eap->bad_char == BAD_DROP)
18788 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18789 else if (eap->bad_char != 0)
18790 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018791# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018792 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018793 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794}
18795#endif
18796
18797/*
18798 * Get the value of internal variable "name".
18799 * Return OK or FAIL.
18800 */
18801 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018802get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 char_u *name;
18804 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018805 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018806 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807{
18808 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018809 typval_T *tv = NULL;
18810 typval_T atv;
18811 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813
18814 /* truncate the name, so that we can use strcmp() */
18815 cc = name[len];
18816 name[len] = NUL;
18817
18818 /*
18819 * Check for "b:changedtick".
18820 */
18821 if (STRCMP(name, "b:changedtick") == 0)
18822 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018823 atv.v_type = VAR_NUMBER;
18824 atv.vval.v_number = curbuf->b_changedtick;
18825 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826 }
18827
18828 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 * Check for user-defined variables.
18830 */
18831 else
18832 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018833 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018835 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 }
18837
Bram Moolenaare9a41262005-01-15 22:18:47 +000018838 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018840 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018841 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842 ret = FAIL;
18843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018844 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018845 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846
18847 name[len] = cc;
18848
18849 return ret;
18850}
18851
18852/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018853 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18854 * Also handle function call with Funcref variable: func(expr)
18855 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18856 */
18857 static int
18858handle_subscript(arg, rettv, evaluate, verbose)
18859 char_u **arg;
18860 typval_T *rettv;
18861 int evaluate; /* do more than finding the end */
18862 int verbose; /* give error messages */
18863{
18864 int ret = OK;
18865 dict_T *selfdict = NULL;
18866 char_u *s;
18867 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018868 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018869
18870 while (ret == OK
18871 && (**arg == '['
18872 || (**arg == '.' && rettv->v_type == VAR_DICT)
18873 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18874 && !vim_iswhite(*(*arg - 1)))
18875 {
18876 if (**arg == '(')
18877 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018878 /* need to copy the funcref so that we can clear rettv */
18879 functv = *rettv;
18880 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018881
18882 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018883 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018884 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018885 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18886 &len, evaluate, selfdict);
18887
18888 /* Clear the funcref afterwards, so that deleting it while
18889 * evaluating the arguments is possible (see test55). */
18890 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018891
18892 /* Stop the expression evaluation when immediately aborting on
18893 * error, or when an interrupt occurred or an exception was thrown
18894 * but not caught. */
18895 if (aborting())
18896 {
18897 if (ret == OK)
18898 clear_tv(rettv);
18899 ret = FAIL;
18900 }
18901 dict_unref(selfdict);
18902 selfdict = NULL;
18903 }
18904 else /* **arg == '[' || **arg == '.' */
18905 {
18906 dict_unref(selfdict);
18907 if (rettv->v_type == VAR_DICT)
18908 {
18909 selfdict = rettv->vval.v_dict;
18910 if (selfdict != NULL)
18911 ++selfdict->dv_refcount;
18912 }
18913 else
18914 selfdict = NULL;
18915 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18916 {
18917 clear_tv(rettv);
18918 ret = FAIL;
18919 }
18920 }
18921 }
18922 dict_unref(selfdict);
18923 return ret;
18924}
18925
18926/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018927 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018928 * value).
18929 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018930 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018931alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018932{
Bram Moolenaar33570922005-01-25 22:26:29 +000018933 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018934}
18935
18936/*
18937 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938 * The string "s" must have been allocated, it is consumed.
18939 * Return NULL for out of memory, the variable otherwise.
18940 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018941 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018942alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943 char_u *s;
18944{
Bram Moolenaar33570922005-01-25 22:26:29 +000018945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018947 rettv = alloc_tv();
18948 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018950 rettv->v_type = VAR_STRING;
18951 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952 }
18953 else
18954 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018955 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956}
18957
18958/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018959 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018961 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018962free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018963 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964{
18965 if (varp != NULL)
18966 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018967 switch (varp->v_type)
18968 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018969 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018970 func_unref(varp->vval.v_string);
18971 /*FALLTHROUGH*/
18972 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018973 vim_free(varp->vval.v_string);
18974 break;
18975 case VAR_LIST:
18976 list_unref(varp->vval.v_list);
18977 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018978 case VAR_DICT:
18979 dict_unref(varp->vval.v_dict);
18980 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018981 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018982#ifdef FEAT_FLOAT
18983 case VAR_FLOAT:
18984#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018985 case VAR_UNKNOWN:
18986 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018987 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018988 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018989 break;
18990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 vim_free(varp);
18992 }
18993}
18994
18995/*
18996 * Free the memory for a variable value and set the value to NULL or 0.
18997 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018998 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018999clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019000 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001{
19002 if (varp != NULL)
19003 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019004 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019006 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019007 func_unref(varp->vval.v_string);
19008 /*FALLTHROUGH*/
19009 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019010 vim_free(varp->vval.v_string);
19011 varp->vval.v_string = NULL;
19012 break;
19013 case VAR_LIST:
19014 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019015 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019016 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019017 case VAR_DICT:
19018 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019019 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019020 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019021 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019022 varp->vval.v_number = 0;
19023 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019024#ifdef FEAT_FLOAT
19025 case VAR_FLOAT:
19026 varp->vval.v_float = 0.0;
19027 break;
19028#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019029 case VAR_UNKNOWN:
19030 break;
19031 default:
19032 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019034 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 }
19036}
19037
19038/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019039 * Set the value of a variable to NULL without freeing items.
19040 */
19041 static void
19042init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019043 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019044{
19045 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019046 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019047}
19048
19049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050 * Get the number value of a variable.
19051 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019052 * For incompatible types, return 0.
19053 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19054 * caller of incompatible types: it sets *denote to TRUE if "denote"
19055 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056 */
19057 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019058get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019059 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019061 int error = FALSE;
19062
19063 return get_tv_number_chk(varp, &error); /* return 0L on error */
19064}
19065
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019066 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019067get_tv_number_chk(varp, denote)
19068 typval_T *varp;
19069 int *denote;
19070{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019071 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019073 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019075 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019076 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019077#ifdef FEAT_FLOAT
19078 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019079 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019080 break;
19081#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019082 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019083 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019084 break;
19085 case VAR_STRING:
19086 if (varp->vval.v_string != NULL)
19087 vim_str2nr(varp->vval.v_string, NULL, NULL,
19088 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019089 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019090 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019091 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019092 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019093 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019094 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019095 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019096 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019097 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019098 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019100 if (denote == NULL) /* useful for values that must be unsigned */
19101 n = -1;
19102 else
19103 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019104 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105}
19106
19107/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019108 * Get the lnum from the first argument.
19109 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019110 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 */
19112 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019113get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019114 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115{
Bram Moolenaar33570922005-01-25 22:26:29 +000019116 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 linenr_T lnum;
19118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019119 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120 if (lnum == 0) /* no valid number, try using line() */
19121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019122 rettv.v_type = VAR_NUMBER;
19123 f_line(argvars, &rettv);
19124 lnum = rettv.vval.v_number;
19125 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126 }
19127 return lnum;
19128}
19129
19130/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019131 * Get the lnum from the first argument.
19132 * Also accepts "$", then "buf" is used.
19133 * Returns 0 on error.
19134 */
19135 static linenr_T
19136get_tv_lnum_buf(argvars, buf)
19137 typval_T *argvars;
19138 buf_T *buf;
19139{
19140 if (argvars[0].v_type == VAR_STRING
19141 && argvars[0].vval.v_string != NULL
19142 && argvars[0].vval.v_string[0] == '$'
19143 && buf != NULL)
19144 return buf->b_ml.ml_line_count;
19145 return get_tv_number_chk(&argvars[0], NULL);
19146}
19147
19148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149 * Get the string value of a variable.
19150 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019151 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19152 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 * If the String variable has never been set, return an empty string.
19154 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019155 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19156 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 */
19158 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019159get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019160 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161{
19162 static char_u mybuf[NUMBUFLEN];
19163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019164 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165}
19166
19167 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019168get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019169 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019170 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019172 char_u *res = get_tv_string_buf_chk(varp, buf);
19173
19174 return res != NULL ? res : (char_u *)"";
19175}
19176
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019177 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019178get_tv_string_chk(varp)
19179 typval_T *varp;
19180{
19181 static char_u mybuf[NUMBUFLEN];
19182
19183 return get_tv_string_buf_chk(varp, mybuf);
19184}
19185
19186 static char_u *
19187get_tv_string_buf_chk(varp, buf)
19188 typval_T *varp;
19189 char_u *buf;
19190{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019191 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019193 case VAR_NUMBER:
19194 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19195 return buf;
19196 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019197 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019198 break;
19199 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019200 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019201 break;
19202 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019203 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019204 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019205#ifdef FEAT_FLOAT
19206 case VAR_FLOAT:
19207 EMSG(_("E806: using Float as a String"));
19208 break;
19209#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019210 case VAR_STRING:
19211 if (varp->vval.v_string != NULL)
19212 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019213 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019214 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019215 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019216 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019218 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219}
19220
19221/*
19222 * Find variable "name" in the list of variables.
19223 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019224 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019225 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019226 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019228 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019229find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019231 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019234 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235
Bram Moolenaara7043832005-01-21 11:56:39 +000019236 ht = find_var_ht(name, &varname);
19237 if (htp != NULL)
19238 *htp = ht;
19239 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019241 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242}
19243
19244/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019245 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019246 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019248 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019249find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019250 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019251 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019252 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019253{
Bram Moolenaar33570922005-01-25 22:26:29 +000019254 hashitem_T *hi;
19255
19256 if (*varname == NUL)
19257 {
19258 /* Must be something like "s:", otherwise "ht" would be NULL. */
19259 switch (varname[-2])
19260 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019261 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019262 case 'g': return &globvars_var;
19263 case 'v': return &vimvars_var;
19264 case 'b': return &curbuf->b_bufvar;
19265 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019266#ifdef FEAT_WINDOWS
19267 case 't': return &curtab->tp_winvar;
19268#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019269 case 'l': return current_funccal == NULL
19270 ? NULL : &current_funccal->l_vars_var;
19271 case 'a': return current_funccal == NULL
19272 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019273 }
19274 return NULL;
19275 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019276
19277 hi = hash_find(ht, varname);
19278 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019279 {
19280 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019281 * worked find the variable again. Don't auto-load a script if it was
19282 * loaded already, otherwise it would be loaded every time when
19283 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019284 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019285 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019286 hi = hash_find(ht, varname);
19287 if (HASHITEM_EMPTY(hi))
19288 return NULL;
19289 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019290 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019291}
19292
19293/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019294 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019295 * Set "varname" to the start of name without ':'.
19296 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019297 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019298find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299 char_u *name;
19300 char_u **varname;
19301{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019302 hashitem_T *hi;
19303
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 if (name[1] != ':')
19305 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019306 /* The name must not start with a colon or #. */
19307 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019308 return NULL;
19309 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019310
19311 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019312 hi = hash_find(&compat_hashtab, name);
19313 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019314 return &compat_hashtab;
19315
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019317 return &globvarht; /* global variable */
19318 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 }
19320 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019321 if (*name == 'g') /* global variable */
19322 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019323 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19324 */
19325 if (vim_strchr(name + 2, ':') != NULL
19326 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019327 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019329 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019331 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019332#ifdef FEAT_WINDOWS
19333 if (*name == 't') /* tab page variable */
19334 return &curtab->tp_vars.dv_hashtab;
19335#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019336 if (*name == 'v') /* v: variable */
19337 return &vimvarht;
19338 if (*name == 'a' && current_funccal != NULL) /* function argument */
19339 return &current_funccal->l_avars.dv_hashtab;
19340 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19341 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 if (*name == 's' /* script variable */
19343 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19344 return &SCRIPT_VARS(current_SID);
19345 return NULL;
19346}
19347
19348/*
19349 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019350 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351 * Returns NULL when it doesn't exist.
19352 */
19353 char_u *
19354get_var_value(name)
19355 char_u *name;
19356{
Bram Moolenaar33570922005-01-25 22:26:29 +000019357 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358
Bram Moolenaara7043832005-01-21 11:56:39 +000019359 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 if (v == NULL)
19361 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019362 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363}
19364
19365/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019366 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 * sourcing this script and when executing functions defined in the script.
19368 */
19369 void
19370new_script_vars(id)
19371 scid_T id;
19372{
Bram Moolenaara7043832005-01-21 11:56:39 +000019373 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019374 hashtab_T *ht;
19375 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019376
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19378 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019379 /* Re-allocating ga_data means that an ht_array pointing to
19380 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019381 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019382 for (i = 1; i <= ga_scripts.ga_len; ++i)
19383 {
19384 ht = &SCRIPT_VARS(i);
19385 if (ht->ht_mask == HT_INIT_SIZE - 1)
19386 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019387 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019388 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019389 }
19390
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 while (ga_scripts.ga_len < id)
19392 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019393 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019394 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019395 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 }
19398 }
19399}
19400
19401/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019402 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19403 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 */
19405 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019406init_var_dict(dict, dict_var)
19407 dict_T *dict;
19408 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409{
Bram Moolenaar33570922005-01-25 22:26:29 +000019410 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019411 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019412 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019413 dict_var->di_tv.vval.v_dict = dict;
19414 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019415 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019416 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19417 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019418}
19419
19420/*
19421 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019422 * Frees all allocated variables and the value they contain.
19423 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424 */
19425 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019426vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019427 hashtab_T *ht;
19428{
19429 vars_clear_ext(ht, TRUE);
19430}
19431
19432/*
19433 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19434 */
19435 static void
19436vars_clear_ext(ht, free_val)
19437 hashtab_T *ht;
19438 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439{
Bram Moolenaara7043832005-01-21 11:56:39 +000019440 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019441 hashitem_T *hi;
19442 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443
Bram Moolenaar33570922005-01-25 22:26:29 +000019444 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019445 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019446 for (hi = ht->ht_array; todo > 0; ++hi)
19447 {
19448 if (!HASHITEM_EMPTY(hi))
19449 {
19450 --todo;
19451
Bram Moolenaar33570922005-01-25 22:26:29 +000019452 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019453 * ht_array might change then. hash_clear() takes care of it
19454 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019455 v = HI2DI(hi);
19456 if (free_val)
19457 clear_tv(&v->di_tv);
19458 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19459 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019460 }
19461 }
19462 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019463 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464}
19465
Bram Moolenaara7043832005-01-21 11:56:39 +000019466/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019467 * Delete a variable from hashtab "ht" at item "hi".
19468 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019471delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019472 hashtab_T *ht;
19473 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474{
Bram Moolenaar33570922005-01-25 22:26:29 +000019475 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019476
19477 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019478 clear_tv(&di->di_tv);
19479 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480}
19481
19482/*
19483 * List the value of one internal variable.
19484 */
19485 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019486list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019487 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019489 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019491 char_u *tofree;
19492 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019493 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019494
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019495 current_copyID += COPYID_INC;
19496 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019497 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019498 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019499 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500}
19501
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019503list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 char_u *prefix;
19505 char_u *name;
19506 int type;
19507 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019508 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509{
Bram Moolenaar31859182007-08-14 20:41:13 +000019510 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19511 msg_start();
19512 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 if (name != NULL) /* "a:" vars don't have a name stored */
19514 msg_puts(name);
19515 msg_putchar(' ');
19516 msg_advance(22);
19517 if (type == VAR_NUMBER)
19518 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019519 else if (type == VAR_FUNC)
19520 msg_putchar('*');
19521 else if (type == VAR_LIST)
19522 {
19523 msg_putchar('[');
19524 if (*string == '[')
19525 ++string;
19526 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019527 else if (type == VAR_DICT)
19528 {
19529 msg_putchar('{');
19530 if (*string == '{')
19531 ++string;
19532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 else
19534 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019535
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019537
19538 if (type == VAR_FUNC)
19539 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019540 if (*first)
19541 {
19542 msg_clr_eos();
19543 *first = FALSE;
19544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545}
19546
19547/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019548 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549 * If the variable already exists, the value is updated.
19550 * Otherwise the variable is created.
19551 */
19552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019553set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019555 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019556 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557{
Bram Moolenaar33570922005-01-25 22:26:29 +000019558 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019560 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019561 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019563 ht = find_var_ht(name, &varname);
19564 if (ht == NULL || *varname == NUL)
19565 {
19566 EMSG2(_(e_illvar), name);
19567 return;
19568 }
19569 v = find_var_in_ht(ht, varname, TRUE);
19570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019571 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019572 {
19573 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19574 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19575 ? name[2] : name[0]))
19576 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019577 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019578 return;
19579 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019580 /* Don't allow hiding a function. When "v" is not NULL we migth be
19581 * assigning another function to the same var, the type is checked
19582 * below. */
19583 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019584 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019585 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019586 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019587 return;
19588 }
19589 }
19590
Bram Moolenaar33570922005-01-25 22:26:29 +000019591 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019593 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019594 if (var_check_ro(v->di_flags, name)
19595 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019596 return;
19597 if (v->di_tv.v_type != tv->v_type
19598 && !((v->di_tv.v_type == VAR_STRING
19599 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019600 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019601 || tv->v_type == VAR_NUMBER))
19602#ifdef FEAT_FLOAT
19603 && !((v->di_tv.v_type == VAR_NUMBER
19604 || v->di_tv.v_type == VAR_FLOAT)
19605 && (tv->v_type == VAR_NUMBER
19606 || tv->v_type == VAR_FLOAT))
19607#endif
19608 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019609 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019610 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019611 return;
19612 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019613
19614 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019615 * Handle setting internal v: variables separately: we don't change
19616 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019617 */
19618 if (ht == &vimvarht)
19619 {
19620 if (v->di_tv.v_type == VAR_STRING)
19621 {
19622 vim_free(v->di_tv.vval.v_string);
19623 if (copy || tv->v_type != VAR_STRING)
19624 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19625 else
19626 {
19627 /* Take over the string to avoid an extra alloc/free. */
19628 v->di_tv.vval.v_string = tv->vval.v_string;
19629 tv->vval.v_string = NULL;
19630 }
19631 }
19632 else if (v->di_tv.v_type != VAR_NUMBER)
19633 EMSG2(_(e_intern2), "set_var()");
19634 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019635 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019637 if (STRCMP(varname, "searchforward") == 0)
19638 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19639 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019640 return;
19641 }
19642
19643 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 }
19645 else /* add a new variable */
19646 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019647 /* Can't add "v:" variable. */
19648 if (ht == &vimvarht)
19649 {
19650 EMSG2(_(e_illvar), name);
19651 return;
19652 }
19653
Bram Moolenaar92124a32005-06-17 22:03:40 +000019654 /* Make sure the variable name is valid. */
19655 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019656 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19657 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019658 {
19659 EMSG2(_(e_illvar), varname);
19660 return;
19661 }
19662
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019663 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19664 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019665 if (v == NULL)
19666 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019667 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019668 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019670 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 return;
19672 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019673 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019675
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019676 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019677 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019678 else
19679 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019680 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019681 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019682 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684}
19685
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019686/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019687 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019688 * Also give an error message.
19689 */
19690 static int
19691var_check_ro(flags, name)
19692 int flags;
19693 char_u *name;
19694{
19695 if (flags & DI_FLAGS_RO)
19696 {
19697 EMSG2(_(e_readonlyvar), name);
19698 return TRUE;
19699 }
19700 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19701 {
19702 EMSG2(_(e_readonlysbx), name);
19703 return TRUE;
19704 }
19705 return FALSE;
19706}
19707
19708/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019709 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19710 * Also give an error message.
19711 */
19712 static int
19713var_check_fixed(flags, name)
19714 int flags;
19715 char_u *name;
19716{
19717 if (flags & DI_FLAGS_FIX)
19718 {
19719 EMSG2(_("E795: Cannot delete variable %s"), name);
19720 return TRUE;
19721 }
19722 return FALSE;
19723}
19724
19725/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019726 * Return TRUE if typeval "tv" is set to be locked (immutable).
19727 * Also give an error message, using "name".
19728 */
19729 static int
19730tv_check_lock(lock, name)
19731 int lock;
19732 char_u *name;
19733{
19734 if (lock & VAR_LOCKED)
19735 {
19736 EMSG2(_("E741: Value is locked: %s"),
19737 name == NULL ? (char_u *)_("Unknown") : name);
19738 return TRUE;
19739 }
19740 if (lock & VAR_FIXED)
19741 {
19742 EMSG2(_("E742: Cannot change value of %s"),
19743 name == NULL ? (char_u *)_("Unknown") : name);
19744 return TRUE;
19745 }
19746 return FALSE;
19747}
19748
19749/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019750 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019751 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019752 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019753 * It is OK for "from" and "to" to point to the same item. This is used to
19754 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019755 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019756 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019757copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019758 typval_T *from;
19759 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019761 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019762 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019763 switch (from->v_type)
19764 {
19765 case VAR_NUMBER:
19766 to->vval.v_number = from->vval.v_number;
19767 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019768#ifdef FEAT_FLOAT
19769 case VAR_FLOAT:
19770 to->vval.v_float = from->vval.v_float;
19771 break;
19772#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019773 case VAR_STRING:
19774 case VAR_FUNC:
19775 if (from->vval.v_string == NULL)
19776 to->vval.v_string = NULL;
19777 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019778 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019779 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019780 if (from->v_type == VAR_FUNC)
19781 func_ref(to->vval.v_string);
19782 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019783 break;
19784 case VAR_LIST:
19785 if (from->vval.v_list == NULL)
19786 to->vval.v_list = NULL;
19787 else
19788 {
19789 to->vval.v_list = from->vval.v_list;
19790 ++to->vval.v_list->lv_refcount;
19791 }
19792 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019793 case VAR_DICT:
19794 if (from->vval.v_dict == NULL)
19795 to->vval.v_dict = NULL;
19796 else
19797 {
19798 to->vval.v_dict = from->vval.v_dict;
19799 ++to->vval.v_dict->dv_refcount;
19800 }
19801 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019802 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019803 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019804 break;
19805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806}
19807
19808/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019809 * Make a copy of an item.
19810 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019811 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19812 * reference to an already copied list/dict can be used.
19813 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019814 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019815 static int
19816item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019817 typval_T *from;
19818 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019819 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019820 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019821{
19822 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019823 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019824
Bram Moolenaar33570922005-01-25 22:26:29 +000019825 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019826 {
19827 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019828 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019829 }
19830 ++recurse;
19831
19832 switch (from->v_type)
19833 {
19834 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019835#ifdef FEAT_FLOAT
19836 case VAR_FLOAT:
19837#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019838 case VAR_STRING:
19839 case VAR_FUNC:
19840 copy_tv(from, to);
19841 break;
19842 case VAR_LIST:
19843 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019844 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019845 if (from->vval.v_list == NULL)
19846 to->vval.v_list = NULL;
19847 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19848 {
19849 /* use the copy made earlier */
19850 to->vval.v_list = from->vval.v_list->lv_copylist;
19851 ++to->vval.v_list->lv_refcount;
19852 }
19853 else
19854 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19855 if (to->vval.v_list == NULL)
19856 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019857 break;
19858 case VAR_DICT:
19859 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019860 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019861 if (from->vval.v_dict == NULL)
19862 to->vval.v_dict = NULL;
19863 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19864 {
19865 /* use the copy made earlier */
19866 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19867 ++to->vval.v_dict->dv_refcount;
19868 }
19869 else
19870 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19871 if (to->vval.v_dict == NULL)
19872 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019873 break;
19874 default:
19875 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019876 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019877 }
19878 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019879 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019880}
19881
19882/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 * ":echo expr1 ..." print each argument separated with a space, add a
19884 * newline at the end.
19885 * ":echon expr1 ..." print each argument plain.
19886 */
19887 void
19888ex_echo(eap)
19889 exarg_T *eap;
19890{
19891 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019892 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019893 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 char_u *p;
19895 int needclr = TRUE;
19896 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019897 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898
19899 if (eap->skip)
19900 ++emsg_skip;
19901 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19902 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019903 /* If eval1() causes an error message the text from the command may
19904 * still need to be cleared. E.g., "echo 22,44". */
19905 need_clr_eos = needclr;
19906
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019908 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909 {
19910 /*
19911 * Report the invalid expression unless the expression evaluation
19912 * has been cancelled due to an aborting error, an interrupt, or an
19913 * exception.
19914 */
19915 if (!aborting())
19916 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019917 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 break;
19919 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019920 need_clr_eos = FALSE;
19921
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922 if (!eap->skip)
19923 {
19924 if (atstart)
19925 {
19926 atstart = FALSE;
19927 /* Call msg_start() after eval1(), evaluating the expression
19928 * may cause a message to appear. */
19929 if (eap->cmdidx == CMD_echo)
19930 msg_start();
19931 }
19932 else if (eap->cmdidx == CMD_echo)
19933 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019934 current_copyID += COPYID_INC;
19935 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019936 if (p != NULL)
19937 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019939 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019941 if (*p != TAB && needclr)
19942 {
19943 /* remove any text still there from the command */
19944 msg_clr_eos();
19945 needclr = FALSE;
19946 }
19947 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 }
19949 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019950 {
19951#ifdef FEAT_MBYTE
19952 if (has_mbyte)
19953 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019954 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019955
19956 (void)msg_outtrans_len_attr(p, i, echo_attr);
19957 p += i - 1;
19958 }
19959 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019961 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019964 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019966 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 arg = skipwhite(arg);
19968 }
19969 eap->nextcmd = check_nextcmd(arg);
19970
19971 if (eap->skip)
19972 --emsg_skip;
19973 else
19974 {
19975 /* remove text that may still be there from the command */
19976 if (needclr)
19977 msg_clr_eos();
19978 if (eap->cmdidx == CMD_echo)
19979 msg_end();
19980 }
19981}
19982
19983/*
19984 * ":echohl {name}".
19985 */
19986 void
19987ex_echohl(eap)
19988 exarg_T *eap;
19989{
19990 int id;
19991
19992 id = syn_name2id(eap->arg);
19993 if (id == 0)
19994 echo_attr = 0;
19995 else
19996 echo_attr = syn_id2attr(id);
19997}
19998
19999/*
20000 * ":execute expr1 ..." execute the result of an expression.
20001 * ":echomsg expr1 ..." Print a message
20002 * ":echoerr expr1 ..." Print an error
20003 * Each gets spaces around each argument and a newline at the end for
20004 * echo commands
20005 */
20006 void
20007ex_execute(eap)
20008 exarg_T *eap;
20009{
20010 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020011 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 int ret = OK;
20013 char_u *p;
20014 garray_T ga;
20015 int len;
20016 int save_did_emsg;
20017
20018 ga_init2(&ga, 1, 80);
20019
20020 if (eap->skip)
20021 ++emsg_skip;
20022 while (*arg != NUL && *arg != '|' && *arg != '\n')
20023 {
20024 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020025 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 {
20027 /*
20028 * Report the invalid expression unless the expression evaluation
20029 * has been cancelled due to an aborting error, an interrupt, or an
20030 * exception.
20031 */
20032 if (!aborting())
20033 EMSG2(_(e_invexpr2), p);
20034 ret = FAIL;
20035 break;
20036 }
20037
20038 if (!eap->skip)
20039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020040 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 len = (int)STRLEN(p);
20042 if (ga_grow(&ga, len + 2) == FAIL)
20043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020044 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 ret = FAIL;
20046 break;
20047 }
20048 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 ga.ga_len += len;
20052 }
20053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020054 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 arg = skipwhite(arg);
20056 }
20057
20058 if (ret != FAIL && ga.ga_data != NULL)
20059 {
20060 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020063 out_flush();
20064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 else if (eap->cmdidx == CMD_echoerr)
20066 {
20067 /* We don't want to abort following commands, restore did_emsg. */
20068 save_did_emsg = did_emsg;
20069 EMSG((char_u *)ga.ga_data);
20070 if (!force_abort)
20071 did_emsg = save_did_emsg;
20072 }
20073 else if (eap->cmdidx == CMD_execute)
20074 do_cmdline((char_u *)ga.ga_data,
20075 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20076 }
20077
20078 ga_clear(&ga);
20079
20080 if (eap->skip)
20081 --emsg_skip;
20082
20083 eap->nextcmd = check_nextcmd(arg);
20084}
20085
20086/*
20087 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20088 * "arg" points to the "&" or '+' when called, to "option" when returning.
20089 * Returns NULL when no option name found. Otherwise pointer to the char
20090 * after the option name.
20091 */
20092 static char_u *
20093find_option_end(arg, opt_flags)
20094 char_u **arg;
20095 int *opt_flags;
20096{
20097 char_u *p = *arg;
20098
20099 ++p;
20100 if (*p == 'g' && p[1] == ':')
20101 {
20102 *opt_flags = OPT_GLOBAL;
20103 p += 2;
20104 }
20105 else if (*p == 'l' && p[1] == ':')
20106 {
20107 *opt_flags = OPT_LOCAL;
20108 p += 2;
20109 }
20110 else
20111 *opt_flags = 0;
20112
20113 if (!ASCII_ISALPHA(*p))
20114 return NULL;
20115 *arg = p;
20116
20117 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20118 p += 4; /* termcap option */
20119 else
20120 while (ASCII_ISALPHA(*p))
20121 ++p;
20122 return p;
20123}
20124
20125/*
20126 * ":function"
20127 */
20128 void
20129ex_function(eap)
20130 exarg_T *eap;
20131{
20132 char_u *theline;
20133 int j;
20134 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 char_u *name = NULL;
20137 char_u *p;
20138 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020139 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 garray_T newargs;
20141 garray_T newlines;
20142 int varargs = FALSE;
20143 int mustend = FALSE;
20144 int flags = 0;
20145 ufunc_T *fp;
20146 int indent;
20147 int nesting;
20148 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020149 dictitem_T *v;
20150 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020151 static int func_nr = 0; /* number for nameless function */
20152 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020153 hashtab_T *ht;
20154 int todo;
20155 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020156 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157
20158 /*
20159 * ":function" without argument: list functions.
20160 */
20161 if (ends_excmd(*eap->arg))
20162 {
20163 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020164 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020165 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020166 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020167 {
20168 if (!HASHITEM_EMPTY(hi))
20169 {
20170 --todo;
20171 fp = HI2UF(hi);
20172 if (!isdigit(*fp->uf_name))
20173 list_func_head(fp, FALSE);
20174 }
20175 }
20176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 eap->nextcmd = check_nextcmd(eap->arg);
20178 return;
20179 }
20180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020181 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020182 * ":function /pat": list functions matching pattern.
20183 */
20184 if (*eap->arg == '/')
20185 {
20186 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20187 if (!eap->skip)
20188 {
20189 regmatch_T regmatch;
20190
20191 c = *p;
20192 *p = NUL;
20193 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20194 *p = c;
20195 if (regmatch.regprog != NULL)
20196 {
20197 regmatch.rm_ic = p_ic;
20198
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020199 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020200 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20201 {
20202 if (!HASHITEM_EMPTY(hi))
20203 {
20204 --todo;
20205 fp = HI2UF(hi);
20206 if (!isdigit(*fp->uf_name)
20207 && vim_regexec(&regmatch, fp->uf_name, 0))
20208 list_func_head(fp, FALSE);
20209 }
20210 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020211 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020212 }
20213 }
20214 if (*p == '/')
20215 ++p;
20216 eap->nextcmd = check_nextcmd(p);
20217 return;
20218 }
20219
20220 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020221 * Get the function name. There are these situations:
20222 * func normal function name
20223 * "name" == func, "fudi.fd_dict" == NULL
20224 * dict.func new dictionary entry
20225 * "name" == NULL, "fudi.fd_dict" set,
20226 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20227 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020228 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020229 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20230 * dict.func existing dict entry that's not a Funcref
20231 * "name" == NULL, "fudi.fd_dict" set,
20232 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020235 name = trans_function_name(&p, eap->skip, 0, &fudi);
20236 paren = (vim_strchr(p, '(') != NULL);
20237 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 {
20239 /*
20240 * Return on an invalid expression in braces, unless the expression
20241 * evaluation has been cancelled due to an aborting error, an
20242 * interrupt, or an exception.
20243 */
20244 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020245 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020246 if (!eap->skip && fudi.fd_newkey != NULL)
20247 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020248 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 else
20252 eap->skip = TRUE;
20253 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020254
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 /* An error in a function call during evaluation of an expression in magic
20256 * braces should not cause the function not to be defined. */
20257 saved_did_emsg = did_emsg;
20258 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259
20260 /*
20261 * ":function func" with only function name: list function.
20262 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020263 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264 {
20265 if (!ends_excmd(*skipwhite(p)))
20266 {
20267 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020268 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 }
20270 eap->nextcmd = check_nextcmd(p);
20271 if (eap->nextcmd != NULL)
20272 *p = NUL;
20273 if (!eap->skip && !got_int)
20274 {
20275 fp = find_func(name);
20276 if (fp != NULL)
20277 {
20278 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020279 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020281 if (FUNCLINE(fp, j) == NULL)
20282 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 msg_putchar('\n');
20284 msg_outnum((long)(j + 1));
20285 if (j < 9)
20286 msg_putchar(' ');
20287 if (j < 99)
20288 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020289 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 out_flush(); /* show a line at a time */
20291 ui_breakcheck();
20292 }
20293 if (!got_int)
20294 {
20295 msg_putchar('\n');
20296 msg_puts((char_u *)" endfunction");
20297 }
20298 }
20299 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020300 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020302 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 }
20304
20305 /*
20306 * ":function name(arg1, arg2)" Define function.
20307 */
20308 p = skipwhite(p);
20309 if (*p != '(')
20310 {
20311 if (!eap->skip)
20312 {
20313 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020314 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 }
20316 /* attempt to continue by skipping some text */
20317 if (vim_strchr(p, '(') != NULL)
20318 p = vim_strchr(p, '(');
20319 }
20320 p = skipwhite(p + 1);
20321
20322 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20323 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20324
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020325 if (!eap->skip)
20326 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020327 /* Check the name of the function. Unless it's a dictionary function
20328 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020329 if (name != NULL)
20330 arg = name;
20331 else
20332 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020333 if (arg != NULL && (fudi.fd_di == NULL
20334 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020335 {
20336 if (*arg == K_SPECIAL)
20337 j = 3;
20338 else
20339 j = 0;
20340 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20341 : eval_isnamec(arg[j])))
20342 ++j;
20343 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020344 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020345 }
20346 }
20347
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 /*
20349 * Isolate the arguments: "arg1, arg2, ...)"
20350 */
20351 while (*p != ')')
20352 {
20353 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20354 {
20355 varargs = TRUE;
20356 p += 3;
20357 mustend = TRUE;
20358 }
20359 else
20360 {
20361 arg = p;
20362 while (ASCII_ISALNUM(*p) || *p == '_')
20363 ++p;
20364 if (arg == p || isdigit(*arg)
20365 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20366 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20367 {
20368 if (!eap->skip)
20369 EMSG2(_("E125: Illegal argument: %s"), arg);
20370 break;
20371 }
20372 if (ga_grow(&newargs, 1) == FAIL)
20373 goto erret;
20374 c = *p;
20375 *p = NUL;
20376 arg = vim_strsave(arg);
20377 if (arg == NULL)
20378 goto erret;
20379 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20380 *p = c;
20381 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 if (*p == ',')
20383 ++p;
20384 else
20385 mustend = TRUE;
20386 }
20387 p = skipwhite(p);
20388 if (mustend && *p != ')')
20389 {
20390 if (!eap->skip)
20391 EMSG2(_(e_invarg2), eap->arg);
20392 break;
20393 }
20394 }
20395 ++p; /* skip the ')' */
20396
Bram Moolenaare9a41262005-01-15 22:18:47 +000020397 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 for (;;)
20399 {
20400 p = skipwhite(p);
20401 if (STRNCMP(p, "range", 5) == 0)
20402 {
20403 flags |= FC_RANGE;
20404 p += 5;
20405 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020406 else if (STRNCMP(p, "dict", 4) == 0)
20407 {
20408 flags |= FC_DICT;
20409 p += 4;
20410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 else if (STRNCMP(p, "abort", 5) == 0)
20412 {
20413 flags |= FC_ABORT;
20414 p += 5;
20415 }
20416 else
20417 break;
20418 }
20419
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020420 /* When there is a line break use what follows for the function body.
20421 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20422 if (*p == '\n')
20423 line_arg = p + 1;
20424 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425 EMSG(_(e_trailing));
20426
20427 /*
20428 * Read the body of the function, until ":endfunction" is found.
20429 */
20430 if (KeyTyped)
20431 {
20432 /* Check if the function already exists, don't let the user type the
20433 * whole function before telling him it doesn't work! For a script we
20434 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020435 if (!eap->skip && !eap->forceit)
20436 {
20437 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20438 EMSG(_(e_funcdict));
20439 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020440 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020443 if (!eap->skip && did_emsg)
20444 goto erret;
20445
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 msg_putchar('\n'); /* don't overwrite the function name */
20447 cmdline_row = msg_row;
20448 }
20449
20450 indent = 2;
20451 nesting = 0;
20452 for (;;)
20453 {
20454 msg_scroll = TRUE;
20455 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020456 sourcing_lnum_off = sourcing_lnum;
20457
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020458 if (line_arg != NULL)
20459 {
20460 /* Use eap->arg, split up in parts by line breaks. */
20461 theline = line_arg;
20462 p = vim_strchr(theline, '\n');
20463 if (p == NULL)
20464 line_arg += STRLEN(line_arg);
20465 else
20466 {
20467 *p = NUL;
20468 line_arg = p + 1;
20469 }
20470 }
20471 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472 theline = getcmdline(':', 0L, indent);
20473 else
20474 theline = eap->getline(':', eap->cookie, indent);
20475 if (KeyTyped)
20476 lines_left = Rows - 1;
20477 if (theline == NULL)
20478 {
20479 EMSG(_("E126: Missing :endfunction"));
20480 goto erret;
20481 }
20482
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020483 /* Detect line continuation: sourcing_lnum increased more than one. */
20484 if (sourcing_lnum > sourcing_lnum_off + 1)
20485 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20486 else
20487 sourcing_lnum_off = 0;
20488
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 if (skip_until != NULL)
20490 {
20491 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20492 * don't check for ":endfunc". */
20493 if (STRCMP(theline, skip_until) == 0)
20494 {
20495 vim_free(skip_until);
20496 skip_until = NULL;
20497 }
20498 }
20499 else
20500 {
20501 /* skip ':' and blanks*/
20502 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20503 ;
20504
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020505 /* Check for "endfunction". */
20506 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020508 if (line_arg == NULL)
20509 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 break;
20511 }
20512
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020513 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 * at "end". */
20515 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20516 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020517 else if (STRNCMP(p, "if", 2) == 0
20518 || STRNCMP(p, "wh", 2) == 0
20519 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 || STRNCMP(p, "try", 3) == 0)
20521 indent += 2;
20522
20523 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020524 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020526 if (*p == '!')
20527 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 p += eval_fname_script(p);
20529 if (ASCII_ISALPHA(*p))
20530 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020531 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 if (*skipwhite(p) == '(')
20533 {
20534 ++nesting;
20535 indent += 2;
20536 }
20537 }
20538 }
20539
20540 /* Check for ":append" or ":insert". */
20541 p = skip_range(p, NULL);
20542 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20543 || (p[0] == 'i'
20544 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20545 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20546 skip_until = vim_strsave((char_u *)".");
20547
20548 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20549 arg = skipwhite(skiptowhite(p));
20550 if (arg[0] == '<' && arg[1] =='<'
20551 && ((p[0] == 'p' && p[1] == 'y'
20552 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20553 || (p[0] == 'p' && p[1] == 'e'
20554 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20555 || (p[0] == 't' && p[1] == 'c'
20556 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20557 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20558 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020559 || (p[0] == 'm' && p[1] == 'z'
20560 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561 ))
20562 {
20563 /* ":python <<" continues until a dot, like ":append" */
20564 p = skipwhite(arg + 2);
20565 if (*p == NUL)
20566 skip_until = vim_strsave((char_u *)".");
20567 else
20568 skip_until = vim_strsave(p);
20569 }
20570 }
20571
20572 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020573 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020574 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020575 if (line_arg == NULL)
20576 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020578 }
20579
20580 /* Copy the line to newly allocated memory. get_one_sourceline()
20581 * allocates 250 bytes per line, this saves 80% on average. The cost
20582 * is an extra alloc/free. */
20583 p = vim_strsave(theline);
20584 if (p != NULL)
20585 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020586 if (line_arg == NULL)
20587 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020588 theline = p;
20589 }
20590
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020591 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20592
20593 /* Add NULL lines for continuation lines, so that the line count is
20594 * equal to the index in the growarray. */
20595 while (sourcing_lnum_off-- > 0)
20596 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020597
20598 /* Check for end of eap->arg. */
20599 if (line_arg != NULL && *line_arg == NUL)
20600 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601 }
20602
20603 /* Don't define the function when skipping commands or when an error was
20604 * detected. */
20605 if (eap->skip || did_emsg)
20606 goto erret;
20607
20608 /*
20609 * If there are no errors, add the function
20610 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020611 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020612 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020613 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020614 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020615 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020616 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020617 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020618 goto erret;
20619 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020620
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020621 fp = find_func(name);
20622 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020624 if (!eap->forceit)
20625 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020626 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020627 goto erret;
20628 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020629 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020630 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020631 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020632 name);
20633 goto erret;
20634 }
20635 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020636 ga_clear_strings(&(fp->uf_args));
20637 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020638 vim_free(name);
20639 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 }
20642 else
20643 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020644 char numbuf[20];
20645
20646 fp = NULL;
20647 if (fudi.fd_newkey == NULL && !eap->forceit)
20648 {
20649 EMSG(_(e_funcdict));
20650 goto erret;
20651 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020652 if (fudi.fd_di == NULL)
20653 {
20654 /* Can't add a function to a locked dictionary */
20655 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20656 goto erret;
20657 }
20658 /* Can't change an existing function if it is locked */
20659 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20660 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020661
20662 /* Give the function a sequential number. Can only be used with a
20663 * Funcref! */
20664 vim_free(name);
20665 sprintf(numbuf, "%d", ++func_nr);
20666 name = vim_strsave((char_u *)numbuf);
20667 if (name == NULL)
20668 goto erret;
20669 }
20670
20671 if (fp == NULL)
20672 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020673 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020674 {
20675 int slen, plen;
20676 char_u *scriptname;
20677
20678 /* Check that the autoload name matches the script name. */
20679 j = FAIL;
20680 if (sourcing_name != NULL)
20681 {
20682 scriptname = autoload_name(name);
20683 if (scriptname != NULL)
20684 {
20685 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020686 plen = (int)STRLEN(p);
20687 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020688 if (slen > plen && fnamecmp(p,
20689 sourcing_name + slen - plen) == 0)
20690 j = OK;
20691 vim_free(scriptname);
20692 }
20693 }
20694 if (j == FAIL)
20695 {
20696 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20697 goto erret;
20698 }
20699 }
20700
20701 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 if (fp == NULL)
20703 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020704
20705 if (fudi.fd_dict != NULL)
20706 {
20707 if (fudi.fd_di == NULL)
20708 {
20709 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020710 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020711 if (fudi.fd_di == NULL)
20712 {
20713 vim_free(fp);
20714 goto erret;
20715 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020716 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20717 {
20718 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020719 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020720 goto erret;
20721 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020722 }
20723 else
20724 /* overwrite existing dict entry */
20725 clear_tv(&fudi.fd_di->di_tv);
20726 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020727 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020728 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020729 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020730
20731 /* behave like "dict" was used */
20732 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020733 }
20734
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020736 STRCPY(fp->uf_name, name);
20737 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020739 fp->uf_args = newargs;
20740 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020741#ifdef FEAT_PROFILE
20742 fp->uf_tml_count = NULL;
20743 fp->uf_tml_total = NULL;
20744 fp->uf_tml_self = NULL;
20745 fp->uf_profiling = FALSE;
20746 if (prof_def_func())
20747 func_do_profile(fp);
20748#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020749 fp->uf_varargs = varargs;
20750 fp->uf_flags = flags;
20751 fp->uf_calls = 0;
20752 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020753 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754
20755erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756 ga_clear_strings(&newargs);
20757 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020758ret_free:
20759 vim_free(skip_until);
20760 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763}
20764
20765/*
20766 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020767 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020769 * flags:
20770 * TFN_INT: internal function name OK
20771 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772 * Advances "pp" to just after the function name (if no error).
20773 */
20774 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020775trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 char_u **pp;
20777 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020778 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020779 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020781 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 char_u *start;
20783 char_u *end;
20784 int lead;
20785 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020787 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020788
20789 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020790 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020792
20793 /* Check for hard coded <SNR>: already translated function ID (from a user
20794 * command). */
20795 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20796 && (*pp)[2] == (int)KE_SNR)
20797 {
20798 *pp += 3;
20799 len = get_id_len(pp) + 3;
20800 return vim_strnsave(start, len);
20801 }
20802
20803 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20804 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020806 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020808
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020809 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20810 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020811 if (end == start)
20812 {
20813 if (!skip)
20814 EMSG(_("E129: Function name required"));
20815 goto theend;
20816 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020817 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020818 {
20819 /*
20820 * Report an invalid expression in braces, unless the expression
20821 * evaluation has been cancelled due to an aborting error, an
20822 * interrupt, or an exception.
20823 */
20824 if (!aborting())
20825 {
20826 if (end != NULL)
20827 EMSG2(_(e_invarg2), start);
20828 }
20829 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020830 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020831 goto theend;
20832 }
20833
20834 if (lv.ll_tv != NULL)
20835 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020836 if (fdp != NULL)
20837 {
20838 fdp->fd_dict = lv.ll_dict;
20839 fdp->fd_newkey = lv.ll_newkey;
20840 lv.ll_newkey = NULL;
20841 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020842 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020843 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20844 {
20845 name = vim_strsave(lv.ll_tv->vval.v_string);
20846 *pp = end;
20847 }
20848 else
20849 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020850 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20851 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020852 EMSG(_(e_funcref));
20853 else
20854 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020855 name = NULL;
20856 }
20857 goto theend;
20858 }
20859
20860 if (lv.ll_name == NULL)
20861 {
20862 /* Error found, but continue after the function name. */
20863 *pp = end;
20864 goto theend;
20865 }
20866
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020867 /* Check if the name is a Funcref. If so, use the value. */
20868 if (lv.ll_exp_name != NULL)
20869 {
20870 len = (int)STRLEN(lv.ll_exp_name);
20871 name = deref_func_name(lv.ll_exp_name, &len);
20872 if (name == lv.ll_exp_name)
20873 name = NULL;
20874 }
20875 else
20876 {
20877 len = (int)(end - *pp);
20878 name = deref_func_name(*pp, &len);
20879 if (name == *pp)
20880 name = NULL;
20881 }
20882 if (name != NULL)
20883 {
20884 name = vim_strsave(name);
20885 *pp = end;
20886 goto theend;
20887 }
20888
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020889 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020890 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020891 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020892 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20893 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20894 {
20895 /* When there was "s:" already or the name expanded to get a
20896 * leading "s:" then remove it. */
20897 lv.ll_name += 2;
20898 len -= 2;
20899 lead = 2;
20900 }
20901 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020902 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020903 {
20904 if (lead == 2) /* skip over "s:" */
20905 lv.ll_name += 2;
20906 len = (int)(end - lv.ll_name);
20907 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020908
20909 /*
20910 * Copy the function name to allocated memory.
20911 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20912 * Accept <SNR>123_name() outside a script.
20913 */
20914 if (skip)
20915 lead = 0; /* do nothing */
20916 else if (lead > 0)
20917 {
20918 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020919 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20920 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020921 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020922 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020923 if (current_SID <= 0)
20924 {
20925 EMSG(_(e_usingsid));
20926 goto theend;
20927 }
20928 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20929 lead += (int)STRLEN(sid_buf);
20930 }
20931 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020932 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020933 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020934 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020935 goto theend;
20936 }
20937 name = alloc((unsigned)(len + lead + 1));
20938 if (name != NULL)
20939 {
20940 if (lead > 0)
20941 {
20942 name[0] = K_SPECIAL;
20943 name[1] = KS_EXTRA;
20944 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020945 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020946 STRCPY(name + 3, sid_buf);
20947 }
20948 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20949 name[len + lead] = NUL;
20950 }
20951 *pp = end;
20952
20953theend:
20954 clear_lval(&lv);
20955 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956}
20957
20958/*
20959 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20960 * Return 2 if "p" starts with "s:".
20961 * Return 0 otherwise.
20962 */
20963 static int
20964eval_fname_script(p)
20965 char_u *p;
20966{
20967 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20968 || STRNICMP(p + 1, "SNR>", 4) == 0))
20969 return 5;
20970 if (p[0] == 's' && p[1] == ':')
20971 return 2;
20972 return 0;
20973}
20974
20975/*
20976 * Return TRUE if "p" starts with "<SID>" or "s:".
20977 * Only works if eval_fname_script() returned non-zero for "p"!
20978 */
20979 static int
20980eval_fname_sid(p)
20981 char_u *p;
20982{
20983 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20984}
20985
20986/*
20987 * List the head of the function: "name(arg1, arg2)".
20988 */
20989 static void
20990list_func_head(fp, indent)
20991 ufunc_T *fp;
20992 int indent;
20993{
20994 int j;
20995
20996 msg_start();
20997 if (indent)
20998 MSG_PUTS(" ");
20999 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021000 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 {
21002 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021003 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 }
21005 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021006 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021008 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 {
21010 if (j)
21011 MSG_PUTS(", ");
21012 msg_puts(FUNCARG(fp, j));
21013 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021014 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 {
21016 if (j)
21017 MSG_PUTS(", ");
21018 MSG_PUTS("...");
21019 }
21020 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021021 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021022 if (p_verbose > 0)
21023 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024}
21025
21026/*
21027 * Find a function by name, return pointer to it in ufuncs.
21028 * Return NULL for unknown function.
21029 */
21030 static ufunc_T *
21031find_func(name)
21032 char_u *name;
21033{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021034 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021036 hi = hash_find(&func_hashtab, name);
21037 if (!HASHITEM_EMPTY(hi))
21038 return HI2UF(hi);
21039 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040}
21041
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021042#if defined(EXITFREE) || defined(PROTO)
21043 void
21044free_all_functions()
21045{
21046 hashitem_T *hi;
21047
21048 /* Need to start all over every time, because func_free() may change the
21049 * hash table. */
21050 while (func_hashtab.ht_used > 0)
21051 for (hi = func_hashtab.ht_array; ; ++hi)
21052 if (!HASHITEM_EMPTY(hi))
21053 {
21054 func_free(HI2UF(hi));
21055 break;
21056 }
21057}
21058#endif
21059
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021060/*
21061 * Return TRUE if a function "name" exists.
21062 */
21063 static int
21064function_exists(name)
21065 char_u *name;
21066{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021067 char_u *nm = name;
21068 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021069 int n = FALSE;
21070
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021071 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021072 nm = skipwhite(nm);
21073
21074 /* Only accept "funcname", "funcname ", "funcname (..." and
21075 * "funcname(...", not "funcname!...". */
21076 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021077 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021078 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021079 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021080 else
21081 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021082 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021083 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021084 return n;
21085}
21086
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021087/*
21088 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021089 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021090 */
21091 static int
21092builtin_function(name)
21093 char_u *name;
21094{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021095 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21096 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021097}
21098
Bram Moolenaar05159a02005-02-26 23:04:13 +000021099#if defined(FEAT_PROFILE) || defined(PROTO)
21100/*
21101 * Start profiling function "fp".
21102 */
21103 static void
21104func_do_profile(fp)
21105 ufunc_T *fp;
21106{
21107 fp->uf_tm_count = 0;
21108 profile_zero(&fp->uf_tm_self);
21109 profile_zero(&fp->uf_tm_total);
21110 if (fp->uf_tml_count == NULL)
21111 fp->uf_tml_count = (int *)alloc_clear((unsigned)
21112 (sizeof(int) * fp->uf_lines.ga_len));
21113 if (fp->uf_tml_total == NULL)
21114 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
21115 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21116 if (fp->uf_tml_self == NULL)
21117 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
21118 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21119 fp->uf_tml_idx = -1;
21120 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21121 || fp->uf_tml_self == NULL)
21122 return; /* out of memory */
21123
21124 fp->uf_profiling = TRUE;
21125}
21126
21127/*
21128 * Dump the profiling results for all functions in file "fd".
21129 */
21130 void
21131func_dump_profile(fd)
21132 FILE *fd;
21133{
21134 hashitem_T *hi;
21135 int todo;
21136 ufunc_T *fp;
21137 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021138 ufunc_T **sorttab;
21139 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021140
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021141 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021142 if (todo == 0)
21143 return; /* nothing to dump */
21144
Bram Moolenaar73830342005-02-28 22:48:19 +000021145 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21146
Bram Moolenaar05159a02005-02-26 23:04:13 +000021147 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21148 {
21149 if (!HASHITEM_EMPTY(hi))
21150 {
21151 --todo;
21152 fp = HI2UF(hi);
21153 if (fp->uf_profiling)
21154 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021155 if (sorttab != NULL)
21156 sorttab[st_len++] = fp;
21157
Bram Moolenaar05159a02005-02-26 23:04:13 +000021158 if (fp->uf_name[0] == K_SPECIAL)
21159 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21160 else
21161 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21162 if (fp->uf_tm_count == 1)
21163 fprintf(fd, "Called 1 time\n");
21164 else
21165 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21166 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21167 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21168 fprintf(fd, "\n");
21169 fprintf(fd, "count total (s) self (s)\n");
21170
21171 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21172 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021173 if (FUNCLINE(fp, i) == NULL)
21174 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021175 prof_func_line(fd, fp->uf_tml_count[i],
21176 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021177 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21178 }
21179 fprintf(fd, "\n");
21180 }
21181 }
21182 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021183
21184 if (sorttab != NULL && st_len > 0)
21185 {
21186 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21187 prof_total_cmp);
21188 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21189 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21190 prof_self_cmp);
21191 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21192 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021193
21194 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021195}
Bram Moolenaar73830342005-02-28 22:48:19 +000021196
21197 static void
21198prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21199 FILE *fd;
21200 ufunc_T **sorttab;
21201 int st_len;
21202 char *title;
21203 int prefer_self; /* when equal print only self time */
21204{
21205 int i;
21206 ufunc_T *fp;
21207
21208 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21209 fprintf(fd, "count total (s) self (s) function\n");
21210 for (i = 0; i < 20 && i < st_len; ++i)
21211 {
21212 fp = sorttab[i];
21213 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21214 prefer_self);
21215 if (fp->uf_name[0] == K_SPECIAL)
21216 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21217 else
21218 fprintf(fd, " %s()\n", fp->uf_name);
21219 }
21220 fprintf(fd, "\n");
21221}
21222
21223/*
21224 * Print the count and times for one function or function line.
21225 */
21226 static void
21227prof_func_line(fd, count, total, self, prefer_self)
21228 FILE *fd;
21229 int count;
21230 proftime_T *total;
21231 proftime_T *self;
21232 int prefer_self; /* when equal print only self time */
21233{
21234 if (count > 0)
21235 {
21236 fprintf(fd, "%5d ", count);
21237 if (prefer_self && profile_equal(total, self))
21238 fprintf(fd, " ");
21239 else
21240 fprintf(fd, "%s ", profile_msg(total));
21241 if (!prefer_self && profile_equal(total, self))
21242 fprintf(fd, " ");
21243 else
21244 fprintf(fd, "%s ", profile_msg(self));
21245 }
21246 else
21247 fprintf(fd, " ");
21248}
21249
21250/*
21251 * Compare function for total time sorting.
21252 */
21253 static int
21254#ifdef __BORLANDC__
21255_RTLENTRYF
21256#endif
21257prof_total_cmp(s1, s2)
21258 const void *s1;
21259 const void *s2;
21260{
21261 ufunc_T *p1, *p2;
21262
21263 p1 = *(ufunc_T **)s1;
21264 p2 = *(ufunc_T **)s2;
21265 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21266}
21267
21268/*
21269 * Compare function for self time sorting.
21270 */
21271 static int
21272#ifdef __BORLANDC__
21273_RTLENTRYF
21274#endif
21275prof_self_cmp(s1, s2)
21276 const void *s1;
21277 const void *s2;
21278{
21279 ufunc_T *p1, *p2;
21280
21281 p1 = *(ufunc_T **)s1;
21282 p2 = *(ufunc_T **)s2;
21283 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21284}
21285
Bram Moolenaar05159a02005-02-26 23:04:13 +000021286#endif
21287
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021288/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021289 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021290 * Return TRUE if a package was loaded.
21291 */
21292 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021293script_autoload(name, reload)
21294 char_u *name;
21295 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021296{
21297 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021298 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021299 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021300 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021301
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021302 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021303 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021304 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021305 return FALSE;
21306
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021307 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021308
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021309 /* Find the name in the list of previously loaded package names. Skip
21310 * "autoload/", it's always the same. */
21311 for (i = 0; i < ga_loaded.ga_len; ++i)
21312 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21313 break;
21314 if (!reload && i < ga_loaded.ga_len)
21315 ret = FALSE; /* was loaded already */
21316 else
21317 {
21318 /* Remember the name if it wasn't loaded already. */
21319 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21320 {
21321 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21322 tofree = NULL;
21323 }
21324
21325 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021326 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021327 ret = TRUE;
21328 }
21329
21330 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021331 return ret;
21332}
21333
21334/*
21335 * Return the autoload script name for a function or variable name.
21336 * Returns NULL when out of memory.
21337 */
21338 static char_u *
21339autoload_name(name)
21340 char_u *name;
21341{
21342 char_u *p;
21343 char_u *scriptname;
21344
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021345 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021346 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21347 if (scriptname == NULL)
21348 return FALSE;
21349 STRCPY(scriptname, "autoload/");
21350 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021351 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021352 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021353 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021354 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021355 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021356}
21357
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21359
21360/*
21361 * Function given to ExpandGeneric() to obtain the list of user defined
21362 * function names.
21363 */
21364 char_u *
21365get_user_func_name(xp, idx)
21366 expand_T *xp;
21367 int idx;
21368{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021369 static long_u done;
21370 static hashitem_T *hi;
21371 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372
21373 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021375 done = 0;
21376 hi = func_hashtab.ht_array;
21377 }
21378 if (done < func_hashtab.ht_used)
21379 {
21380 if (done++ > 0)
21381 ++hi;
21382 while (HASHITEM_EMPTY(hi))
21383 ++hi;
21384 fp = HI2UF(hi);
21385
21386 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21387 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388
21389 cat_func_name(IObuff, fp);
21390 if (xp->xp_context != EXPAND_USER_FUNC)
21391 {
21392 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021393 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 STRCAT(IObuff, ")");
21395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 return IObuff;
21397 }
21398 return NULL;
21399}
21400
21401#endif /* FEAT_CMDL_COMPL */
21402
21403/*
21404 * Copy the function name of "fp" to buffer "buf".
21405 * "buf" must be able to hold the function name plus three bytes.
21406 * Takes care of script-local function names.
21407 */
21408 static void
21409cat_func_name(buf, fp)
21410 char_u *buf;
21411 ufunc_T *fp;
21412{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021413 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 {
21415 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021416 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 }
21418 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021419 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420}
21421
21422/*
21423 * ":delfunction {name}"
21424 */
21425 void
21426ex_delfunction(eap)
21427 exarg_T *eap;
21428{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021429 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 char_u *p;
21431 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021432 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433
21434 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021435 name = trans_function_name(&p, eap->skip, 0, &fudi);
21436 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021438 {
21439 if (fudi.fd_dict != NULL && !eap->skip)
21440 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 if (!ends_excmd(*skipwhite(p)))
21444 {
21445 vim_free(name);
21446 EMSG(_(e_trailing));
21447 return;
21448 }
21449 eap->nextcmd = check_nextcmd(p);
21450 if (eap->nextcmd != NULL)
21451 *p = NUL;
21452
21453 if (!eap->skip)
21454 fp = find_func(name);
21455 vim_free(name);
21456
21457 if (!eap->skip)
21458 {
21459 if (fp == NULL)
21460 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021461 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462 return;
21463 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021464 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465 {
21466 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21467 return;
21468 }
21469
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021470 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021472 /* Delete the dict item that refers to the function, it will
21473 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021474 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021476 else
21477 func_free(fp);
21478 }
21479}
21480
21481/*
21482 * Free a function and remove it from the list of functions.
21483 */
21484 static void
21485func_free(fp)
21486 ufunc_T *fp;
21487{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021488 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021489
21490 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021491 ga_clear_strings(&(fp->uf_args));
21492 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021493#ifdef FEAT_PROFILE
21494 vim_free(fp->uf_tml_count);
21495 vim_free(fp->uf_tml_total);
21496 vim_free(fp->uf_tml_self);
21497#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021498
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021499 /* remove the function from the function hashtable */
21500 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21501 if (HASHITEM_EMPTY(hi))
21502 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021503 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021504 hash_remove(&func_hashtab, hi);
21505
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021506 vim_free(fp);
21507}
21508
21509/*
21510 * Unreference a Function: decrement the reference count and free it when it
21511 * becomes zero. Only for numbered functions.
21512 */
21513 static void
21514func_unref(name)
21515 char_u *name;
21516{
21517 ufunc_T *fp;
21518
21519 if (name != NULL && isdigit(*name))
21520 {
21521 fp = find_func(name);
21522 if (fp == NULL)
21523 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021524 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021525 {
21526 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021527 * when "uf_calls" becomes zero. */
21528 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021529 func_free(fp);
21530 }
21531 }
21532}
21533
21534/*
21535 * Count a reference to a Function.
21536 */
21537 static void
21538func_ref(name)
21539 char_u *name;
21540{
21541 ufunc_T *fp;
21542
21543 if (name != NULL && isdigit(*name))
21544 {
21545 fp = find_func(name);
21546 if (fp == NULL)
21547 EMSG2(_(e_intern2), "func_ref()");
21548 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021549 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550 }
21551}
21552
21553/*
21554 * Call a user function.
21555 */
21556 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021557call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 ufunc_T *fp; /* pointer to function */
21559 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021560 typval_T *argvars; /* arguments */
21561 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562 linenr_T firstline; /* first line of range */
21563 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021564 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565{
Bram Moolenaar33570922005-01-25 22:26:29 +000021566 char_u *save_sourcing_name;
21567 linenr_T save_sourcing_lnum;
21568 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021569 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021570 int save_did_emsg;
21571 static int depth = 0;
21572 dictitem_T *v;
21573 int fixvar_idx = 0; /* index in fixvar[] */
21574 int i;
21575 int ai;
21576 char_u numbuf[NUMBUFLEN];
21577 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021578#ifdef FEAT_PROFILE
21579 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021580 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582
21583 /* If depth of calling is getting too high, don't execute the function */
21584 if (depth >= p_mfd)
21585 {
21586 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021587 rettv->v_type = VAR_NUMBER;
21588 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 return;
21590 }
21591 ++depth;
21592
21593 line_breakcheck(); /* check for CTRL-C hit */
21594
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021595 fc = (funccall_T *)alloc(sizeof(funccall_T));
21596 fc->caller = current_funccal;
21597 current_funccal = fc;
21598 fc->func = fp;
21599 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021600 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021601 fc->linenr = 0;
21602 fc->returned = FALSE;
21603 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021605 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21606 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607
Bram Moolenaar33570922005-01-25 22:26:29 +000021608 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021609 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021610 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21611 * each argument variable and saves a lot of time.
21612 */
21613 /*
21614 * Init l: variables.
21615 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021616 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021617 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021618 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021619 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21620 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021621 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021622 name = v->di_key;
21623 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021624 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021625 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021626 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021627 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021628 v->di_tv.vval.v_dict = selfdict;
21629 ++selfdict->dv_refcount;
21630 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021631
Bram Moolenaar33570922005-01-25 22:26:29 +000021632 /*
21633 * Init a: variables.
21634 * Set a:0 to "argcount".
21635 * Set a:000 to a list with room for the "..." arguments.
21636 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021637 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21638 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021639 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021640 /* Use "name" to avoid a warning from some compiler that checks the
21641 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021642 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021643 name = v->di_key;
21644 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021645 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021646 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021647 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021648 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021649 v->di_tv.vval.v_list = &fc->l_varlist;
21650 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21651 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21652 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021653
21654 /*
21655 * Set a:firstline to "firstline" and a:lastline to "lastline".
21656 * Set a:name to named arguments.
21657 * Set a:N to the "..." arguments.
21658 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021659 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021660 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021661 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021662 (varnumber_T)lastline);
21663 for (i = 0; i < argcount; ++i)
21664 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021665 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021666 if (ai < 0)
21667 /* named argument a:name */
21668 name = FUNCARG(fp, i);
21669 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021670 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021671 /* "..." argument a:1, a:2, etc. */
21672 sprintf((char *)numbuf, "%d", ai + 1);
21673 name = numbuf;
21674 }
21675 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21676 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021677 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021678 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21679 }
21680 else
21681 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021682 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21683 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021684 if (v == NULL)
21685 break;
21686 v->di_flags = DI_FLAGS_RO;
21687 }
21688 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021689 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021690
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021691 /* Note: the values are copied directly to avoid alloc/free.
21692 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021693 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021694 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021695
21696 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21697 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021698 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21699 fc->l_listitems[ai].li_tv = argvars[i];
21700 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021701 }
21702 }
21703
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 /* Don't redraw while executing the function. */
21705 ++RedrawingDisabled;
21706 save_sourcing_name = sourcing_name;
21707 save_sourcing_lnum = sourcing_lnum;
21708 sourcing_lnum = 1;
21709 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021710 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711 if (sourcing_name != NULL)
21712 {
21713 if (save_sourcing_name != NULL
21714 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21715 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21716 else
21717 STRCPY(sourcing_name, "function ");
21718 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21719
21720 if (p_verbose >= 12)
21721 {
21722 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021723 verbose_enter_scroll();
21724
Bram Moolenaar555b2802005-05-19 21:08:39 +000021725 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726 if (p_verbose >= 14)
21727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021729 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021730 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021731 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732
21733 msg_puts((char_u *)"(");
21734 for (i = 0; i < argcount; ++i)
21735 {
21736 if (i > 0)
21737 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021738 if (argvars[i].v_type == VAR_NUMBER)
21739 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 else
21741 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021742 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21743 if (s != NULL)
21744 {
21745 trunc_string(s, buf, MSG_BUF_CLEN);
21746 msg_puts(buf);
21747 vim_free(tofree);
21748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 }
21750 }
21751 msg_puts((char_u *)")");
21752 }
21753 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021754
21755 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 --no_wait_return;
21757 }
21758 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021759#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021760 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021761 {
21762 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21763 func_do_profile(fp);
21764 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021765 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021766 {
21767 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021768 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021769 profile_zero(&fp->uf_tm_children);
21770 }
21771 script_prof_save(&wait_start);
21772 }
21773#endif
21774
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021776 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 save_did_emsg = did_emsg;
21778 did_emsg = FALSE;
21779
21780 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021781 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21783
21784 --RedrawingDisabled;
21785
21786 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021787 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021789 clear_tv(rettv);
21790 rettv->v_type = VAR_NUMBER;
21791 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 }
21793
Bram Moolenaar05159a02005-02-26 23:04:13 +000021794#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021795 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021796 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021797 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021798 profile_end(&call_start);
21799 profile_sub_wait(&wait_start, &call_start);
21800 profile_add(&fp->uf_tm_total, &call_start);
21801 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021802 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021803 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021804 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21805 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021806 }
21807 }
21808#endif
21809
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810 /* when being verbose, mention the return value */
21811 if (p_verbose >= 12)
21812 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021814 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021817 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021818 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021819 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021820 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021821 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021823 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021824 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021825 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021826 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021827
Bram Moolenaar555b2802005-05-19 21:08:39 +000021828 /* The value may be very long. Skip the middle part, so that we
21829 * have some idea how it starts and ends. smsg() would always
21830 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021831 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021832 if (s != NULL)
21833 {
21834 trunc_string(s, buf, MSG_BUF_CLEN);
21835 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21836 vim_free(tofree);
21837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 }
21839 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021840
21841 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 --no_wait_return;
21843 }
21844
21845 vim_free(sourcing_name);
21846 sourcing_name = save_sourcing_name;
21847 sourcing_lnum = save_sourcing_lnum;
21848 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021849#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021850 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021851 script_prof_restore(&wait_start);
21852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853
21854 if (p_verbose >= 12 && sourcing_name != NULL)
21855 {
21856 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021857 verbose_enter_scroll();
21858
Bram Moolenaar555b2802005-05-19 21:08:39 +000021859 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021861
21862 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 --no_wait_return;
21864 }
21865
21866 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021867 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021869
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021870 /* If the a:000 list and the l: and a: dicts are not referenced we can
21871 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021872 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21873 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21874 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21875 {
21876 free_funccal(fc, FALSE);
21877 }
21878 else
21879 {
21880 hashitem_T *hi;
21881 listitem_T *li;
21882 int todo;
21883
21884 /* "fc" is still in use. This can happen when returning "a:000" or
21885 * assigning "l:" to a global variable.
21886 * Link "fc" in the list for garbage collection later. */
21887 fc->caller = previous_funccal;
21888 previous_funccal = fc;
21889
21890 /* Make a copy of the a: variables, since we didn't do that above. */
21891 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21892 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21893 {
21894 if (!HASHITEM_EMPTY(hi))
21895 {
21896 --todo;
21897 v = HI2DI(hi);
21898 copy_tv(&v->di_tv, &v->di_tv);
21899 }
21900 }
21901
21902 /* Make a copy of the a:000 items, since we didn't do that above. */
21903 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21904 copy_tv(&li->li_tv, &li->li_tv);
21905 }
21906}
21907
21908/*
21909 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021910 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021911 */
21912 static int
21913can_free_funccal(fc, copyID)
21914 funccall_T *fc;
21915 int copyID;
21916{
21917 return (fc->l_varlist.lv_copyID != copyID
21918 && fc->l_vars.dv_copyID != copyID
21919 && fc->l_avars.dv_copyID != copyID);
21920}
21921
21922/*
21923 * Free "fc" and what it contains.
21924 */
21925 static void
21926free_funccal(fc, free_val)
21927 funccall_T *fc;
21928 int free_val; /* a: vars were allocated */
21929{
21930 listitem_T *li;
21931
21932 /* The a: variables typevals may not have been allocated, only free the
21933 * allocated variables. */
21934 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21935
21936 /* free all l: variables */
21937 vars_clear(&fc->l_vars.dv_hashtab);
21938
21939 /* Free the a:000 variables if they were allocated. */
21940 if (free_val)
21941 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21942 clear_tv(&li->li_tv);
21943
21944 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945}
21946
21947/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021948 * Add a number variable "name" to dict "dp" with value "nr".
21949 */
21950 static void
21951add_nr_var(dp, v, name, nr)
21952 dict_T *dp;
21953 dictitem_T *v;
21954 char *name;
21955 varnumber_T nr;
21956{
21957 STRCPY(v->di_key, name);
21958 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21959 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21960 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021961 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021962 v->di_tv.vval.v_number = nr;
21963}
21964
21965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 * ":return [expr]"
21967 */
21968 void
21969ex_return(eap)
21970 exarg_T *eap;
21971{
21972 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021973 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 int returning = FALSE;
21975
21976 if (current_funccal == NULL)
21977 {
21978 EMSG(_("E133: :return not inside a function"));
21979 return;
21980 }
21981
21982 if (eap->skip)
21983 ++emsg_skip;
21984
21985 eap->nextcmd = NULL;
21986 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021987 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 {
21989 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021990 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021992 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 }
21994 /* It's safer to return also on error. */
21995 else if (!eap->skip)
21996 {
21997 /*
21998 * Return unless the expression evaluation has been cancelled due to an
21999 * aborting error, an interrupt, or an exception.
22000 */
22001 if (!aborting())
22002 returning = do_return(eap, FALSE, TRUE, NULL);
22003 }
22004
22005 /* When skipping or the return gets pending, advance to the next command
22006 * in this line (!returning). Otherwise, ignore the rest of the line.
22007 * Following lines will be ignored by get_func_line(). */
22008 if (returning)
22009 eap->nextcmd = NULL;
22010 else if (eap->nextcmd == NULL) /* no argument */
22011 eap->nextcmd = check_nextcmd(arg);
22012
22013 if (eap->skip)
22014 --emsg_skip;
22015}
22016
22017/*
22018 * Return from a function. Possibly makes the return pending. Also called
22019 * for a pending return at the ":endtry" or after returning from an extra
22020 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022021 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022022 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 * FALSE when the return gets pending.
22024 */
22025 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022026do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027 exarg_T *eap;
22028 int reanimate;
22029 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022030 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031{
22032 int idx;
22033 struct condstack *cstack = eap->cstack;
22034
22035 if (reanimate)
22036 /* Undo the return. */
22037 current_funccal->returned = FALSE;
22038
22039 /*
22040 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22041 * not in its finally clause (which then is to be executed next) is found.
22042 * In this case, make the ":return" pending for execution at the ":endtry".
22043 * Otherwise, return normally.
22044 */
22045 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22046 if (idx >= 0)
22047 {
22048 cstack->cs_pending[idx] = CSTP_RETURN;
22049
22050 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022051 /* A pending return again gets pending. "rettv" points to an
22052 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022054 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055 else
22056 {
22057 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022058 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022060 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022062 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 {
22064 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022065 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022066 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 else
22068 EMSG(_(e_outofmem));
22069 }
22070 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022071 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072
22073 if (reanimate)
22074 {
22075 /* The pending return value could be overwritten by a ":return"
22076 * without argument in a finally clause; reset the default
22077 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022078 current_funccal->rettv->v_type = VAR_NUMBER;
22079 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080 }
22081 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022082 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 }
22084 else
22085 {
22086 current_funccal->returned = TRUE;
22087
22088 /* If the return is carried out now, store the return value. For
22089 * a return immediately after reanimation, the value is already
22090 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022091 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022093 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022094 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022096 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 }
22098 }
22099
22100 return idx < 0;
22101}
22102
22103/*
22104 * Free the variable with a pending return value.
22105 */
22106 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022107discard_pending_return(rettv)
22108 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109{
Bram Moolenaar33570922005-01-25 22:26:29 +000022110 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111}
22112
22113/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022114 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115 * is an allocated string. Used by report_pending() for verbose messages.
22116 */
22117 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022118get_return_cmd(rettv)
22119 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022121 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022122 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022123 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022125 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022126 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022127 if (s == NULL)
22128 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022129
22130 STRCPY(IObuff, ":return ");
22131 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22132 if (STRLEN(s) + 8 >= IOSIZE)
22133 STRCPY(IObuff + IOSIZE - 4, "...");
22134 vim_free(tofree);
22135 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136}
22137
22138/*
22139 * Get next function line.
22140 * Called by do_cmdline() to get the next line.
22141 * Returns allocated string, or NULL for end of function.
22142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 char_u *
22144get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022145 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022147 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148{
Bram Moolenaar33570922005-01-25 22:26:29 +000022149 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022150 ufunc_T *fp = fcp->func;
22151 char_u *retval;
22152 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153
22154 /* If breakpoints have been added/deleted need to check for it. */
22155 if (fcp->dbg_tick != debug_tick)
22156 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022157 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 sourcing_lnum);
22159 fcp->dbg_tick = debug_tick;
22160 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022161#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022162 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022163 func_line_end(cookie);
22164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165
Bram Moolenaar05159a02005-02-26 23:04:13 +000022166 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022167 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22168 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 retval = NULL;
22170 else
22171 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022172 /* Skip NULL lines (continuation lines). */
22173 while (fcp->linenr < gap->ga_len
22174 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22175 ++fcp->linenr;
22176 if (fcp->linenr >= gap->ga_len)
22177 retval = NULL;
22178 else
22179 {
22180 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22181 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022182#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022183 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022184 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022185#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 }
22188
22189 /* Did we encounter a breakpoint? */
22190 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22191 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022192 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022194 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195 sourcing_lnum);
22196 fcp->dbg_tick = debug_tick;
22197 }
22198
22199 return retval;
22200}
22201
Bram Moolenaar05159a02005-02-26 23:04:13 +000022202#if defined(FEAT_PROFILE) || defined(PROTO)
22203/*
22204 * Called when starting to read a function line.
22205 * "sourcing_lnum" must be correct!
22206 * When skipping lines it may not actually be executed, but we won't find out
22207 * until later and we need to store the time now.
22208 */
22209 void
22210func_line_start(cookie)
22211 void *cookie;
22212{
22213 funccall_T *fcp = (funccall_T *)cookie;
22214 ufunc_T *fp = fcp->func;
22215
22216 if (fp->uf_profiling && sourcing_lnum >= 1
22217 && sourcing_lnum <= fp->uf_lines.ga_len)
22218 {
22219 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022220 /* Skip continuation lines. */
22221 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22222 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022223 fp->uf_tml_execed = FALSE;
22224 profile_start(&fp->uf_tml_start);
22225 profile_zero(&fp->uf_tml_children);
22226 profile_get_wait(&fp->uf_tml_wait);
22227 }
22228}
22229
22230/*
22231 * Called when actually executing a function line.
22232 */
22233 void
22234func_line_exec(cookie)
22235 void *cookie;
22236{
22237 funccall_T *fcp = (funccall_T *)cookie;
22238 ufunc_T *fp = fcp->func;
22239
22240 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22241 fp->uf_tml_execed = TRUE;
22242}
22243
22244/*
22245 * Called when done with a function line.
22246 */
22247 void
22248func_line_end(cookie)
22249 void *cookie;
22250{
22251 funccall_T *fcp = (funccall_T *)cookie;
22252 ufunc_T *fp = fcp->func;
22253
22254 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22255 {
22256 if (fp->uf_tml_execed)
22257 {
22258 ++fp->uf_tml_count[fp->uf_tml_idx];
22259 profile_end(&fp->uf_tml_start);
22260 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022261 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022262 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22263 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022264 }
22265 fp->uf_tml_idx = -1;
22266 }
22267}
22268#endif
22269
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270/*
22271 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022272 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022273 */
22274 int
22275func_has_ended(cookie)
22276 void *cookie;
22277{
Bram Moolenaar33570922005-01-25 22:26:29 +000022278 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279
22280 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22281 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022282 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283 || fcp->returned);
22284}
22285
22286/*
22287 * return TRUE if cookie indicates a function which "abort"s on errors.
22288 */
22289 int
22290func_has_abort(cookie)
22291 void *cookie;
22292{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022293 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294}
22295
22296#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22297typedef enum
22298{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022299 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22300 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22301 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302} var_flavour_T;
22303
22304static var_flavour_T var_flavour __ARGS((char_u *varname));
22305
22306 static var_flavour_T
22307var_flavour(varname)
22308 char_u *varname;
22309{
22310 char_u *p = varname;
22311
22312 if (ASCII_ISUPPER(*p))
22313 {
22314 while (*(++p))
22315 if (ASCII_ISLOWER(*p))
22316 return VAR_FLAVOUR_SESSION;
22317 return VAR_FLAVOUR_VIMINFO;
22318 }
22319 else
22320 return VAR_FLAVOUR_DEFAULT;
22321}
22322#endif
22323
22324#if defined(FEAT_VIMINFO) || defined(PROTO)
22325/*
22326 * Restore global vars that start with a capital from the viminfo file
22327 */
22328 int
22329read_viminfo_varlist(virp, writing)
22330 vir_T *virp;
22331 int writing;
22332{
22333 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022334 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022335 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336
22337 if (!writing && (find_viminfo_parameter('!') != NULL))
22338 {
22339 tab = vim_strchr(virp->vir_line + 1, '\t');
22340 if (tab != NULL)
22341 {
22342 *tab++ = '\0'; /* isolate the variable name */
22343 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022344 type = VAR_STRING;
22345#ifdef FEAT_FLOAT
22346 else if (*tab == 'F')
22347 type = VAR_FLOAT;
22348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349
22350 tab = vim_strchr(tab, '\t');
22351 if (tab != NULL)
22352 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022353 tv.v_type = type;
22354 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022355 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022357#ifdef FEAT_FLOAT
22358 else if (type == VAR_FLOAT)
22359 (void)string2float(tab + 1, &tv.vval.v_float);
22360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022362 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022363 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022364 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022365 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 }
22367 }
22368 }
22369
22370 return viminfo_readline(virp);
22371}
22372
22373/*
22374 * Write global vars that start with a capital to the viminfo file
22375 */
22376 void
22377write_viminfo_varlist(fp)
22378 FILE *fp;
22379{
Bram Moolenaar33570922005-01-25 22:26:29 +000022380 hashitem_T *hi;
22381 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022382 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022383 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022384 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022385 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022386 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387
22388 if (find_viminfo_parameter('!') == NULL)
22389 return;
22390
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022391 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022392
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022393 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022394 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022396 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022398 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022399 this_var = HI2DI(hi);
22400 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022401 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022402 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022403 {
22404 case VAR_STRING: s = "STR"; break;
22405 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022406#ifdef FEAT_FLOAT
22407 case VAR_FLOAT: s = "FLO"; break;
22408#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022409 default: continue;
22410 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022411 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022412 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022413 if (p != NULL)
22414 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022415 vim_free(tofree);
22416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417 }
22418 }
22419}
22420#endif
22421
22422#if defined(FEAT_SESSION) || defined(PROTO)
22423 int
22424store_session_globals(fd)
22425 FILE *fd;
22426{
Bram Moolenaar33570922005-01-25 22:26:29 +000022427 hashitem_T *hi;
22428 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022429 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430 char_u *p, *t;
22431
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022432 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022433 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022435 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022437 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022438 this_var = HI2DI(hi);
22439 if ((this_var->di_tv.v_type == VAR_NUMBER
22440 || this_var->di_tv.v_type == VAR_STRING)
22441 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022442 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022443 /* Escape special characters with a backslash. Turn a LF and
22444 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022445 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022446 (char_u *)"\\\"\n\r");
22447 if (p == NULL) /* out of memory */
22448 break;
22449 for (t = p; *t != NUL; ++t)
22450 if (*t == '\n')
22451 *t = 'n';
22452 else if (*t == '\r')
22453 *t = 'r';
22454 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022455 this_var->di_key,
22456 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22457 : ' ',
22458 p,
22459 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22460 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022461 || put_eol(fd) == FAIL)
22462 {
22463 vim_free(p);
22464 return FAIL;
22465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466 vim_free(p);
22467 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022468#ifdef FEAT_FLOAT
22469 else if (this_var->di_tv.v_type == VAR_FLOAT
22470 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22471 {
22472 float_T f = this_var->di_tv.vval.v_float;
22473 int sign = ' ';
22474
22475 if (f < 0)
22476 {
22477 f = -f;
22478 sign = '-';
22479 }
22480 if ((fprintf(fd, "let %s = %c&%f",
22481 this_var->di_key, sign, f) < 0)
22482 || put_eol(fd) == FAIL)
22483 return FAIL;
22484 }
22485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 }
22487 }
22488 return OK;
22489}
22490#endif
22491
Bram Moolenaar661b1822005-07-28 22:36:45 +000022492/*
22493 * Display script name where an item was last set.
22494 * Should only be invoked when 'verbose' is non-zero.
22495 */
22496 void
22497last_set_msg(scriptID)
22498 scid_T scriptID;
22499{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022500 char_u *p;
22501
Bram Moolenaar661b1822005-07-28 22:36:45 +000022502 if (scriptID != 0)
22503 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022504 p = home_replace_save(NULL, get_scriptname(scriptID));
22505 if (p != NULL)
22506 {
22507 verbose_enter();
22508 MSG_PUTS(_("\n\tLast set from "));
22509 MSG_PUTS(p);
22510 vim_free(p);
22511 verbose_leave();
22512 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022513 }
22514}
22515
Bram Moolenaard812df62008-11-09 12:46:09 +000022516/*
22517 * List v:oldfiles in a nice way.
22518 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022519 void
22520ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022521 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022522{
22523 list_T *l = vimvars[VV_OLDFILES].vv_list;
22524 listitem_T *li;
22525 int nr = 0;
22526
22527 if (l == NULL)
22528 msg((char_u *)_("No old files"));
22529 else
22530 {
22531 msg_start();
22532 msg_scroll = TRUE;
22533 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22534 {
22535 msg_outnum((long)++nr);
22536 MSG_PUTS(": ");
22537 msg_outtrans(get_tv_string(&li->li_tv));
22538 msg_putchar('\n');
22539 out_flush(); /* output one line at a time */
22540 ui_breakcheck();
22541 }
22542 /* Assume "got_int" was set to truncate the listing. */
22543 got_int = FALSE;
22544
22545#ifdef FEAT_BROWSE_CMD
22546 if (cmdmod.browse)
22547 {
22548 quit_more = FALSE;
22549 nr = prompt_for_number(FALSE);
22550 msg_starthere();
22551 if (nr > 0)
22552 {
22553 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22554 (long)nr);
22555
22556 if (p != NULL)
22557 {
22558 p = expand_env_save(p);
22559 eap->arg = p;
22560 eap->cmdidx = CMD_edit;
22561 cmdmod.browse = FALSE;
22562 do_exedit(eap, NULL);
22563 vim_free(p);
22564 }
22565 }
22566 }
22567#endif
22568 }
22569}
22570
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571#endif /* FEAT_EVAL */
22572
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022574#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575
22576#ifdef WIN3264
22577/*
22578 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22579 */
22580static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22581static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22582static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22583
22584/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022585 * Get the short path (8.3) for the filename in "fnamep".
22586 * Only works for a valid file name.
22587 * When the path gets longer "fnamep" is changed and the allocated buffer
22588 * is put in "bufp".
22589 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22590 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591 */
22592 static int
22593get_short_pathname(fnamep, bufp, fnamelen)
22594 char_u **fnamep;
22595 char_u **bufp;
22596 int *fnamelen;
22597{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022598 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 char_u *newbuf;
22600
22601 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 l = GetShortPathName(*fnamep, *fnamep, len);
22603 if (l > len - 1)
22604 {
22605 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022606 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 newbuf = vim_strnsave(*fnamep, l);
22608 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610
22611 vim_free(*bufp);
22612 *fnamep = *bufp = newbuf;
22613
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022614 /* Really should always succeed, as the buffer is big enough. */
22615 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 }
22617
22618 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022619 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620}
22621
22622/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022623 * Get the short path (8.3) for the filename in "fname". The converted
22624 * path is returned in "bufp".
22625 *
22626 * Some of the directories specified in "fname" may not exist. This function
22627 * will shorten the existing directories at the beginning of the path and then
22628 * append the remaining non-existing path.
22629 *
22630 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022631 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022632 * bufp - Pointer to an allocated buffer for the filename.
22633 * fnamelen - Length of the filename pointed to by fname
22634 *
22635 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 */
22637 static int
22638shortpath_for_invalid_fname(fname, bufp, fnamelen)
22639 char_u **fname;
22640 char_u **bufp;
22641 int *fnamelen;
22642{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022643 char_u *short_fname, *save_fname, *pbuf_unused;
22644 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022646 int old_len, len;
22647 int new_len, sfx_len;
22648 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649
22650 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022651 old_len = *fnamelen;
22652 save_fname = vim_strnsave(*fname, old_len);
22653 pbuf_unused = NULL;
22654 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022656 endp = save_fname + old_len - 1; /* Find the end of the copy */
22657 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022659 /*
22660 * Try shortening the supplied path till it succeeds by removing one
22661 * directory at a time from the tail of the path.
22662 */
22663 len = 0;
22664 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022666 /* go back one path-separator */
22667 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22668 --endp;
22669 if (endp <= save_fname)
22670 break; /* processed the complete path */
22671
22672 /*
22673 * Replace the path separator with a NUL and try to shorten the
22674 * resulting path.
22675 */
22676 ch = *endp;
22677 *endp = 0;
22678 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022679 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022680 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22681 {
22682 retval = FAIL;
22683 goto theend;
22684 }
22685 *endp = ch; /* preserve the string */
22686
22687 if (len > 0)
22688 break; /* successfully shortened the path */
22689
22690 /* failed to shorten the path. Skip the path separator */
22691 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 }
22693
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022694 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022696 /*
22697 * Succeeded in shortening the path. Now concatenate the shortened
22698 * path with the remaining path at the tail.
22699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022701 /* Compute the length of the new path. */
22702 sfx_len = (int)(save_endp - endp) + 1;
22703 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022705 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022707 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022709 /* There is not enough space in the currently allocated string,
22710 * copy it to a buffer big enough. */
22711 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022713 {
22714 retval = FAIL;
22715 goto theend;
22716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717 }
22718 else
22719 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022720 /* Transfer short_fname to the main buffer (it's big enough),
22721 * unless get_short_pathname() did its work in-place. */
22722 *fname = *bufp = save_fname;
22723 if (short_fname != save_fname)
22724 vim_strncpy(save_fname, short_fname, len);
22725 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022727
22728 /* concat the not-shortened part of the path */
22729 vim_strncpy(*fname + len, endp, sfx_len);
22730 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022732
22733theend:
22734 vim_free(pbuf_unused);
22735 vim_free(save_fname);
22736
22737 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738}
22739
22740/*
22741 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022742 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743 */
22744 static int
22745shortpath_for_partial(fnamep, bufp, fnamelen)
22746 char_u **fnamep;
22747 char_u **bufp;
22748 int *fnamelen;
22749{
22750 int sepcount, len, tflen;
22751 char_u *p;
22752 char_u *pbuf, *tfname;
22753 int hasTilde;
22754
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022755 /* Count up the path separators from the RHS.. so we know which part
22756 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022758 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 if (vim_ispathsep(*p))
22760 ++sepcount;
22761
22762 /* Need full path first (use expand_env() to remove a "~/") */
22763 hasTilde = (**fnamep == '~');
22764 if (hasTilde)
22765 pbuf = tfname = expand_env_save(*fnamep);
22766 else
22767 pbuf = tfname = FullName_save(*fnamep, FALSE);
22768
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022769 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022771 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22772 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773
22774 if (len == 0)
22775 {
22776 /* Don't have a valid filename, so shorten the rest of the
22777 * path if we can. This CAN give us invalid 8.3 filenames, but
22778 * there's not a lot of point in guessing what it might be.
22779 */
22780 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022781 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22782 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783 }
22784
22785 /* Count the paths backward to find the beginning of the desired string. */
22786 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022787 {
22788#ifdef FEAT_MBYTE
22789 if (has_mbyte)
22790 p -= mb_head_off(tfname, p);
22791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 if (vim_ispathsep(*p))
22793 {
22794 if (sepcount == 0 || (hasTilde && sepcount == 1))
22795 break;
22796 else
22797 sepcount --;
22798 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800 if (hasTilde)
22801 {
22802 --p;
22803 if (p >= tfname)
22804 *p = '~';
22805 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022806 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 }
22808 else
22809 ++p;
22810
22811 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22812 vim_free(*bufp);
22813 *fnamelen = (int)STRLEN(p);
22814 *bufp = pbuf;
22815 *fnamep = p;
22816
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022817 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818}
22819#endif /* WIN3264 */
22820
22821/*
22822 * Adjust a filename, according to a string of modifiers.
22823 * *fnamep must be NUL terminated when called. When returning, the length is
22824 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022825 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 * When there is an error, *fnamep is set to NULL.
22827 */
22828 int
22829modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22830 char_u *src; /* string with modifiers */
22831 int *usedlen; /* characters after src that are used */
22832 char_u **fnamep; /* file name so far */
22833 char_u **bufp; /* buffer for allocated file name or NULL */
22834 int *fnamelen; /* length of fnamep */
22835{
22836 int valid = 0;
22837 char_u *tail;
22838 char_u *s, *p, *pbuf;
22839 char_u dirname[MAXPATHL];
22840 int c;
22841 int has_fullname = 0;
22842#ifdef WIN3264
22843 int has_shortname = 0;
22844#endif
22845
22846repeat:
22847 /* ":p" - full path/file_name */
22848 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22849 {
22850 has_fullname = 1;
22851
22852 valid |= VALID_PATH;
22853 *usedlen += 2;
22854
22855 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22856 if ((*fnamep)[0] == '~'
22857#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22858 && ((*fnamep)[1] == '/'
22859# ifdef BACKSLASH_IN_FILENAME
22860 || (*fnamep)[1] == '\\'
22861# endif
22862 || (*fnamep)[1] == NUL)
22863
22864#endif
22865 )
22866 {
22867 *fnamep = expand_env_save(*fnamep);
22868 vim_free(*bufp); /* free any allocated file name */
22869 *bufp = *fnamep;
22870 if (*fnamep == NULL)
22871 return -1;
22872 }
22873
22874 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022875 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 {
22877 if (vim_ispathsep(*p)
22878 && p[1] == '.'
22879 && (p[2] == NUL
22880 || vim_ispathsep(p[2])
22881 || (p[2] == '.'
22882 && (p[3] == NUL || vim_ispathsep(p[3])))))
22883 break;
22884 }
22885
22886 /* FullName_save() is slow, don't use it when not needed. */
22887 if (*p != NUL || !vim_isAbsName(*fnamep))
22888 {
22889 *fnamep = FullName_save(*fnamep, *p != NUL);
22890 vim_free(*bufp); /* free any allocated file name */
22891 *bufp = *fnamep;
22892 if (*fnamep == NULL)
22893 return -1;
22894 }
22895
22896 /* Append a path separator to a directory. */
22897 if (mch_isdir(*fnamep))
22898 {
22899 /* Make room for one or two extra characters. */
22900 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22901 vim_free(*bufp); /* free any allocated file name */
22902 *bufp = *fnamep;
22903 if (*fnamep == NULL)
22904 return -1;
22905 add_pathsep(*fnamep);
22906 }
22907 }
22908
22909 /* ":." - path relative to the current directory */
22910 /* ":~" - path relative to the home directory */
22911 /* ":8" - shortname path - postponed till after */
22912 while (src[*usedlen] == ':'
22913 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22914 {
22915 *usedlen += 2;
22916 if (c == '8')
22917 {
22918#ifdef WIN3264
22919 has_shortname = 1; /* Postpone this. */
22920#endif
22921 continue;
22922 }
22923 pbuf = NULL;
22924 /* Need full path first (use expand_env() to remove a "~/") */
22925 if (!has_fullname)
22926 {
22927 if (c == '.' && **fnamep == '~')
22928 p = pbuf = expand_env_save(*fnamep);
22929 else
22930 p = pbuf = FullName_save(*fnamep, FALSE);
22931 }
22932 else
22933 p = *fnamep;
22934
22935 has_fullname = 0;
22936
22937 if (p != NULL)
22938 {
22939 if (c == '.')
22940 {
22941 mch_dirname(dirname, MAXPATHL);
22942 s = shorten_fname(p, dirname);
22943 if (s != NULL)
22944 {
22945 *fnamep = s;
22946 if (pbuf != NULL)
22947 {
22948 vim_free(*bufp); /* free any allocated file name */
22949 *bufp = pbuf;
22950 pbuf = NULL;
22951 }
22952 }
22953 }
22954 else
22955 {
22956 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22957 /* Only replace it when it starts with '~' */
22958 if (*dirname == '~')
22959 {
22960 s = vim_strsave(dirname);
22961 if (s != NULL)
22962 {
22963 *fnamep = s;
22964 vim_free(*bufp);
22965 *bufp = s;
22966 }
22967 }
22968 }
22969 vim_free(pbuf);
22970 }
22971 }
22972
22973 tail = gettail(*fnamep);
22974 *fnamelen = (int)STRLEN(*fnamep);
22975
22976 /* ":h" - head, remove "/file_name", can be repeated */
22977 /* Don't remove the first "/" or "c:\" */
22978 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22979 {
22980 valid |= VALID_HEAD;
22981 *usedlen += 2;
22982 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022983 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022984 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 *fnamelen = (int)(tail - *fnamep);
22986#ifdef VMS
22987 if (*fnamelen > 0)
22988 *fnamelen += 1; /* the path separator is part of the path */
22989#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022990 if (*fnamelen == 0)
22991 {
22992 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22993 p = vim_strsave((char_u *)".");
22994 if (p == NULL)
22995 return -1;
22996 vim_free(*bufp);
22997 *bufp = *fnamep = tail = p;
22998 *fnamelen = 1;
22999 }
23000 else
23001 {
23002 while (tail > s && !after_pathsep(s, tail))
23003 mb_ptr_back(*fnamep, tail);
23004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 }
23006
23007 /* ":8" - shortname */
23008 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23009 {
23010 *usedlen += 2;
23011#ifdef WIN3264
23012 has_shortname = 1;
23013#endif
23014 }
23015
23016#ifdef WIN3264
23017 /* Check shortname after we have done 'heads' and before we do 'tails'
23018 */
23019 if (has_shortname)
23020 {
23021 pbuf = NULL;
23022 /* Copy the string if it is shortened by :h */
23023 if (*fnamelen < (int)STRLEN(*fnamep))
23024 {
23025 p = vim_strnsave(*fnamep, *fnamelen);
23026 if (p == 0)
23027 return -1;
23028 vim_free(*bufp);
23029 *bufp = *fnamep = p;
23030 }
23031
23032 /* Split into two implementations - makes it easier. First is where
23033 * there isn't a full name already, second is where there is.
23034 */
23035 if (!has_fullname && !vim_isAbsName(*fnamep))
23036 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023037 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 return -1;
23039 }
23040 else
23041 {
23042 int l;
23043
23044 /* Simple case, already have the full-name
23045 * Nearly always shorter, so try first time. */
23046 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023047 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023048 return -1;
23049
23050 if (l == 0)
23051 {
23052 /* Couldn't find the filename.. search the paths.
23053 */
23054 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023055 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 return -1;
23057 }
23058 *fnamelen = l;
23059 }
23060 }
23061#endif /* WIN3264 */
23062
23063 /* ":t" - tail, just the basename */
23064 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23065 {
23066 *usedlen += 2;
23067 *fnamelen -= (int)(tail - *fnamep);
23068 *fnamep = tail;
23069 }
23070
23071 /* ":e" - extension, can be repeated */
23072 /* ":r" - root, without extension, can be repeated */
23073 while (src[*usedlen] == ':'
23074 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23075 {
23076 /* find a '.' in the tail:
23077 * - for second :e: before the current fname
23078 * - otherwise: The last '.'
23079 */
23080 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23081 s = *fnamep - 2;
23082 else
23083 s = *fnamep + *fnamelen - 1;
23084 for ( ; s > tail; --s)
23085 if (s[0] == '.')
23086 break;
23087 if (src[*usedlen + 1] == 'e') /* :e */
23088 {
23089 if (s > tail)
23090 {
23091 *fnamelen += (int)(*fnamep - (s + 1));
23092 *fnamep = s + 1;
23093#ifdef VMS
23094 /* cut version from the extension */
23095 s = *fnamep + *fnamelen - 1;
23096 for ( ; s > *fnamep; --s)
23097 if (s[0] == ';')
23098 break;
23099 if (s > *fnamep)
23100 *fnamelen = s - *fnamep;
23101#endif
23102 }
23103 else if (*fnamep <= tail)
23104 *fnamelen = 0;
23105 }
23106 else /* :r */
23107 {
23108 if (s > tail) /* remove one extension */
23109 *fnamelen = (int)(s - *fnamep);
23110 }
23111 *usedlen += 2;
23112 }
23113
23114 /* ":s?pat?foo?" - substitute */
23115 /* ":gs?pat?foo?" - global substitute */
23116 if (src[*usedlen] == ':'
23117 && (src[*usedlen + 1] == 's'
23118 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23119 {
23120 char_u *str;
23121 char_u *pat;
23122 char_u *sub;
23123 int sep;
23124 char_u *flags;
23125 int didit = FALSE;
23126
23127 flags = (char_u *)"";
23128 s = src + *usedlen + 2;
23129 if (src[*usedlen + 1] == 'g')
23130 {
23131 flags = (char_u *)"g";
23132 ++s;
23133 }
23134
23135 sep = *s++;
23136 if (sep)
23137 {
23138 /* find end of pattern */
23139 p = vim_strchr(s, sep);
23140 if (p != NULL)
23141 {
23142 pat = vim_strnsave(s, (int)(p - s));
23143 if (pat != NULL)
23144 {
23145 s = p + 1;
23146 /* find end of substitution */
23147 p = vim_strchr(s, sep);
23148 if (p != NULL)
23149 {
23150 sub = vim_strnsave(s, (int)(p - s));
23151 str = vim_strnsave(*fnamep, *fnamelen);
23152 if (sub != NULL && str != NULL)
23153 {
23154 *usedlen = (int)(p + 1 - src);
23155 s = do_string_sub(str, pat, sub, flags);
23156 if (s != NULL)
23157 {
23158 *fnamep = s;
23159 *fnamelen = (int)STRLEN(s);
23160 vim_free(*bufp);
23161 *bufp = s;
23162 didit = TRUE;
23163 }
23164 }
23165 vim_free(sub);
23166 vim_free(str);
23167 }
23168 vim_free(pat);
23169 }
23170 }
23171 /* after using ":s", repeat all the modifiers */
23172 if (didit)
23173 goto repeat;
23174 }
23175 }
23176
23177 return valid;
23178}
23179
23180/*
23181 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23182 * "flags" can be "g" to do a global substitute.
23183 * Returns an allocated string, NULL for error.
23184 */
23185 char_u *
23186do_string_sub(str, pat, sub, flags)
23187 char_u *str;
23188 char_u *pat;
23189 char_u *sub;
23190 char_u *flags;
23191{
23192 int sublen;
23193 regmatch_T regmatch;
23194 int i;
23195 int do_all;
23196 char_u *tail;
23197 garray_T ga;
23198 char_u *ret;
23199 char_u *save_cpo;
23200
23201 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23202 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023203 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204
23205 ga_init2(&ga, 1, 200);
23206
23207 do_all = (flags[0] == 'g');
23208
23209 regmatch.rm_ic = p_ic;
23210 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23211 if (regmatch.regprog != NULL)
23212 {
23213 tail = str;
23214 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23215 {
23216 /*
23217 * Get some space for a temporary buffer to do the substitution
23218 * into. It will contain:
23219 * - The text up to where the match is.
23220 * - The substituted text.
23221 * - The text after the match.
23222 */
23223 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23224 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23225 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23226 {
23227 ga_clear(&ga);
23228 break;
23229 }
23230
23231 /* copy the text up to where the match is */
23232 i = (int)(regmatch.startp[0] - tail);
23233 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23234 /* add the substituted text */
23235 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23236 + ga.ga_len + i, TRUE, TRUE, FALSE);
23237 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238 /* avoid getting stuck on a match with an empty string */
23239 if (tail == regmatch.endp[0])
23240 {
23241 if (*tail == NUL)
23242 break;
23243 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23244 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245 }
23246 else
23247 {
23248 tail = regmatch.endp[0];
23249 if (*tail == NUL)
23250 break;
23251 }
23252 if (!do_all)
23253 break;
23254 }
23255
23256 if (ga.ga_data != NULL)
23257 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23258
23259 vim_free(regmatch.regprog);
23260 }
23261
23262 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23263 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023264 if (p_cpo == empty_option)
23265 p_cpo = save_cpo;
23266 else
23267 /* Darn, evaluating {sub} expression changed the value. */
23268 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269
23270 return ret;
23271}
23272
23273#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */