blob: 29447582408abf4951750b307bc608a2d1d1747b [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 Moolenaara5525202006-03-02 22:52:09 +00009440#ifdef FEAT_VIRTUALEDIT
9441 long coladd = 0;
9442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009444 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009445 if (argvars[1].v_type == VAR_UNKNOWN)
9446 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009447 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009448
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009449 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009450 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009451 line = pos.lnum;
9452 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009453#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009454 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009455#endif
9456 }
9457 else
9458 {
9459 line = get_tv_lnum(argvars);
9460 col = get_tv_number_chk(&argvars[1], NULL);
9461#ifdef FEAT_VIRTUALEDIT
9462 if (argvars[2].v_type != VAR_UNKNOWN)
9463 coladd = get_tv_number_chk(&argvars[2], NULL);
9464#endif
9465 }
9466 if (line < 0 || col < 0
9467#ifdef FEAT_VIRTUALEDIT
9468 || coladd < 0
9469#endif
9470 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009471 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 if (line > 0)
9473 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 if (col > 0)
9475 curwin->w_cursor.col = col - 1;
9476#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009477 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478#endif
9479
9480 /* Make sure the cursor is in a valid position. */
9481 check_cursor();
9482#ifdef FEAT_MBYTE
9483 /* Correct cursor for multi-byte character. */
9484 if (has_mbyte)
9485 mb_adjust_cursor();
9486#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009487
9488 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009489 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490}
9491
9492/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009493 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 */
9495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009496f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009497 typval_T *argvars;
9498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009500 int noref = 0;
9501
9502 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009503 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009504 if (noref < 0 || noref > 1)
9505 EMSG(_(e_invarg));
9506 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009507 {
9508 current_copyID += COPYID_INC;
9509 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511}
9512
9513/*
9514 * "delete()" function
9515 */
9516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009518 typval_T *argvars;
9519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009522 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009524 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525}
9526
9527/*
9528 * "did_filetype()" function
9529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009531f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009532 typval_T *argvars UNUSED;
9533 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009536 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537#endif
9538}
9539
9540/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009541 * "diff_filler()" function
9542 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009544f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009545 typval_T *argvars UNUSED;
9546 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009547{
9548#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009550#endif
9551}
9552
9553/*
9554 * "diff_hlID()" function
9555 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009558 typval_T *argvars UNUSED;
9559 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009560{
9561#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009563 static linenr_T prev_lnum = 0;
9564 static int changedtick = 0;
9565 static int fnum = 0;
9566 static int change_start = 0;
9567 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009568 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009569 int filler_lines;
9570 int col;
9571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009572 if (lnum < 0) /* ignore type error in {lnum} arg */
9573 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009574 if (lnum != prev_lnum
9575 || changedtick != curbuf->b_changedtick
9576 || fnum != curbuf->b_fnum)
9577 {
9578 /* New line, buffer, change: need to get the values. */
9579 filler_lines = diff_check(curwin, lnum);
9580 if (filler_lines < 0)
9581 {
9582 if (filler_lines == -1)
9583 {
9584 change_start = MAXCOL;
9585 change_end = -1;
9586 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9587 hlID = HLF_ADD; /* added line */
9588 else
9589 hlID = HLF_CHD; /* changed line */
9590 }
9591 else
9592 hlID = HLF_ADD; /* added line */
9593 }
9594 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009595 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009596 prev_lnum = lnum;
9597 changedtick = curbuf->b_changedtick;
9598 fnum = curbuf->b_fnum;
9599 }
9600
9601 if (hlID == HLF_CHD || hlID == HLF_TXD)
9602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009603 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009604 if (col >= change_start && col <= change_end)
9605 hlID = HLF_TXD; /* changed text */
9606 else
9607 hlID = HLF_CHD; /* changed line */
9608 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009609 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009610#endif
9611}
9612
9613/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009614 * "empty({expr})" function
9615 */
9616 static void
9617f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009618 typval_T *argvars;
9619 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009620{
9621 int n;
9622
9623 switch (argvars[0].v_type)
9624 {
9625 case VAR_STRING:
9626 case VAR_FUNC:
9627 n = argvars[0].vval.v_string == NULL
9628 || *argvars[0].vval.v_string == NUL;
9629 break;
9630 case VAR_NUMBER:
9631 n = argvars[0].vval.v_number == 0;
9632 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009633#ifdef FEAT_FLOAT
9634 case VAR_FLOAT:
9635 n = argvars[0].vval.v_float == 0.0;
9636 break;
9637#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009638 case VAR_LIST:
9639 n = argvars[0].vval.v_list == NULL
9640 || argvars[0].vval.v_list->lv_first == NULL;
9641 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009642 case VAR_DICT:
9643 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009644 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009646 default:
9647 EMSG2(_(e_intern2), "f_empty()");
9648 n = 0;
9649 }
9650
9651 rettv->vval.v_number = n;
9652}
9653
9654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 * "escape({string}, {chars})" function
9656 */
9657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009659 typval_T *argvars;
9660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662 char_u buf[NUMBUFLEN];
9663
Bram Moolenaar758711c2005-02-02 23:11:38 +00009664 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9665 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009666 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667}
9668
9669/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009670 * "eval()" function
9671 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009672 static void
9673f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009674 typval_T *argvars;
9675 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009676{
9677 char_u *s;
9678
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009679 s = get_tv_string_chk(&argvars[0]);
9680 if (s != NULL)
9681 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009683 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9684 {
9685 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009686 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009687 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009688 else if (*s != NUL)
9689 EMSG(_(e_trailing));
9690}
9691
9692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 * "eventhandler()" function
9694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009697 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701}
9702
9703/*
9704 * "executable()" function
9705 */
9706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009708 typval_T *argvars;
9709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712}
9713
9714/*
9715 * "exists()" function
9716 */
9717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 typval_T *argvars;
9720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
9722 char_u *p;
9723 char_u *name;
9724 int n = FALSE;
9725 int len = 0;
9726
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 if (*p == '$') /* environment variable */
9729 {
9730 /* first try "normal" environment variables (fast) */
9731 if (mch_getenv(p + 1) != NULL)
9732 n = TRUE;
9733 else
9734 {
9735 /* try expanding things like $VIM and ${HOME} */
9736 p = expand_env_save(p);
9737 if (p != NULL && *p != '$')
9738 n = TRUE;
9739 vim_free(p);
9740 }
9741 }
9742 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009744 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009745 if (*skipwhite(p) != NUL)
9746 n = FALSE; /* trailing garbage */
9747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 else if (*p == '*') /* internal or user defined function */
9749 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009750 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 }
9752 else if (*p == ':')
9753 {
9754 n = cmd_exists(p + 1);
9755 }
9756 else if (*p == '#')
9757 {
9758#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009759 if (p[1] == '#')
9760 n = autocmd_supported(p + 2);
9761 else
9762 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763#endif
9764 }
9765 else /* internal variable */
9766 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009767 char_u *tofree;
9768 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009770 /* get_name_len() takes care of expanding curly braces */
9771 name = p;
9772 len = get_name_len(&p, &tofree, TRUE, FALSE);
9773 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009775 if (tofree != NULL)
9776 name = tofree;
9777 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9778 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009780 /* handle d.key, l[idx], f(expr) */
9781 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9782 if (n)
9783 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 }
9785 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009786 if (*p != NUL)
9787 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009789 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 }
9791
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793}
9794
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009795#ifdef FEAT_FLOAT
9796/*
9797 * "exp()" function
9798 */
9799 static void
9800f_exp(argvars, rettv)
9801 typval_T *argvars;
9802 typval_T *rettv;
9803{
9804 float_T f;
9805
9806 rettv->v_type = VAR_FLOAT;
9807 if (get_float_arg(argvars, &f) == OK)
9808 rettv->vval.v_float = exp(f);
9809 else
9810 rettv->vval.v_float = 0.0;
9811}
9812#endif
9813
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814/*
9815 * "expand()" function
9816 */
9817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009819 typval_T *argvars;
9820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
9822 char_u *s;
9823 int len;
9824 char_u *errormsg;
9825 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9826 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009827 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 rettv->v_type = VAR_STRING;
9830 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 if (*s == '%' || *s == '#' || *s == '<')
9832 {
9833 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009834 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 --emsg_off;
9836 }
9837 else
9838 {
9839 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009840 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009841 if (argvars[1].v_type != VAR_UNKNOWN
9842 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009844 if (!error)
9845 {
9846 ExpandInit(&xpc);
9847 xpc.xp_context = EXPAND_FILES;
9848 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009849 }
9850 else
9851 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 }
9853}
9854
9855/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009856 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009857 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009858 */
9859 static void
9860f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009861 typval_T *argvars;
9862 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009863{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009864 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009865 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009866 list_T *l1, *l2;
9867 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009868 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009869 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009870
Bram Moolenaare9a41262005-01-15 22:18:47 +00009871 l1 = argvars[0].vval.v_list;
9872 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009873 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9874 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009875 {
9876 if (argvars[2].v_type != VAR_UNKNOWN)
9877 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009878 before = get_tv_number_chk(&argvars[2], &error);
9879 if (error)
9880 return; /* type error; errmsg already given */
9881
Bram Moolenaar758711c2005-02-02 23:11:38 +00009882 if (before == l1->lv_len)
9883 item = NULL;
9884 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009885 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009886 item = list_find(l1, before);
9887 if (item == NULL)
9888 {
9889 EMSGN(_(e_listidx), before);
9890 return;
9891 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009892 }
9893 }
9894 else
9895 item = NULL;
9896 list_extend(l1, l2, item);
9897
Bram Moolenaare9a41262005-01-15 22:18:47 +00009898 copy_tv(&argvars[0], rettv);
9899 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009900 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009901 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9902 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009903 dict_T *d1, *d2;
9904 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009905 char_u *action;
9906 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009907 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009908 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009909
9910 d1 = argvars[0].vval.v_dict;
9911 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009912 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9913 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009914 {
9915 /* Check the third argument. */
9916 if (argvars[2].v_type != VAR_UNKNOWN)
9917 {
9918 static char *(av[]) = {"keep", "force", "error"};
9919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009920 action = get_tv_string_chk(&argvars[2]);
9921 if (action == NULL)
9922 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009923 for (i = 0; i < 3; ++i)
9924 if (STRCMP(action, av[i]) == 0)
9925 break;
9926 if (i == 3)
9927 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009928 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009929 return;
9930 }
9931 }
9932 else
9933 action = (char_u *)"force";
9934
9935 /* Go over all entries in the second dict and add them to the
9936 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009937 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009938 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009939 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009940 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009941 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009942 --todo;
9943 di1 = dict_find(d1, hi2->hi_key, -1);
9944 if (di1 == NULL)
9945 {
9946 di1 = dictitem_copy(HI2DI(hi2));
9947 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9948 dictitem_free(di1);
9949 }
9950 else if (*action == 'e')
9951 {
9952 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9953 break;
9954 }
9955 else if (*action == 'f')
9956 {
9957 clear_tv(&di1->di_tv);
9958 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960 }
9961 }
9962
Bram Moolenaare9a41262005-01-15 22:18:47 +00009963 copy_tv(&argvars[0], rettv);
9964 }
9965 }
9966 else
9967 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009968}
9969
9970/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009971 * "feedkeys()" function
9972 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009973 static void
9974f_feedkeys(argvars, rettv)
9975 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009976 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009977{
9978 int remap = TRUE;
9979 char_u *keys, *flags;
9980 char_u nbuf[NUMBUFLEN];
9981 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009982 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009983
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009984 /* This is not allowed in the sandbox. If the commands would still be
9985 * executed in the sandbox it would be OK, but it probably happens later,
9986 * when "sandbox" is no longer set. */
9987 if (check_secure())
9988 return;
9989
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009990 keys = get_tv_string(&argvars[0]);
9991 if (*keys != NUL)
9992 {
9993 if (argvars[1].v_type != VAR_UNKNOWN)
9994 {
9995 flags = get_tv_string_buf(&argvars[1], nbuf);
9996 for ( ; *flags != NUL; ++flags)
9997 {
9998 switch (*flags)
9999 {
10000 case 'n': remap = FALSE; break;
10001 case 'm': remap = TRUE; break;
10002 case 't': typed = TRUE; break;
10003 }
10004 }
10005 }
10006
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010007 /* Need to escape K_SPECIAL and CSI before putting the string in the
10008 * typeahead buffer. */
10009 keys_esc = vim_strsave_escape_csi(keys);
10010 if (keys_esc != NULL)
10011 {
10012 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010013 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010014 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010015 if (vgetc_busy)
10016 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010017 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010018 }
10019}
10020
10021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 * "filereadable()" function
10023 */
10024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010025f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010026 typval_T *argvars;
10027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010029 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 char_u *p;
10031 int n;
10032
Bram Moolenaarc236c162008-07-13 17:41:49 +000010033#ifndef O_NONBLOCK
10034# define O_NONBLOCK 0
10035#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010037 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10038 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 {
10040 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010041 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 }
10043 else
10044 n = FALSE;
10045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047}
10048
10049/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010050 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 * rights to write into.
10052 */
10053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010055 typval_T *argvars;
10056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010058 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059}
10060
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010061static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010062
10063 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010064findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010065 typval_T *argvars;
10066 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010067 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010068{
10069#ifdef FEAT_SEARCHPATH
10070 char_u *fname;
10071 char_u *fresult = NULL;
10072 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10073 char_u *p;
10074 char_u pathbuf[NUMBUFLEN];
10075 int count = 1;
10076 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010077 int error = FALSE;
10078#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010079
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010080 rettv->vval.v_string = NULL;
10081 rettv->v_type = VAR_STRING;
10082
10083#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010085
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010086 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010088 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10089 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010090 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010091 else
10092 {
10093 if (*p != NUL)
10094 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010095
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010096 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010097 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010099 }
10100
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010101 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10102 error = TRUE;
10103
10104 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106 do
10107 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010108 if (rettv->v_type == VAR_STRING)
10109 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010110 fresult = find_file_in_path_option(first ? fname : NULL,
10111 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010112 0, first, path,
10113 find_what,
10114 curbuf->b_ffname,
10115 find_what == FINDFILE_DIR
10116 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010117 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010118
10119 if (fresult != NULL && rettv->v_type == VAR_LIST)
10120 list_append_string(rettv->vval.v_list, fresult, -1);
10121
10122 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010123 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010124
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010125 if (rettv->v_type == VAR_STRING)
10126 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010127#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010128}
10129
Bram Moolenaar33570922005-01-25 22:26:29 +000010130static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10131static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010132
10133/*
10134 * Implementation of map() and filter().
10135 */
10136 static void
10137filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010138 typval_T *argvars;
10139 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010140 int map;
10141{
10142 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010143 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010144 listitem_T *li, *nli;
10145 list_T *l = NULL;
10146 dictitem_T *di;
10147 hashtab_T *ht;
10148 hashitem_T *hi;
10149 dict_T *d = NULL;
10150 typval_T save_val;
10151 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010152 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010153 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010154 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010155 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010156 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010157
Bram Moolenaare9a41262005-01-15 22:18:47 +000010158 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010159 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010160 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010161 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010162 return;
10163 }
10164 else if (argvars[0].v_type == VAR_DICT)
10165 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010166 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010167 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010168 return;
10169 }
10170 else
10171 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010172 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010173 return;
10174 }
10175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176 expr = get_tv_string_buf_chk(&argvars[1], buf);
10177 /* On type errors, the preceding call has already displayed an error
10178 * message. Avoid a misleading error message for an empty string that
10179 * was not passed as argument. */
10180 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010182 prepare_vimvar(VV_VAL, &save_val);
10183 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010184
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010185 /* We reset "did_emsg" to be able to detect whether an error
10186 * occurred during evaluation of the expression. */
10187 save_did_emsg = did_emsg;
10188 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010189
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010190 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010191 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010193 vimvars[VV_KEY].vv_type = VAR_STRING;
10194
10195 ht = &d->dv_hashtab;
10196 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010197 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010200 if (!HASHITEM_EMPTY(hi))
10201 {
10202 --todo;
10203 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010204 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010205 break;
10206 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010207 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010208 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010209 break;
10210 if (!map && rem)
10211 dictitem_remove(d, di);
10212 clear_tv(&vimvars[VV_KEY].vv_tv);
10213 }
10214 }
10215 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010216 }
10217 else
10218 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010219 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 for (li = l->lv_first; li != NULL; li = nli)
10222 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010223 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010224 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010225 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010226 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010227 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010228 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010229 break;
10230 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010232 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010233 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010234 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010235
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010236 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010238
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010239 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010240 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010241
10242 copy_tv(&argvars[0], rettv);
10243}
10244
10245 static int
10246filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010247 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248 char_u *expr;
10249 int map;
10250 int *remp;
10251{
Bram Moolenaar33570922005-01-25 22:26:29 +000010252 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010253 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010254 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010255
Bram Moolenaar33570922005-01-25 22:26:29 +000010256 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010257 s = expr;
10258 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010259 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010260 if (*s != NUL) /* check for trailing chars after expr */
10261 {
10262 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010263 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010264 }
10265 if (map)
10266 {
10267 /* map(): replace the list item value */
10268 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010269 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010270 *tv = rettv;
10271 }
10272 else
10273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274 int error = FALSE;
10275
Bram Moolenaare9a41262005-01-15 22:18:47 +000010276 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010277 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010278 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010279 /* On type error, nothing has been removed; return FAIL to stop the
10280 * loop. The error message was given by get_tv_number_chk(). */
10281 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010282 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010283 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010284 retval = OK;
10285theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010286 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010287 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010288}
10289
10290/*
10291 * "filter()" function
10292 */
10293 static void
10294f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010295 typval_T *argvars;
10296 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010297{
10298 filter_map(argvars, rettv, FALSE);
10299}
10300
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010301/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010302 * "finddir({fname}[, {path}[, {count}]])" function
10303 */
10304 static void
10305f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010306 typval_T *argvars;
10307 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010308{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010309 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010310}
10311
10312/*
10313 * "findfile({fname}[, {path}[, {count}]])" function
10314 */
10315 static void
10316f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010317 typval_T *argvars;
10318 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010319{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010320 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010321}
10322
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010323#ifdef FEAT_FLOAT
10324/*
10325 * "float2nr({float})" function
10326 */
10327 static void
10328f_float2nr(argvars, rettv)
10329 typval_T *argvars;
10330 typval_T *rettv;
10331{
10332 float_T f;
10333
10334 if (get_float_arg(argvars, &f) == OK)
10335 {
10336 if (f < -0x7fffffff)
10337 rettv->vval.v_number = -0x7fffffff;
10338 else if (f > 0x7fffffff)
10339 rettv->vval.v_number = 0x7fffffff;
10340 else
10341 rettv->vval.v_number = (varnumber_T)f;
10342 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010343}
10344
10345/*
10346 * "floor({float})" function
10347 */
10348 static void
10349f_floor(argvars, rettv)
10350 typval_T *argvars;
10351 typval_T *rettv;
10352{
10353 float_T f;
10354
10355 rettv->v_type = VAR_FLOAT;
10356 if (get_float_arg(argvars, &f) == OK)
10357 rettv->vval.v_float = floor(f);
10358 else
10359 rettv->vval.v_float = 0.0;
10360}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010361
10362/*
10363 * "fmod()" function
10364 */
10365 static void
10366f_fmod(argvars, rettv)
10367 typval_T *argvars;
10368 typval_T *rettv;
10369{
10370 float_T fx, fy;
10371
10372 rettv->v_type = VAR_FLOAT;
10373 if (get_float_arg(argvars, &fx) == OK
10374 && get_float_arg(&argvars[1], &fy) == OK)
10375 rettv->vval.v_float = fmod(fx, fy);
10376 else
10377 rettv->vval.v_float = 0.0;
10378}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010379#endif
10380
Bram Moolenaar0d660222005-01-07 21:51:51 +000010381/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010382 * "fnameescape({string})" function
10383 */
10384 static void
10385f_fnameescape(argvars, rettv)
10386 typval_T *argvars;
10387 typval_T *rettv;
10388{
10389 rettv->vval.v_string = vim_strsave_fnameescape(
10390 get_tv_string(&argvars[0]), FALSE);
10391 rettv->v_type = VAR_STRING;
10392}
10393
10394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 * "fnamemodify({fname}, {mods})" function
10396 */
10397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010398f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010399 typval_T *argvars;
10400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401{
10402 char_u *fname;
10403 char_u *mods;
10404 int usedlen = 0;
10405 int len;
10406 char_u *fbuf = NULL;
10407 char_u buf[NUMBUFLEN];
10408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010409 fname = get_tv_string_chk(&argvars[0]);
10410 mods = get_tv_string_buf_chk(&argvars[1], buf);
10411 if (fname == NULL || mods == NULL)
10412 fname = NULL;
10413 else
10414 {
10415 len = (int)STRLEN(fname);
10416 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010419 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010421 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010423 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 vim_free(fbuf);
10425}
10426
Bram Moolenaar33570922005-01-25 22:26:29 +000010427static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428
10429/*
10430 * "foldclosed()" function
10431 */
10432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010433foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010434 typval_T *argvars;
10435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 int end;
10437{
10438#ifdef FEAT_FOLDING
10439 linenr_T lnum;
10440 linenr_T first, last;
10441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010442 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10444 {
10445 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10446 {
10447 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010448 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010450 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 return;
10452 }
10453 }
10454#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456}
10457
10458/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010459 * "foldclosed()" function
10460 */
10461 static void
10462f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010463 typval_T *argvars;
10464 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010465{
10466 foldclosed_both(argvars, rettv, FALSE);
10467}
10468
10469/*
10470 * "foldclosedend()" function
10471 */
10472 static void
10473f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010474 typval_T *argvars;
10475 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010476{
10477 foldclosed_both(argvars, rettv, TRUE);
10478}
10479
10480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 * "foldlevel()" function
10482 */
10483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010484f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010485 typval_T *argvars;
10486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487{
10488#ifdef FEAT_FOLDING
10489 linenr_T lnum;
10490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010491 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010493 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495}
10496
10497/*
10498 * "foldtext()" function
10499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010501f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010502 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504{
10505#ifdef FEAT_FOLDING
10506 linenr_T lnum;
10507 char_u *s;
10508 char_u *r;
10509 int len;
10510 char *txt;
10511#endif
10512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010513 rettv->v_type = VAR_STRING;
10514 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010516 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10517 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10518 <= curbuf->b_ml.ml_line_count
10519 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 {
10521 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010522 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10523 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 {
10525 if (!linewhite(lnum))
10526 break;
10527 ++lnum;
10528 }
10529
10530 /* Find interesting text in this line. */
10531 s = skipwhite(ml_get(lnum));
10532 /* skip C comment-start */
10533 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010536 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010537 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010538 {
10539 s = skipwhite(ml_get(lnum + 1));
10540 if (*s == '*')
10541 s = skipwhite(s + 1);
10542 }
10543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 txt = _("+-%s%3ld lines: ");
10545 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010546 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 + 20 /* for %3ld */
10548 + STRLEN(s))); /* concatenated */
10549 if (r != NULL)
10550 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010551 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10552 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10553 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 len = (int)STRLEN(r);
10555 STRCAT(r, s);
10556 /* remove 'foldmarker' and 'commentstring' */
10557 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010558 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 }
10560 }
10561#endif
10562}
10563
10564/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010565 * "foldtextresult(lnum)" function
10566 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010568f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010569 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010570 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010571{
10572#ifdef FEAT_FOLDING
10573 linenr_T lnum;
10574 char_u *text;
10575 char_u buf[51];
10576 foldinfo_T foldinfo;
10577 int fold_count;
10578#endif
10579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580 rettv->v_type = VAR_STRING;
10581 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010582#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010583 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010584 /* treat illegal types and illegal string values for {lnum} the same */
10585 if (lnum < 0)
10586 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010587 fold_count = foldedCount(curwin, lnum, &foldinfo);
10588 if (fold_count > 0)
10589 {
10590 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10591 &foldinfo, buf);
10592 if (text == buf)
10593 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010594 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010595 }
10596#endif
10597}
10598
10599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 * "foreground()" function
10601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010604 typval_T *argvars UNUSED;
10605 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607#ifdef FEAT_GUI
10608 if (gui.in_use)
10609 gui_mch_set_foreground();
10610#else
10611# ifdef WIN32
10612 win32_set_foreground();
10613# endif
10614#endif
10615}
10616
10617/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010618 * "function()" function
10619 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010621f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010622 typval_T *argvars;
10623 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010624{
10625 char_u *s;
10626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010627 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010628 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010629 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010630 /* Don't check an autoload name for existence here. */
10631 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010632 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010633 else
10634 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635 rettv->vval.v_string = vim_strsave(s);
10636 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010637 }
10638}
10639
10640/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010641 * "garbagecollect()" function
10642 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010643 static void
10644f_garbagecollect(argvars, rettv)
10645 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010646 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010647{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010648 /* This is postponed until we are back at the toplevel, because we may be
10649 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10650 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010651
10652 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10653 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010654}
10655
10656/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010657 * "get()" function
10658 */
10659 static void
10660f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010661 typval_T *argvars;
10662 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010663{
Bram Moolenaar33570922005-01-25 22:26:29 +000010664 listitem_T *li;
10665 list_T *l;
10666 dictitem_T *di;
10667 dict_T *d;
10668 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010669
Bram Moolenaare9a41262005-01-15 22:18:47 +000010670 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010671 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010672 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 int error = FALSE;
10675
10676 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10677 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010678 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010679 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010680 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010681 else if (argvars[0].v_type == VAR_DICT)
10682 {
10683 if ((d = argvars[0].vval.v_dict) != NULL)
10684 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010685 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010686 if (di != NULL)
10687 tv = &di->di_tv;
10688 }
10689 }
10690 else
10691 EMSG2(_(e_listdictarg), "get()");
10692
10693 if (tv == NULL)
10694 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010695 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010696 copy_tv(&argvars[2], rettv);
10697 }
10698 else
10699 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010700}
10701
Bram Moolenaar342337a2005-07-21 21:11:17 +000010702static 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 +000010703
10704/*
10705 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010706 * Return a range (from start to end) of lines in rettv from the specified
10707 * buffer.
10708 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010709 */
10710 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010711get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010712 buf_T *buf;
10713 linenr_T start;
10714 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010715 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010716 typval_T *rettv;
10717{
10718 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010719
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010720 if (retlist && rettv_list_alloc(rettv) == FAIL)
10721 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010722
10723 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10724 return;
10725
10726 if (!retlist)
10727 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010728 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10729 p = ml_get_buf(buf, start, FALSE);
10730 else
10731 p = (char_u *)"";
10732
10733 rettv->v_type = VAR_STRING;
10734 rettv->vval.v_string = vim_strsave(p);
10735 }
10736 else
10737 {
10738 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010739 return;
10740
10741 if (start < 1)
10742 start = 1;
10743 if (end > buf->b_ml.ml_line_count)
10744 end = buf->b_ml.ml_line_count;
10745 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010746 if (list_append_string(rettv->vval.v_list,
10747 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010748 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010749 }
10750}
10751
10752/*
10753 * "getbufline()" function
10754 */
10755 static void
10756f_getbufline(argvars, rettv)
10757 typval_T *argvars;
10758 typval_T *rettv;
10759{
10760 linenr_T lnum;
10761 linenr_T end;
10762 buf_T *buf;
10763
10764 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10765 ++emsg_off;
10766 buf = get_buf_tv(&argvars[0]);
10767 --emsg_off;
10768
Bram Moolenaar661b1822005-07-28 22:36:45 +000010769 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010770 if (argvars[2].v_type == VAR_UNKNOWN)
10771 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010772 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010773 end = get_tv_lnum_buf(&argvars[2], buf);
10774
Bram Moolenaar342337a2005-07-21 21:11:17 +000010775 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010776}
10777
Bram Moolenaar0d660222005-01-07 21:51:51 +000010778/*
10779 * "getbufvar()" function
10780 */
10781 static void
10782f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010783 typval_T *argvars;
10784 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010785{
10786 buf_T *buf;
10787 buf_T *save_curbuf;
10788 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010789 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010790
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010791 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10792 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010793 ++emsg_off;
10794 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010795
10796 rettv->v_type = VAR_STRING;
10797 rettv->vval.v_string = NULL;
10798
10799 if (buf != NULL && varname != NULL)
10800 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010801 /* set curbuf to be our buf, temporarily */
10802 save_curbuf = curbuf;
10803 curbuf = buf;
10804
Bram Moolenaar0d660222005-01-07 21:51:51 +000010805 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010806 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010807 else
10808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010809 if (*varname == NUL)
10810 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10811 * scope prefix before the NUL byte is required by
10812 * find_var_in_ht(). */
10813 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010814 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010815 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010816 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010817 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010818 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010819
10820 /* restore previous notion of curbuf */
10821 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010822 }
10823
10824 --emsg_off;
10825}
10826
10827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 * "getchar()" function
10829 */
10830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010832 typval_T *argvars;
10833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834{
10835 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010836 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010838 /* Position the cursor. Needed after a message that ends in a space. */
10839 windgoto(msg_row, msg_col);
10840
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 ++no_mapping;
10842 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010843 for (;;)
10844 {
10845 if (argvars[0].v_type == VAR_UNKNOWN)
10846 /* getchar(): blocking wait. */
10847 n = safe_vgetc();
10848 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10849 /* getchar(1): only check if char avail */
10850 n = vpeekc();
10851 else if (error || vpeekc() == NUL)
10852 /* illegal argument or getchar(0) and no char avail: return zero */
10853 n = 0;
10854 else
10855 /* getchar(0) and char avail: return char */
10856 n = safe_vgetc();
10857 if (n == K_IGNORE)
10858 continue;
10859 break;
10860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 --no_mapping;
10862 --allow_keys;
10863
Bram Moolenaar219b8702006-11-01 14:32:36 +000010864 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10865 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10866 vimvars[VV_MOUSE_COL].vv_nr = 0;
10867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010868 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 if (IS_SPECIAL(n) || mod_mask != 0)
10870 {
10871 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10872 int i = 0;
10873
10874 /* Turn a special key into three bytes, plus modifier. */
10875 if (mod_mask != 0)
10876 {
10877 temp[i++] = K_SPECIAL;
10878 temp[i++] = KS_MODIFIER;
10879 temp[i++] = mod_mask;
10880 }
10881 if (IS_SPECIAL(n))
10882 {
10883 temp[i++] = K_SPECIAL;
10884 temp[i++] = K_SECOND(n);
10885 temp[i++] = K_THIRD(n);
10886 }
10887#ifdef FEAT_MBYTE
10888 else if (has_mbyte)
10889 i += (*mb_char2bytes)(n, temp + i);
10890#endif
10891 else
10892 temp[i++] = n;
10893 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 rettv->v_type = VAR_STRING;
10895 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010896
10897#ifdef FEAT_MOUSE
10898 if (n == K_LEFTMOUSE
10899 || n == K_LEFTMOUSE_NM
10900 || n == K_LEFTDRAG
10901 || n == K_LEFTRELEASE
10902 || n == K_LEFTRELEASE_NM
10903 || n == K_MIDDLEMOUSE
10904 || n == K_MIDDLEDRAG
10905 || n == K_MIDDLERELEASE
10906 || n == K_RIGHTMOUSE
10907 || n == K_RIGHTDRAG
10908 || n == K_RIGHTRELEASE
10909 || n == K_X1MOUSE
10910 || n == K_X1DRAG
10911 || n == K_X1RELEASE
10912 || n == K_X2MOUSE
10913 || n == K_X2DRAG
10914 || n == K_X2RELEASE
10915 || n == K_MOUSEDOWN
10916 || n == K_MOUSEUP)
10917 {
10918 int row = mouse_row;
10919 int col = mouse_col;
10920 win_T *win;
10921 linenr_T lnum;
10922# ifdef FEAT_WINDOWS
10923 win_T *wp;
10924# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010925 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010926
10927 if (row >= 0 && col >= 0)
10928 {
10929 /* Find the window at the mouse coordinates and compute the
10930 * text position. */
10931 win = mouse_find_win(&row, &col);
10932 (void)mouse_comp_pos(win, &row, &col, &lnum);
10933# ifdef FEAT_WINDOWS
10934 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010935 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010936# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010937 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010938 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10939 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10940 }
10941 }
10942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 }
10944}
10945
10946/*
10947 * "getcharmod()" function
10948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010950f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010951 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955}
10956
10957/*
10958 * "getcmdline()" function
10959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010961f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010962 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965 rettv->v_type = VAR_STRING;
10966 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967}
10968
10969/*
10970 * "getcmdpos()" function
10971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010973f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010974 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978}
10979
10980/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010981 * "getcmdtype()" function
10982 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010983 static void
10984f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010985 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010986 typval_T *rettv;
10987{
10988 rettv->v_type = VAR_STRING;
10989 rettv->vval.v_string = alloc(2);
10990 if (rettv->vval.v_string != NULL)
10991 {
10992 rettv->vval.v_string[0] = get_cmdline_type();
10993 rettv->vval.v_string[1] = NUL;
10994 }
10995}
10996
10997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 * "getcwd()" function
10999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011001f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011002 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004{
11005 char_u cwd[MAXPATHL];
11006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011009 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 else
11011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011012 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011014 if (rettv->vval.v_string != NULL)
11015 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016#endif
11017 }
11018}
11019
11020/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011021 * "getfontname()" function
11022 */
11023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011025 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011026 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011027{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011028 rettv->v_type = VAR_STRING;
11029 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011030#ifdef FEAT_GUI
11031 if (gui.in_use)
11032 {
11033 GuiFont font;
11034 char_u *name = NULL;
11035
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011036 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011037 {
11038 /* Get the "Normal" font. Either the name saved by
11039 * hl_set_font_name() or from the font ID. */
11040 font = gui.norm_font;
11041 name = hl_get_font_name();
11042 }
11043 else
11044 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011046 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11047 return;
11048 font = gui_mch_get_font(name, FALSE);
11049 if (font == NOFONT)
11050 return; /* Invalid font name, return empty string. */
11051 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011053 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011054 gui_mch_free_font(font);
11055 }
11056#endif
11057}
11058
11059/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011060 * "getfperm({fname})" function
11061 */
11062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011064 typval_T *argvars;
11065 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011066{
11067 char_u *fname;
11068 struct stat st;
11069 char_u *perm = NULL;
11070 char_u flags[] = "rwx";
11071 int i;
11072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011073 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011075 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011076 if (mch_stat((char *)fname, &st) >= 0)
11077 {
11078 perm = vim_strsave((char_u *)"---------");
11079 if (perm != NULL)
11080 {
11081 for (i = 0; i < 9; i++)
11082 {
11083 if (st.st_mode & (1 << (8 - i)))
11084 perm[i] = flags[i % 3];
11085 }
11086 }
11087 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011089}
11090
11091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 * "getfsize({fname})" function
11093 */
11094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011096 typval_T *argvars;
11097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098{
11099 char_u *fname;
11100 struct stat st;
11101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011102 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011104 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105
11106 if (mch_stat((char *)fname, &st) >= 0)
11107 {
11108 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011113
11114 /* non-perfect check for overflow */
11115 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11116 rettv->vval.v_number = -2;
11117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 }
11119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011120 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121}
11122
11123/*
11124 * "getftime({fname})" function
11125 */
11126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011128 typval_T *argvars;
11129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130{
11131 char_u *fname;
11132 struct stat st;
11133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135
11136 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011139 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140}
11141
11142/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011143 * "getftype({fname})" function
11144 */
11145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011147 typval_T *argvars;
11148 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011149{
11150 char_u *fname;
11151 struct stat st;
11152 char_u *type = NULL;
11153 char *t;
11154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011158 if (mch_lstat((char *)fname, &st) >= 0)
11159 {
11160#ifdef S_ISREG
11161 if (S_ISREG(st.st_mode))
11162 t = "file";
11163 else if (S_ISDIR(st.st_mode))
11164 t = "dir";
11165# ifdef S_ISLNK
11166 else if (S_ISLNK(st.st_mode))
11167 t = "link";
11168# endif
11169# ifdef S_ISBLK
11170 else if (S_ISBLK(st.st_mode))
11171 t = "bdev";
11172# endif
11173# ifdef S_ISCHR
11174 else if (S_ISCHR(st.st_mode))
11175 t = "cdev";
11176# endif
11177# ifdef S_ISFIFO
11178 else if (S_ISFIFO(st.st_mode))
11179 t = "fifo";
11180# endif
11181# ifdef S_ISSOCK
11182 else if (S_ISSOCK(st.st_mode))
11183 t = "fifo";
11184# endif
11185 else
11186 t = "other";
11187#else
11188# ifdef S_IFMT
11189 switch (st.st_mode & S_IFMT)
11190 {
11191 case S_IFREG: t = "file"; break;
11192 case S_IFDIR: t = "dir"; break;
11193# ifdef S_IFLNK
11194 case S_IFLNK: t = "link"; break;
11195# endif
11196# ifdef S_IFBLK
11197 case S_IFBLK: t = "bdev"; break;
11198# endif
11199# ifdef S_IFCHR
11200 case S_IFCHR: t = "cdev"; break;
11201# endif
11202# ifdef S_IFIFO
11203 case S_IFIFO: t = "fifo"; break;
11204# endif
11205# ifdef S_IFSOCK
11206 case S_IFSOCK: t = "socket"; break;
11207# endif
11208 default: t = "other";
11209 }
11210# else
11211 if (mch_isdir(fname))
11212 t = "dir";
11213 else
11214 t = "file";
11215# endif
11216#endif
11217 type = vim_strsave((char_u *)t);
11218 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011220}
11221
11222/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011223 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011224 */
11225 static void
11226f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011227 typval_T *argvars;
11228 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011229{
11230 linenr_T lnum;
11231 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011232 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011233
11234 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011235 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011236 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011237 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011238 retlist = FALSE;
11239 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011240 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011241 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011242 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011243 retlist = TRUE;
11244 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011245
Bram Moolenaar342337a2005-07-21 21:11:17 +000011246 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011247}
11248
11249/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011250 * "getmatches()" function
11251 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011252 static void
11253f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011254 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011255 typval_T *rettv;
11256{
11257#ifdef FEAT_SEARCH_EXTRA
11258 dict_T *dict;
11259 matchitem_T *cur = curwin->w_match_head;
11260
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011261 if (rettv_list_alloc(rettv) == OK)
11262 {
11263 while (cur != NULL)
11264 {
11265 dict = dict_alloc();
11266 if (dict == NULL)
11267 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011268 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11269 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11270 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11271 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11272 list_append_dict(rettv->vval.v_list, dict);
11273 cur = cur->next;
11274 }
11275 }
11276#endif
11277}
11278
11279/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011280 * "getpid()" function
11281 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011282 static void
11283f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011284 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011285 typval_T *rettv;
11286{
11287 rettv->vval.v_number = mch_get_pid();
11288}
11289
11290/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011291 * "getpos(string)" function
11292 */
11293 static void
11294f_getpos(argvars, rettv)
11295 typval_T *argvars;
11296 typval_T *rettv;
11297{
11298 pos_T *fp;
11299 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011300 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011301
11302 if (rettv_list_alloc(rettv) == OK)
11303 {
11304 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011305 fp = var2fpos(&argvars[0], TRUE, &fnum);
11306 if (fnum != -1)
11307 list_append_number(l, (varnumber_T)fnum);
11308 else
11309 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011310 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11311 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011312 list_append_number(l, (fp != NULL)
11313 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011314 : (varnumber_T)0);
11315 list_append_number(l,
11316#ifdef FEAT_VIRTUALEDIT
11317 (fp != NULL) ? (varnumber_T)fp->coladd :
11318#endif
11319 (varnumber_T)0);
11320 }
11321 else
11322 rettv->vval.v_number = FALSE;
11323}
11324
11325/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011326 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011327 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011328 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011329f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011330 typval_T *argvars UNUSED;
11331 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011332{
11333#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011334 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011335#endif
11336
Bram Moolenaar2641f772005-03-25 21:58:17 +000011337#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011338 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011339 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011340 wp = NULL;
11341 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11342 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011343 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011344 if (wp == NULL)
11345 return;
11346 }
11347
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011348 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011349 }
11350#endif
11351}
11352
11353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 * "getreg()" function
11355 */
11356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011358 typval_T *argvars;
11359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360{
11361 char_u *strregname;
11362 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011363 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011366 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011368 strregname = get_tv_string_chk(&argvars[0]);
11369 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011370 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011374 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375 regname = (strregname == NULL ? '"' : *strregname);
11376 if (regname == 0)
11377 regname = '"';
11378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011380 rettv->vval.v_string = error ? NULL :
11381 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382}
11383
11384/*
11385 * "getregtype()" function
11386 */
11387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011389 typval_T *argvars;
11390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391{
11392 char_u *strregname;
11393 int regname;
11394 char_u buf[NUMBUFLEN + 2];
11395 long reglen = 0;
11396
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011397 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011398 {
11399 strregname = get_tv_string_chk(&argvars[0]);
11400 if (strregname == NULL) /* type error; errmsg already given */
11401 {
11402 rettv->v_type = VAR_STRING;
11403 rettv->vval.v_string = NULL;
11404 return;
11405 }
11406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 else
11408 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011409 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410
11411 regname = (strregname == NULL ? '"' : *strregname);
11412 if (regname == 0)
11413 regname = '"';
11414
11415 buf[0] = NUL;
11416 buf[1] = NUL;
11417 switch (get_reg_type(regname, &reglen))
11418 {
11419 case MLINE: buf[0] = 'V'; break;
11420 case MCHAR: buf[0] = 'v'; break;
11421#ifdef FEAT_VISUAL
11422 case MBLOCK:
11423 buf[0] = Ctrl_V;
11424 sprintf((char *)buf + 1, "%ld", reglen + 1);
11425 break;
11426#endif
11427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011428 rettv->v_type = VAR_STRING;
11429 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430}
11431
11432/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011433 * "gettabvar()" function
11434 */
11435 static void
11436f_gettabvar(argvars, rettv)
11437 typval_T *argvars;
11438 typval_T *rettv;
11439{
11440 tabpage_T *tp;
11441 dictitem_T *v;
11442 char_u *varname;
11443
11444 rettv->v_type = VAR_STRING;
11445 rettv->vval.v_string = NULL;
11446
11447 varname = get_tv_string_chk(&argvars[1]);
11448 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11449 if (tp != NULL && varname != NULL)
11450 {
11451 /* look up the variable */
11452 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11453 if (v != NULL)
11454 copy_tv(&v->di_tv, rettv);
11455 }
11456}
11457
11458/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011459 * "gettabwinvar()" function
11460 */
11461 static void
11462f_gettabwinvar(argvars, rettv)
11463 typval_T *argvars;
11464 typval_T *rettv;
11465{
11466 getwinvar(argvars, rettv, 1);
11467}
11468
11469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 * "getwinposx()" function
11471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011474 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478#ifdef FEAT_GUI
11479 if (gui.in_use)
11480 {
11481 int x, y;
11482
11483 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 }
11486#endif
11487}
11488
11489/*
11490 * "getwinposy()" function
11491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011494 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011497 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498#ifdef FEAT_GUI
11499 if (gui.in_use)
11500 {
11501 int x, y;
11502
11503 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 }
11506#endif
11507}
11508
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011509/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011510 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011511 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011512 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011513find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011514 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011515 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011516{
11517#ifdef FEAT_WINDOWS
11518 win_T *wp;
11519#endif
11520 int nr;
11521
11522 nr = get_tv_number_chk(vp, NULL);
11523
11524#ifdef FEAT_WINDOWS
11525 if (nr < 0)
11526 return NULL;
11527 if (nr == 0)
11528 return curwin;
11529
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011530 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11531 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011532 if (--nr <= 0)
11533 break;
11534 return wp;
11535#else
11536 if (nr == 0 || nr == 1)
11537 return curwin;
11538 return NULL;
11539#endif
11540}
11541
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542/*
11543 * "getwinvar()" function
11544 */
11545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011547 typval_T *argvars;
11548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011550 getwinvar(argvars, rettv, 0);
11551}
11552
11553/*
11554 * getwinvar() and gettabwinvar()
11555 */
11556 static void
11557getwinvar(argvars, rettv, off)
11558 typval_T *argvars;
11559 typval_T *rettv;
11560 int off; /* 1 for gettabwinvar() */
11561{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562 win_T *win, *oldcurwin;
11563 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011564 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011565 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011567#ifdef FEAT_WINDOWS
11568 if (off == 1)
11569 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11570 else
11571 tp = curtab;
11572#endif
11573 win = find_win_by_nr(&argvars[off], tp);
11574 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011575 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 rettv->v_type = VAR_STRING;
11578 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579
11580 if (win != NULL && varname != NULL)
11581 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011582 /* Set curwin to be our win, temporarily. Also set curbuf, so
11583 * that we can get buffer-local options. */
11584 oldcurwin = curwin;
11585 curwin = win;
11586 curbuf = win->w_buffer;
11587
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011589 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 else
11591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011592 if (*varname == NUL)
11593 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11594 * scope prefix before the NUL byte is required by
11595 * find_var_in_ht(). */
11596 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011598 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011600 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011602
11603 /* restore previous notion of curwin */
11604 curwin = oldcurwin;
11605 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 }
11607
11608 --emsg_off;
11609}
11610
11611/*
11612 * "glob()" function
11613 */
11614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 typval_T *argvars;
11617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011619 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011621 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011623 /* When the optional second argument is non-zero, don't remove matches
11624 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11625 if (argvars[1].v_type != VAR_UNKNOWN
11626 && get_tv_number_chk(&argvars[1], &error))
11627 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011629 if (!error)
11630 {
11631 ExpandInit(&xpc);
11632 xpc.xp_context = EXPAND_FILES;
11633 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11634 NULL, flags, WILD_ALL);
11635 }
11636 else
11637 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638}
11639
11640/*
11641 * "globpath()" function
11642 */
11643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011644f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011645 typval_T *argvars;
11646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011648 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011650 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011651 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011653 /* When the optional second argument is non-zero, don't remove matches
11654 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11655 if (argvars[2].v_type != VAR_UNKNOWN
11656 && get_tv_number_chk(&argvars[2], &error))
11657 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011659 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011660 rettv->vval.v_string = NULL;
11661 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011662 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11663 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664}
11665
11666/*
11667 * "has()" function
11668 */
11669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011671 typval_T *argvars;
11672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673{
11674 int i;
11675 char_u *name;
11676 int n = FALSE;
11677 static char *(has_list[]) =
11678 {
11679#ifdef AMIGA
11680 "amiga",
11681# ifdef FEAT_ARP
11682 "arp",
11683# endif
11684#endif
11685#ifdef __BEOS__
11686 "beos",
11687#endif
11688#ifdef MSDOS
11689# ifdef DJGPP
11690 "dos32",
11691# else
11692 "dos16",
11693# endif
11694#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011695#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 "mac",
11697#endif
11698#if defined(MACOS_X_UNIX)
11699 "macunix",
11700#endif
11701#ifdef OS2
11702 "os2",
11703#endif
11704#ifdef __QNX__
11705 "qnx",
11706#endif
11707#ifdef RISCOS
11708 "riscos",
11709#endif
11710#ifdef UNIX
11711 "unix",
11712#endif
11713#ifdef VMS
11714 "vms",
11715#endif
11716#ifdef WIN16
11717 "win16",
11718#endif
11719#ifdef WIN32
11720 "win32",
11721#endif
11722#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11723 "win32unix",
11724#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011725#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 "win64",
11727#endif
11728#ifdef EBCDIC
11729 "ebcdic",
11730#endif
11731#ifndef CASE_INSENSITIVE_FILENAME
11732 "fname_case",
11733#endif
11734#ifdef FEAT_ARABIC
11735 "arabic",
11736#endif
11737#ifdef FEAT_AUTOCMD
11738 "autocmd",
11739#endif
11740#ifdef FEAT_BEVAL
11741 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011742# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11743 "balloon_multiline",
11744# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745#endif
11746#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11747 "builtin_terms",
11748# ifdef ALL_BUILTIN_TCAPS
11749 "all_builtin_terms",
11750# endif
11751#endif
11752#ifdef FEAT_BYTEOFF
11753 "byte_offset",
11754#endif
11755#ifdef FEAT_CINDENT
11756 "cindent",
11757#endif
11758#ifdef FEAT_CLIENTSERVER
11759 "clientserver",
11760#endif
11761#ifdef FEAT_CLIPBOARD
11762 "clipboard",
11763#endif
11764#ifdef FEAT_CMDL_COMPL
11765 "cmdline_compl",
11766#endif
11767#ifdef FEAT_CMDHIST
11768 "cmdline_hist",
11769#endif
11770#ifdef FEAT_COMMENTS
11771 "comments",
11772#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011773#ifdef FEAT_CONCEAL
11774 "conceal",
11775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776#ifdef FEAT_CRYPT
11777 "cryptv",
11778#endif
11779#ifdef FEAT_CSCOPE
11780 "cscope",
11781#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011782#ifdef FEAT_CURSORBIND
11783 "cursorbind",
11784#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011785#ifdef CURSOR_SHAPE
11786 "cursorshape",
11787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788#ifdef DEBUG
11789 "debug",
11790#endif
11791#ifdef FEAT_CON_DIALOG
11792 "dialog_con",
11793#endif
11794#ifdef FEAT_GUI_DIALOG
11795 "dialog_gui",
11796#endif
11797#ifdef FEAT_DIFF
11798 "diff",
11799#endif
11800#ifdef FEAT_DIGRAPHS
11801 "digraphs",
11802#endif
11803#ifdef FEAT_DND
11804 "dnd",
11805#endif
11806#ifdef FEAT_EMACS_TAGS
11807 "emacs_tags",
11808#endif
11809 "eval", /* always present, of course! */
11810#ifdef FEAT_EX_EXTRA
11811 "ex_extra",
11812#endif
11813#ifdef FEAT_SEARCH_EXTRA
11814 "extra_search",
11815#endif
11816#ifdef FEAT_FKMAP
11817 "farsi",
11818#endif
11819#ifdef FEAT_SEARCHPATH
11820 "file_in_path",
11821#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011822#if defined(UNIX) && !defined(USE_SYSTEM)
11823 "filterpipe",
11824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825#ifdef FEAT_FIND_ID
11826 "find_in_path",
11827#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011828#ifdef FEAT_FLOAT
11829 "float",
11830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831#ifdef FEAT_FOLDING
11832 "folding",
11833#endif
11834#ifdef FEAT_FOOTER
11835 "footer",
11836#endif
11837#if !defined(USE_SYSTEM) && defined(UNIX)
11838 "fork",
11839#endif
11840#ifdef FEAT_GETTEXT
11841 "gettext",
11842#endif
11843#ifdef FEAT_GUI
11844 "gui",
11845#endif
11846#ifdef FEAT_GUI_ATHENA
11847# ifdef FEAT_GUI_NEXTAW
11848 "gui_neXtaw",
11849# else
11850 "gui_athena",
11851# endif
11852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853#ifdef FEAT_GUI_GTK
11854 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011857#ifdef FEAT_GUI_GNOME
11858 "gui_gnome",
11859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860#ifdef FEAT_GUI_MAC
11861 "gui_mac",
11862#endif
11863#ifdef FEAT_GUI_MOTIF
11864 "gui_motif",
11865#endif
11866#ifdef FEAT_GUI_PHOTON
11867 "gui_photon",
11868#endif
11869#ifdef FEAT_GUI_W16
11870 "gui_win16",
11871#endif
11872#ifdef FEAT_GUI_W32
11873 "gui_win32",
11874#endif
11875#ifdef FEAT_HANGULIN
11876 "hangul_input",
11877#endif
11878#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11879 "iconv",
11880#endif
11881#ifdef FEAT_INS_EXPAND
11882 "insert_expand",
11883#endif
11884#ifdef FEAT_JUMPLIST
11885 "jumplist",
11886#endif
11887#ifdef FEAT_KEYMAP
11888 "keymap",
11889#endif
11890#ifdef FEAT_LANGMAP
11891 "langmap",
11892#endif
11893#ifdef FEAT_LIBCALL
11894 "libcall",
11895#endif
11896#ifdef FEAT_LINEBREAK
11897 "linebreak",
11898#endif
11899#ifdef FEAT_LISP
11900 "lispindent",
11901#endif
11902#ifdef FEAT_LISTCMDS
11903 "listcmds",
11904#endif
11905#ifdef FEAT_LOCALMAP
11906 "localmap",
11907#endif
11908#ifdef FEAT_MENU
11909 "menu",
11910#endif
11911#ifdef FEAT_SESSION
11912 "mksession",
11913#endif
11914#ifdef FEAT_MODIFY_FNAME
11915 "modify_fname",
11916#endif
11917#ifdef FEAT_MOUSE
11918 "mouse",
11919#endif
11920#ifdef FEAT_MOUSESHAPE
11921 "mouseshape",
11922#endif
11923#if defined(UNIX) || defined(VMS)
11924# ifdef FEAT_MOUSE_DEC
11925 "mouse_dec",
11926# endif
11927# ifdef FEAT_MOUSE_GPM
11928 "mouse_gpm",
11929# endif
11930# ifdef FEAT_MOUSE_JSB
11931 "mouse_jsbterm",
11932# endif
11933# ifdef FEAT_MOUSE_NET
11934 "mouse_netterm",
11935# endif
11936# ifdef FEAT_MOUSE_PTERM
11937 "mouse_pterm",
11938# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011939# ifdef FEAT_SYSMOUSE
11940 "mouse_sysmouse",
11941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942# ifdef FEAT_MOUSE_XTERM
11943 "mouse_xterm",
11944# endif
11945#endif
11946#ifdef FEAT_MBYTE
11947 "multi_byte",
11948#endif
11949#ifdef FEAT_MBYTE_IME
11950 "multi_byte_ime",
11951#endif
11952#ifdef FEAT_MULTI_LANG
11953 "multi_lang",
11954#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011955#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011956#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011957 "mzscheme",
11958#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960#ifdef FEAT_OLE
11961 "ole",
11962#endif
11963#ifdef FEAT_OSFILETYPE
11964 "osfiletype",
11965#endif
11966#ifdef FEAT_PATH_EXTRA
11967 "path_extra",
11968#endif
11969#ifdef FEAT_PERL
11970#ifndef DYNAMIC_PERL
11971 "perl",
11972#endif
11973#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011974#ifdef FEAT_PERSISTENT_UNDO
11975 "persistent_undo",
11976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977#ifdef FEAT_PYTHON
11978#ifndef DYNAMIC_PYTHON
11979 "python",
11980#endif
11981#endif
11982#ifdef FEAT_POSTSCRIPT
11983 "postscript",
11984#endif
11985#ifdef FEAT_PRINTER
11986 "printer",
11987#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011988#ifdef FEAT_PROFILE
11989 "profile",
11990#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011991#ifdef FEAT_RELTIME
11992 "reltime",
11993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994#ifdef FEAT_QUICKFIX
11995 "quickfix",
11996#endif
11997#ifdef FEAT_RIGHTLEFT
11998 "rightleft",
11999#endif
12000#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12001 "ruby",
12002#endif
12003#ifdef FEAT_SCROLLBIND
12004 "scrollbind",
12005#endif
12006#ifdef FEAT_CMDL_INFO
12007 "showcmd",
12008 "cmdline_info",
12009#endif
12010#ifdef FEAT_SIGNS
12011 "signs",
12012#endif
12013#ifdef FEAT_SMARTINDENT
12014 "smartindent",
12015#endif
12016#ifdef FEAT_SNIFF
12017 "sniff",
12018#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012019#ifdef STARTUPTIME
12020 "startuptime",
12021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022#ifdef FEAT_STL_OPT
12023 "statusline",
12024#endif
12025#ifdef FEAT_SUN_WORKSHOP
12026 "sun_workshop",
12027#endif
12028#ifdef FEAT_NETBEANS_INTG
12029 "netbeans_intg",
12030#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012031#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012032 "spell",
12033#endif
12034#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035 "syntax",
12036#endif
12037#if defined(USE_SYSTEM) || !defined(UNIX)
12038 "system",
12039#endif
12040#ifdef FEAT_TAG_BINS
12041 "tag_binary",
12042#endif
12043#ifdef FEAT_TAG_OLDSTATIC
12044 "tag_old_static",
12045#endif
12046#ifdef FEAT_TAG_ANYWHITE
12047 "tag_any_white",
12048#endif
12049#ifdef FEAT_TCL
12050# ifndef DYNAMIC_TCL
12051 "tcl",
12052# endif
12053#endif
12054#ifdef TERMINFO
12055 "terminfo",
12056#endif
12057#ifdef FEAT_TERMRESPONSE
12058 "termresponse",
12059#endif
12060#ifdef FEAT_TEXTOBJ
12061 "textobjects",
12062#endif
12063#ifdef HAVE_TGETENT
12064 "tgetent",
12065#endif
12066#ifdef FEAT_TITLE
12067 "title",
12068#endif
12069#ifdef FEAT_TOOLBAR
12070 "toolbar",
12071#endif
12072#ifdef FEAT_USR_CMDS
12073 "user-commands", /* was accidentally included in 5.4 */
12074 "user_commands",
12075#endif
12076#ifdef FEAT_VIMINFO
12077 "viminfo",
12078#endif
12079#ifdef FEAT_VERTSPLIT
12080 "vertsplit",
12081#endif
12082#ifdef FEAT_VIRTUALEDIT
12083 "virtualedit",
12084#endif
12085#ifdef FEAT_VISUAL
12086 "visual",
12087#endif
12088#ifdef FEAT_VISUALEXTRA
12089 "visualextra",
12090#endif
12091#ifdef FEAT_VREPLACE
12092 "vreplace",
12093#endif
12094#ifdef FEAT_WILDIGN
12095 "wildignore",
12096#endif
12097#ifdef FEAT_WILDMENU
12098 "wildmenu",
12099#endif
12100#ifdef FEAT_WINDOWS
12101 "windows",
12102#endif
12103#ifdef FEAT_WAK
12104 "winaltkeys",
12105#endif
12106#ifdef FEAT_WRITEBACKUP
12107 "writebackup",
12108#endif
12109#ifdef FEAT_XIM
12110 "xim",
12111#endif
12112#ifdef FEAT_XFONTSET
12113 "xfontset",
12114#endif
12115#ifdef USE_XSMP
12116 "xsmp",
12117#endif
12118#ifdef USE_XSMP_INTERACT
12119 "xsmp_interact",
12120#endif
12121#ifdef FEAT_XCLIPBOARD
12122 "xterm_clipboard",
12123#endif
12124#ifdef FEAT_XTERM_SAVE
12125 "xterm_save",
12126#endif
12127#if defined(UNIX) && defined(FEAT_X11)
12128 "X11",
12129#endif
12130 NULL
12131 };
12132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 for (i = 0; has_list[i] != NULL; ++i)
12135 if (STRICMP(name, has_list[i]) == 0)
12136 {
12137 n = TRUE;
12138 break;
12139 }
12140
12141 if (n == FALSE)
12142 {
12143 if (STRNICMP(name, "patch", 5) == 0)
12144 n = has_patch(atoi((char *)name + 5));
12145 else if (STRICMP(name, "vim_starting") == 0)
12146 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012147#ifdef FEAT_MBYTE
12148 else if (STRICMP(name, "multi_byte_encoding") == 0)
12149 n = has_mbyte;
12150#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012151#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12152 else if (STRICMP(name, "balloon_multiline") == 0)
12153 n = multiline_balloon_available();
12154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155#ifdef DYNAMIC_TCL
12156 else if (STRICMP(name, "tcl") == 0)
12157 n = tcl_enabled(FALSE);
12158#endif
12159#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12160 else if (STRICMP(name, "iconv") == 0)
12161 n = iconv_enabled(FALSE);
12162#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012163#ifdef DYNAMIC_MZSCHEME
12164 else if (STRICMP(name, "mzscheme") == 0)
12165 n = mzscheme_enabled(FALSE);
12166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167#ifdef DYNAMIC_RUBY
12168 else if (STRICMP(name, "ruby") == 0)
12169 n = ruby_enabled(FALSE);
12170#endif
12171#ifdef DYNAMIC_PYTHON
12172 else if (STRICMP(name, "python") == 0)
12173 n = python_enabled(FALSE);
12174#endif
12175#ifdef DYNAMIC_PERL
12176 else if (STRICMP(name, "perl") == 0)
12177 n = perl_enabled(FALSE);
12178#endif
12179#ifdef FEAT_GUI
12180 else if (STRICMP(name, "gui_running") == 0)
12181 n = (gui.in_use || gui.starting);
12182# ifdef FEAT_GUI_W32
12183 else if (STRICMP(name, "gui_win32s") == 0)
12184 n = gui_is_win32s();
12185# endif
12186# ifdef FEAT_BROWSE
12187 else if (STRICMP(name, "browse") == 0)
12188 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12189# endif
12190#endif
12191#ifdef FEAT_SYN_HL
12192 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012193 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194#endif
12195#if defined(WIN3264)
12196 else if (STRICMP(name, "win95") == 0)
12197 n = mch_windows95();
12198#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012199#ifdef FEAT_NETBEANS_INTG
12200 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012201 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 }
12204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206}
12207
12208/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012209 * "has_key()" function
12210 */
12211 static void
12212f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012213 typval_T *argvars;
12214 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012215{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012216 if (argvars[0].v_type != VAR_DICT)
12217 {
12218 EMSG(_(e_dictreq));
12219 return;
12220 }
12221 if (argvars[0].vval.v_dict == NULL)
12222 return;
12223
12224 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012225 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012226}
12227
12228/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012229 * "haslocaldir()" function
12230 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012231 static void
12232f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012233 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012234 typval_T *rettv;
12235{
12236 rettv->vval.v_number = (curwin->w_localdir != NULL);
12237}
12238
12239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240 * "hasmapto()" function
12241 */
12242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012244 typval_T *argvars;
12245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246{
12247 char_u *name;
12248 char_u *mode;
12249 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012250 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012252 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012253 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254 mode = (char_u *)"nvo";
12255 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012257 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012258 if (argvars[2].v_type != VAR_UNKNOWN)
12259 abbr = get_tv_number(&argvars[2]);
12260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261
Bram Moolenaar2c932302006-03-18 21:42:09 +000012262 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012263 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012265 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266}
12267
12268/*
12269 * "histadd()" function
12270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012273 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275{
12276#ifdef FEAT_CMDHIST
12277 int histype;
12278 char_u *str;
12279 char_u buf[NUMBUFLEN];
12280#endif
12281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012282 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 if (check_restricted() || check_secure())
12284 return;
12285#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012286 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12287 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012288 if (histype >= 0)
12289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012290 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 if (*str != NUL)
12292 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012293 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012295 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 return;
12297 }
12298 }
12299#endif
12300}
12301
12302/*
12303 * "histdel()" function
12304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012306f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012307 typval_T *argvars UNUSED;
12308 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309{
12310#ifdef FEAT_CMDHIST
12311 int n;
12312 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012313 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012315 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12316 if (str == NULL)
12317 n = 0;
12318 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012320 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012321 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012323 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325 else
12326 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012327 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012328 get_tv_string_buf(&argvars[1], buf));
12329 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330#endif
12331}
12332
12333/*
12334 * "histget()" function
12335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012337f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012338 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340{
12341#ifdef FEAT_CMDHIST
12342 int type;
12343 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012344 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012346 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12347 if (str == NULL)
12348 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012350 {
12351 type = get_histtype(str);
12352 if (argvars[1].v_type == VAR_UNKNOWN)
12353 idx = get_history_idx(type);
12354 else
12355 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12356 /* -1 on type error */
12357 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012360 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012362 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363}
12364
12365/*
12366 * "histnr()" function
12367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012370 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372{
12373 int i;
12374
12375#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012376 char_u *history = get_tv_string_chk(&argvars[0]);
12377
12378 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 if (i >= HIST_CMD && i < HIST_COUNT)
12380 i = get_history_idx(i);
12381 else
12382#endif
12383 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385}
12386
12387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 * "highlightID(name)" function
12389 */
12390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012392 typval_T *argvars;
12393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012395 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396}
12397
12398/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012399 * "highlight_exists()" function
12400 */
12401 static void
12402f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012403 typval_T *argvars;
12404 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012405{
12406 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12407}
12408
12409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 * "hostname()" function
12411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012413f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012414 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416{
12417 char_u hostname[256];
12418
12419 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420 rettv->v_type = VAR_STRING;
12421 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422}
12423
12424/*
12425 * iconv() function
12426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012429 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431{
12432#ifdef FEAT_MBYTE
12433 char_u buf1[NUMBUFLEN];
12434 char_u buf2[NUMBUFLEN];
12435 char_u *from, *to, *str;
12436 vimconv_T vimconv;
12437#endif
12438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439 rettv->v_type = VAR_STRING;
12440 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441
12442#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012443 str = get_tv_string(&argvars[0]);
12444 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12445 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 vimconv.vc_type = CONV_NONE;
12447 convert_setup(&vimconv, from, to);
12448
12449 /* If the encodings are equal, no conversion needed. */
12450 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012451 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012453 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454
12455 convert_setup(&vimconv, NULL, NULL);
12456 vim_free(from);
12457 vim_free(to);
12458#endif
12459}
12460
12461/*
12462 * "indent()" function
12463 */
12464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012465f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012466 typval_T *argvars;
12467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468{
12469 linenr_T lnum;
12470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012471 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012473 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476}
12477
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012478/*
12479 * "index()" function
12480 */
12481 static void
12482f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012483 typval_T *argvars;
12484 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012485{
Bram Moolenaar33570922005-01-25 22:26:29 +000012486 list_T *l;
12487 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012488 long idx = 0;
12489 int ic = FALSE;
12490
12491 rettv->vval.v_number = -1;
12492 if (argvars[0].v_type != VAR_LIST)
12493 {
12494 EMSG(_(e_listreq));
12495 return;
12496 }
12497 l = argvars[0].vval.v_list;
12498 if (l != NULL)
12499 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012500 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012501 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012503 int error = FALSE;
12504
Bram Moolenaar758711c2005-02-02 23:11:38 +000012505 /* Start at specified item. Use the cached index that list_find()
12506 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012507 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012508 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012509 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012510 ic = get_tv_number_chk(&argvars[3], &error);
12511 if (error)
12512 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012513 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012514
Bram Moolenaar758711c2005-02-02 23:11:38 +000012515 for ( ; item != NULL; item = item->li_next, ++idx)
12516 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012517 {
12518 rettv->vval.v_number = idx;
12519 break;
12520 }
12521 }
12522}
12523
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524static int inputsecret_flag = 0;
12525
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012526static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12527
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012529 * This function is used by f_input() and f_inputdialog() functions. The third
12530 * argument to f_input() specifies the type of completion to use at the
12531 * prompt. The third argument to f_inputdialog() specifies the value to return
12532 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533 */
12534 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012535get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012536 typval_T *argvars;
12537 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012538 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012540 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541 char_u *p = NULL;
12542 int c;
12543 char_u buf[NUMBUFLEN];
12544 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012545 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012546 int xp_type = EXPAND_NOTHING;
12547 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012549 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012550 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551
12552#ifdef NO_CONSOLE_INPUT
12553 /* While starting up, there is no place to enter text. */
12554 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556#endif
12557
12558 cmd_silent = FALSE; /* Want to see the prompt. */
12559 if (prompt != NULL)
12560 {
12561 /* Only the part of the message after the last NL is considered as
12562 * prompt for the command line */
12563 p = vim_strrchr(prompt, '\n');
12564 if (p == NULL)
12565 p = prompt;
12566 else
12567 {
12568 ++p;
12569 c = *p;
12570 *p = NUL;
12571 msg_start();
12572 msg_clr_eos();
12573 msg_puts_attr(prompt, echo_attr);
12574 msg_didout = FALSE;
12575 msg_starthere();
12576 *p = c;
12577 }
12578 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012580 if (argvars[1].v_type != VAR_UNKNOWN)
12581 {
12582 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12583 if (defstr != NULL)
12584 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012586 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012587 {
12588 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012589 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012590 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012591
Bram Moolenaar4463f292005-09-25 22:20:24 +000012592 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012593
Bram Moolenaar4463f292005-09-25 22:20:24 +000012594 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12595 if (xp_name == NULL)
12596 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012597
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012598 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012599
Bram Moolenaar4463f292005-09-25 22:20:24 +000012600 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12601 &xp_arg) == FAIL)
12602 return;
12603 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012604 }
12605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012606 if (defstr != NULL)
12607 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012608 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12609 xp_type, xp_arg);
12610
12611 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012613 /* since the user typed this, no need to wait for return */
12614 need_wait_return = FALSE;
12615 msg_didout = FALSE;
12616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617 cmd_silent = cmd_silent_save;
12618}
12619
12620/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012621 * "input()" function
12622 * Also handles inputsecret() when inputsecret is set.
12623 */
12624 static void
12625f_input(argvars, rettv)
12626 typval_T *argvars;
12627 typval_T *rettv;
12628{
12629 get_user_input(argvars, rettv, FALSE);
12630}
12631
12632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633 * "inputdialog()" function
12634 */
12635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012636f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012637 typval_T *argvars;
12638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639{
12640#if defined(FEAT_GUI_TEXTDIALOG)
12641 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12642 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12643 {
12644 char_u *message;
12645 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012646 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012648 message = get_tv_string_chk(&argvars[0]);
12649 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012650 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012651 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 else
12653 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012654 if (message != NULL && defstr != NULL
12655 && do_dialog(VIM_QUESTION, NULL, message,
12656 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658 else
12659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012660 if (message != NULL && defstr != NULL
12661 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012662 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012663 rettv->vval.v_string = vim_strsave(
12664 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012666 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012668 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669 }
12670 else
12671#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012672 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673}
12674
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012675/*
12676 * "inputlist()" function
12677 */
12678 static void
12679f_inputlist(argvars, rettv)
12680 typval_T *argvars;
12681 typval_T *rettv;
12682{
12683 listitem_T *li;
12684 int selected;
12685 int mouse_used;
12686
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012687#ifdef NO_CONSOLE_INPUT
12688 /* While starting up, there is no place to enter text. */
12689 if (no_console_input())
12690 return;
12691#endif
12692 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12693 {
12694 EMSG2(_(e_listarg), "inputlist()");
12695 return;
12696 }
12697
12698 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012699 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012700 lines_left = Rows; /* avoid more prompt */
12701 msg_scroll = TRUE;
12702 msg_clr_eos();
12703
12704 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12705 {
12706 msg_puts(get_tv_string(&li->li_tv));
12707 msg_putchar('\n');
12708 }
12709
12710 /* Ask for choice. */
12711 selected = prompt_for_number(&mouse_used);
12712 if (mouse_used)
12713 selected -= lines_left;
12714
12715 rettv->vval.v_number = selected;
12716}
12717
12718
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12720
12721/*
12722 * "inputrestore()" function
12723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012725f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012726 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728{
12729 if (ga_userinput.ga_len > 0)
12730 {
12731 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12733 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012734 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735 }
12736 else if (p_verbose > 1)
12737 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012738 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 }
12741}
12742
12743/*
12744 * "inputsave()" function
12745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012747f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012748 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012751 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752 if (ga_grow(&ga_userinput, 1) == OK)
12753 {
12754 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12755 + ga_userinput.ga_len);
12756 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012757 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 }
12759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012760 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761}
12762
12763/*
12764 * "inputsecret()" function
12765 */
12766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012767f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012768 typval_T *argvars;
12769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770{
12771 ++cmdline_star;
12772 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 --cmdline_star;
12775 --inputsecret_flag;
12776}
12777
12778/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012779 * "insert()" function
12780 */
12781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012783 typval_T *argvars;
12784 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012785{
12786 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012787 listitem_T *item;
12788 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012789 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012790
12791 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012792 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012793 else if ((l = argvars[0].vval.v_list) != NULL
12794 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012795 {
12796 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012797 before = get_tv_number_chk(&argvars[2], &error);
12798 if (error)
12799 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012800
Bram Moolenaar758711c2005-02-02 23:11:38 +000012801 if (before == l->lv_len)
12802 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012803 else
12804 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012805 item = list_find(l, before);
12806 if (item == NULL)
12807 {
12808 EMSGN(_(e_listidx), before);
12809 l = NULL;
12810 }
12811 }
12812 if (l != NULL)
12813 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012814 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012815 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012816 }
12817 }
12818}
12819
12820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 * "isdirectory()" function
12822 */
12823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012824f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012825 typval_T *argvars;
12826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829}
12830
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012831/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012832 * "islocked()" function
12833 */
12834 static void
12835f_islocked(argvars, rettv)
12836 typval_T *argvars;
12837 typval_T *rettv;
12838{
12839 lval_T lv;
12840 char_u *end;
12841 dictitem_T *di;
12842
12843 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012844 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12845 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012846 if (end != NULL && lv.ll_name != NULL)
12847 {
12848 if (*end != NUL)
12849 EMSG(_(e_trailing));
12850 else
12851 {
12852 if (lv.ll_tv == NULL)
12853 {
12854 if (check_changedtick(lv.ll_name))
12855 rettv->vval.v_number = 1; /* always locked */
12856 else
12857 {
12858 di = find_var(lv.ll_name, NULL);
12859 if (di != NULL)
12860 {
12861 /* Consider a variable locked when:
12862 * 1. the variable itself is locked
12863 * 2. the value of the variable is locked.
12864 * 3. the List or Dict value is locked.
12865 */
12866 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12867 || tv_islocked(&di->di_tv));
12868 }
12869 }
12870 }
12871 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012872 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012873 else if (lv.ll_newkey != NULL)
12874 EMSG2(_(e_dictkey), lv.ll_newkey);
12875 else if (lv.ll_list != NULL)
12876 /* List item. */
12877 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12878 else
12879 /* Dictionary item. */
12880 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12881 }
12882 }
12883
12884 clear_lval(&lv);
12885}
12886
Bram Moolenaar33570922005-01-25 22:26:29 +000012887static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012888
12889/*
12890 * Turn a dict into a list:
12891 * "what" == 0: list of keys
12892 * "what" == 1: list of values
12893 * "what" == 2: list of items
12894 */
12895 static void
12896dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012897 typval_T *argvars;
12898 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012899 int what;
12900{
Bram Moolenaar33570922005-01-25 22:26:29 +000012901 list_T *l2;
12902 dictitem_T *di;
12903 hashitem_T *hi;
12904 listitem_T *li;
12905 listitem_T *li2;
12906 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012907 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012908
Bram Moolenaar8c711452005-01-14 21:53:12 +000012909 if (argvars[0].v_type != VAR_DICT)
12910 {
12911 EMSG(_(e_dictreq));
12912 return;
12913 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012914 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012915 return;
12916
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012917 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012918 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012919
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012920 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012921 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012922 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012923 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012924 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012925 --todo;
12926 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012927
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012928 li = listitem_alloc();
12929 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012930 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012931 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012932
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012933 if (what == 0)
12934 {
12935 /* keys() */
12936 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012937 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012938 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12939 }
12940 else if (what == 1)
12941 {
12942 /* values() */
12943 copy_tv(&di->di_tv, &li->li_tv);
12944 }
12945 else
12946 {
12947 /* items() */
12948 l2 = list_alloc();
12949 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012950 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012951 li->li_tv.vval.v_list = l2;
12952 if (l2 == NULL)
12953 break;
12954 ++l2->lv_refcount;
12955
12956 li2 = listitem_alloc();
12957 if (li2 == NULL)
12958 break;
12959 list_append(l2, li2);
12960 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012961 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012962 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12963
12964 li2 = listitem_alloc();
12965 if (li2 == NULL)
12966 break;
12967 list_append(l2, li2);
12968 copy_tv(&di->di_tv, &li2->li_tv);
12969 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012970 }
12971 }
12972}
12973
12974/*
12975 * "items(dict)" function
12976 */
12977 static void
12978f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012979 typval_T *argvars;
12980 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012981{
12982 dict_list(argvars, rettv, 2);
12983}
12984
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012986 * "join()" function
12987 */
12988 static void
12989f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012990 typval_T *argvars;
12991 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012992{
12993 garray_T ga;
12994 char_u *sep;
12995
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012996 if (argvars[0].v_type != VAR_LIST)
12997 {
12998 EMSG(_(e_listreq));
12999 return;
13000 }
13001 if (argvars[0].vval.v_list == NULL)
13002 return;
13003 if (argvars[1].v_type == VAR_UNKNOWN)
13004 sep = (char_u *)" ";
13005 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013006 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013007
13008 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013009
13010 if (sep != NULL)
13011 {
13012 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013013 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013014 ga_append(&ga, NUL);
13015 rettv->vval.v_string = (char_u *)ga.ga_data;
13016 }
13017 else
13018 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013019}
13020
13021/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013022 * "keys()" function
13023 */
13024 static void
13025f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013026 typval_T *argvars;
13027 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013028{
13029 dict_list(argvars, rettv, 0);
13030}
13031
13032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033 * "last_buffer_nr()" function.
13034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013036f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013037 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039{
13040 int n = 0;
13041 buf_T *buf;
13042
13043 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13044 if (n < buf->b_fnum)
13045 n = buf->b_fnum;
13046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013047 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013048}
13049
13050/*
13051 * "len()" function
13052 */
13053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013054f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013055 typval_T *argvars;
13056 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013057{
13058 switch (argvars[0].v_type)
13059 {
13060 case VAR_STRING:
13061 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013062 rettv->vval.v_number = (varnumber_T)STRLEN(
13063 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013064 break;
13065 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013066 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013067 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013068 case VAR_DICT:
13069 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13070 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013071 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013072 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013073 break;
13074 }
13075}
13076
Bram Moolenaar33570922005-01-25 22:26:29 +000013077static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013078
13079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013080libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013081 typval_T *argvars;
13082 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013083 int type;
13084{
13085#ifdef FEAT_LIBCALL
13086 char_u *string_in;
13087 char_u **string_result;
13088 int nr_result;
13089#endif
13090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013091 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013092 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013093 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013094
13095 if (check_restricted() || check_secure())
13096 return;
13097
13098#ifdef FEAT_LIBCALL
13099 /* The first two args must be strings, otherwise its meaningless */
13100 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13101 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013102 string_in = NULL;
13103 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013104 string_in = argvars[2].vval.v_string;
13105 if (type == VAR_NUMBER)
13106 string_result = NULL;
13107 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013108 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013109 if (mch_libcall(argvars[0].vval.v_string,
13110 argvars[1].vval.v_string,
13111 string_in,
13112 argvars[2].vval.v_number,
13113 string_result,
13114 &nr_result) == OK
13115 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013116 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013117 }
13118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119}
13120
13121/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013122 * "libcall()" function
13123 */
13124 static void
13125f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013126 typval_T *argvars;
13127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013128{
13129 libcall_common(argvars, rettv, VAR_STRING);
13130}
13131
13132/*
13133 * "libcallnr()" function
13134 */
13135 static void
13136f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013137 typval_T *argvars;
13138 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013139{
13140 libcall_common(argvars, rettv, VAR_NUMBER);
13141}
13142
13143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 * "line(string)" function
13145 */
13146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013148 typval_T *argvars;
13149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150{
13151 linenr_T lnum = 0;
13152 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013153 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013155 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156 if (fp != NULL)
13157 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159}
13160
13161/*
13162 * "line2byte(lnum)" function
13163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013165f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013166 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168{
13169#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171#else
13172 linenr_T lnum;
13173
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013174 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013178 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13179 if (rettv->vval.v_number >= 0)
13180 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181#endif
13182}
13183
13184/*
13185 * "lispindent(lnum)" function
13186 */
13187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013188f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013189 typval_T *argvars;
13190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191{
13192#ifdef FEAT_LISP
13193 pos_T pos;
13194 linenr_T lnum;
13195
13196 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013197 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13199 {
13200 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013201 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202 curwin->w_cursor = pos;
13203 }
13204 else
13205#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013206 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207}
13208
13209/*
13210 * "localtime()" function
13211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013214 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013217 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218}
13219
Bram Moolenaar33570922005-01-25 22:26:29 +000013220static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221
13222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013223get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013224 typval_T *argvars;
13225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226 int exact;
13227{
13228 char_u *keys;
13229 char_u *which;
13230 char_u buf[NUMBUFLEN];
13231 char_u *keys_buf = NULL;
13232 char_u *rhs;
13233 int mode;
13234 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013235 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236
13237 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238 rettv->v_type = VAR_STRING;
13239 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 if (*keys == NUL)
13243 return;
13244
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013245 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013247 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013248 if (argvars[2].v_type != VAR_UNKNOWN)
13249 abbr = get_tv_number(&argvars[2]);
13250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251 else
13252 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013253 if (which == NULL)
13254 return;
13255
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256 mode = get_map_mode(&which, 0);
13257
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013258 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013259 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260 vim_free(keys_buf);
13261 if (rhs != NULL)
13262 {
13263 ga_init(&ga);
13264 ga.ga_itemsize = 1;
13265 ga.ga_growsize = 40;
13266
13267 while (*rhs != NUL)
13268 ga_concat(&ga, str2special(&rhs, FALSE));
13269
13270 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013271 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 }
13273}
13274
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013275#ifdef FEAT_FLOAT
13276/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013277 * "log()" function
13278 */
13279 static void
13280f_log(argvars, rettv)
13281 typval_T *argvars;
13282 typval_T *rettv;
13283{
13284 float_T f;
13285
13286 rettv->v_type = VAR_FLOAT;
13287 if (get_float_arg(argvars, &f) == OK)
13288 rettv->vval.v_float = log(f);
13289 else
13290 rettv->vval.v_float = 0.0;
13291}
13292
13293/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013294 * "log10()" function
13295 */
13296 static void
13297f_log10(argvars, rettv)
13298 typval_T *argvars;
13299 typval_T *rettv;
13300{
13301 float_T f;
13302
13303 rettv->v_type = VAR_FLOAT;
13304 if (get_float_arg(argvars, &f) == OK)
13305 rettv->vval.v_float = log10(f);
13306 else
13307 rettv->vval.v_float = 0.0;
13308}
13309#endif
13310
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013312 * "map()" function
13313 */
13314 static void
13315f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013316 typval_T *argvars;
13317 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013318{
13319 filter_map(argvars, rettv, TRUE);
13320}
13321
13322/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013323 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324 */
13325 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013326f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013327 typval_T *argvars;
13328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013330 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331}
13332
13333/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013334 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 */
13336 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013337f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013338 typval_T *argvars;
13339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013341 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342}
13343
Bram Moolenaar33570922005-01-25 22:26:29 +000013344static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345
13346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013347find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013348 typval_T *argvars;
13349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350 int type;
13351{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013352 char_u *str = NULL;
13353 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354 char_u *pat;
13355 regmatch_T regmatch;
13356 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013357 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358 char_u *save_cpo;
13359 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013360 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013361 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013362 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013363 list_T *l = NULL;
13364 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013365 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013366 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367
13368 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13369 save_cpo = p_cpo;
13370 p_cpo = (char_u *)"";
13371
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013372 rettv->vval.v_number = -1;
13373 if (type == 3)
13374 {
13375 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013376 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013377 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013378 }
13379 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013381 rettv->v_type = VAR_STRING;
13382 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013385 if (argvars[0].v_type == VAR_LIST)
13386 {
13387 if ((l = argvars[0].vval.v_list) == NULL)
13388 goto theend;
13389 li = l->lv_first;
13390 }
13391 else
13392 expr = str = get_tv_string(&argvars[0]);
13393
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013394 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13395 if (pat == NULL)
13396 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013397
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013398 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013400 int error = FALSE;
13401
13402 start = get_tv_number_chk(&argvars[2], &error);
13403 if (error)
13404 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013405 if (l != NULL)
13406 {
13407 li = list_find(l, start);
13408 if (li == NULL)
13409 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013410 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013411 }
13412 else
13413 {
13414 if (start < 0)
13415 start = 0;
13416 if (start > (long)STRLEN(str))
13417 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013418 /* When "count" argument is there ignore matches before "start",
13419 * otherwise skip part of the string. Differs when pattern is "^"
13420 * or "\<". */
13421 if (argvars[3].v_type != VAR_UNKNOWN)
13422 startcol = start;
13423 else
13424 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013425 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013426
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013427 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013428 nth = get_tv_number_chk(&argvars[3], &error);
13429 if (error)
13430 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431 }
13432
13433 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13434 if (regmatch.regprog != NULL)
13435 {
13436 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013437
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013438 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013439 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013440 if (l != NULL)
13441 {
13442 if (li == NULL)
13443 {
13444 match = FALSE;
13445 break;
13446 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013447 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013448 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013449 if (str == NULL)
13450 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013451 }
13452
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013453 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013454
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013455 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013456 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013457 if (l == NULL && !match)
13458 break;
13459
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013460 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013461 if (l != NULL)
13462 {
13463 li = li->li_next;
13464 ++idx;
13465 }
13466 else
13467 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013468#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013469 startcol = (colnr_T)(regmatch.startp[0]
13470 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013471#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013472 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013473#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013474 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013475 }
13476
13477 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013479 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013480 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013481 int i;
13482
13483 /* return list with matched string and submatches */
13484 for (i = 0; i < NSUBEXP; ++i)
13485 {
13486 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013487 {
13488 if (list_append_string(rettv->vval.v_list,
13489 (char_u *)"", 0) == FAIL)
13490 break;
13491 }
13492 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013493 regmatch.startp[i],
13494 (int)(regmatch.endp[i] - regmatch.startp[i]))
13495 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013496 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013497 }
13498 }
13499 else if (type == 2)
13500 {
13501 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013502 if (l != NULL)
13503 copy_tv(&li->li_tv, rettv);
13504 else
13505 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013507 }
13508 else if (l != NULL)
13509 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 else
13511 {
13512 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013513 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514 (varnumber_T)(regmatch.startp[0] - str);
13515 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013516 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013518 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519 }
13520 }
13521 vim_free(regmatch.regprog);
13522 }
13523
13524theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013525 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526 p_cpo = save_cpo;
13527}
13528
13529/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013530 * "match()" function
13531 */
13532 static void
13533f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013534 typval_T *argvars;
13535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013536{
13537 find_some_match(argvars, rettv, 1);
13538}
13539
13540/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013541 * "matchadd()" function
13542 */
13543 static void
13544f_matchadd(argvars, rettv)
13545 typval_T *argvars;
13546 typval_T *rettv;
13547{
13548#ifdef FEAT_SEARCH_EXTRA
13549 char_u buf[NUMBUFLEN];
13550 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13551 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13552 int prio = 10; /* default priority */
13553 int id = -1;
13554 int error = FALSE;
13555
13556 rettv->vval.v_number = -1;
13557
13558 if (grp == NULL || pat == NULL)
13559 return;
13560 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013561 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013562 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013563 if (argvars[3].v_type != VAR_UNKNOWN)
13564 id = get_tv_number_chk(&argvars[3], &error);
13565 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013566 if (error == TRUE)
13567 return;
13568 if (id >= 1 && id <= 3)
13569 {
13570 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13571 return;
13572 }
13573
13574 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13575#endif
13576}
13577
13578/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013579 * "matcharg()" function
13580 */
13581 static void
13582f_matcharg(argvars, rettv)
13583 typval_T *argvars;
13584 typval_T *rettv;
13585{
13586 if (rettv_list_alloc(rettv) == OK)
13587 {
13588#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013589 int id = get_tv_number(&argvars[0]);
13590 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013591
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013592 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013593 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013594 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13595 {
13596 list_append_string(rettv->vval.v_list,
13597 syn_id2name(m->hlg_id), -1);
13598 list_append_string(rettv->vval.v_list, m->pattern, -1);
13599 }
13600 else
13601 {
13602 list_append_string(rettv->vval.v_list, NUL, -1);
13603 list_append_string(rettv->vval.v_list, NUL, -1);
13604 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013605 }
13606#endif
13607 }
13608}
13609
13610/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013611 * "matchdelete()" function
13612 */
13613 static void
13614f_matchdelete(argvars, rettv)
13615 typval_T *argvars;
13616 typval_T *rettv;
13617{
13618#ifdef FEAT_SEARCH_EXTRA
13619 rettv->vval.v_number = match_delete(curwin,
13620 (int)get_tv_number(&argvars[0]), TRUE);
13621#endif
13622}
13623
13624/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013625 * "matchend()" function
13626 */
13627 static void
13628f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013629 typval_T *argvars;
13630 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013631{
13632 find_some_match(argvars, rettv, 0);
13633}
13634
13635/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013636 * "matchlist()" function
13637 */
13638 static void
13639f_matchlist(argvars, rettv)
13640 typval_T *argvars;
13641 typval_T *rettv;
13642{
13643 find_some_match(argvars, rettv, 3);
13644}
13645
13646/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013647 * "matchstr()" function
13648 */
13649 static void
13650f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013651 typval_T *argvars;
13652 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013653{
13654 find_some_match(argvars, rettv, 2);
13655}
13656
Bram Moolenaar33570922005-01-25 22:26:29 +000013657static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013658
13659 static void
13660max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013661 typval_T *argvars;
13662 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013663 int domax;
13664{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013665 long n = 0;
13666 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013667 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013668
13669 if (argvars[0].v_type == VAR_LIST)
13670 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013671 list_T *l;
13672 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013673
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013674 l = argvars[0].vval.v_list;
13675 if (l != NULL)
13676 {
13677 li = l->lv_first;
13678 if (li != NULL)
13679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013680 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013681 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013682 {
13683 li = li->li_next;
13684 if (li == NULL)
13685 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013686 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013687 if (domax ? i > n : i < n)
13688 n = i;
13689 }
13690 }
13691 }
13692 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013693 else if (argvars[0].v_type == VAR_DICT)
13694 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013695 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013696 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013697 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013698 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013699
13700 d = argvars[0].vval.v_dict;
13701 if (d != NULL)
13702 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013703 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013704 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013705 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013706 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013707 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013708 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013709 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013710 if (first)
13711 {
13712 n = i;
13713 first = FALSE;
13714 }
13715 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013716 n = i;
13717 }
13718 }
13719 }
13720 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013721 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013722 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013723 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013724}
13725
13726/*
13727 * "max()" function
13728 */
13729 static void
13730f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013731 typval_T *argvars;
13732 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013733{
13734 max_min(argvars, rettv, TRUE);
13735}
13736
13737/*
13738 * "min()" function
13739 */
13740 static void
13741f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013742 typval_T *argvars;
13743 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013744{
13745 max_min(argvars, rettv, FALSE);
13746}
13747
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013748static int mkdir_recurse __ARGS((char_u *dir, int prot));
13749
13750/*
13751 * Create the directory in which "dir" is located, and higher levels when
13752 * needed.
13753 */
13754 static int
13755mkdir_recurse(dir, prot)
13756 char_u *dir;
13757 int prot;
13758{
13759 char_u *p;
13760 char_u *updir;
13761 int r = FAIL;
13762
13763 /* Get end of directory name in "dir".
13764 * We're done when it's "/" or "c:/". */
13765 p = gettail_sep(dir);
13766 if (p <= get_past_head(dir))
13767 return OK;
13768
13769 /* If the directory exists we're done. Otherwise: create it.*/
13770 updir = vim_strnsave(dir, (int)(p - dir));
13771 if (updir == NULL)
13772 return FAIL;
13773 if (mch_isdir(updir))
13774 r = OK;
13775 else if (mkdir_recurse(updir, prot) == OK)
13776 r = vim_mkdir_emsg(updir, prot);
13777 vim_free(updir);
13778 return r;
13779}
13780
13781#ifdef vim_mkdir
13782/*
13783 * "mkdir()" function
13784 */
13785 static void
13786f_mkdir(argvars, rettv)
13787 typval_T *argvars;
13788 typval_T *rettv;
13789{
13790 char_u *dir;
13791 char_u buf[NUMBUFLEN];
13792 int prot = 0755;
13793
13794 rettv->vval.v_number = FAIL;
13795 if (check_restricted() || check_secure())
13796 return;
13797
13798 dir = get_tv_string_buf(&argvars[0], buf);
13799 if (argvars[1].v_type != VAR_UNKNOWN)
13800 {
13801 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013802 prot = get_tv_number_chk(&argvars[2], NULL);
13803 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013804 mkdir_recurse(dir, prot);
13805 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013806 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013807}
13808#endif
13809
Bram Moolenaar0d660222005-01-07 21:51:51 +000013810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 * "mode()" function
13812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013814f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013815 typval_T *argvars;
13816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013818 char_u buf[3];
13819
13820 buf[1] = NUL;
13821 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822
13823#ifdef FEAT_VISUAL
13824 if (VIsual_active)
13825 {
13826 if (VIsual_select)
13827 buf[0] = VIsual_mode + 's' - 'v';
13828 else
13829 buf[0] = VIsual_mode;
13830 }
13831 else
13832#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013833 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13834 || State == CONFIRM)
13835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013837 if (State == ASKMORE)
13838 buf[1] = 'm';
13839 else if (State == CONFIRM)
13840 buf[1] = '?';
13841 }
13842 else if (State == EXTERNCMD)
13843 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844 else if (State & INSERT)
13845 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013846#ifdef FEAT_VREPLACE
13847 if (State & VREPLACE_FLAG)
13848 {
13849 buf[0] = 'R';
13850 buf[1] = 'v';
13851 }
13852 else
13853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854 if (State & REPLACE_FLAG)
13855 buf[0] = 'R';
13856 else
13857 buf[0] = 'i';
13858 }
13859 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013862 if (exmode_active)
13863 buf[1] = 'v';
13864 }
13865 else if (exmode_active)
13866 {
13867 buf[0] = 'c';
13868 buf[1] = 'e';
13869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013873 if (finish_op)
13874 buf[1] = 'o';
13875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013877 /* Clear out the minor mode when the argument is not a non-zero number or
13878 * non-empty string. */
13879 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013880 buf[1] = NUL;
13881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013882 rettv->vval.v_string = vim_strsave(buf);
13883 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884}
13885
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013886#ifdef FEAT_MZSCHEME
13887/*
13888 * "mzeval()" function
13889 */
13890 static void
13891f_mzeval(argvars, rettv)
13892 typval_T *argvars;
13893 typval_T *rettv;
13894{
13895 char_u *str;
13896 char_u buf[NUMBUFLEN];
13897
13898 str = get_tv_string_buf(&argvars[0], buf);
13899 do_mzeval(str, rettv);
13900}
13901#endif
13902
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013904 * "nextnonblank()" function
13905 */
13906 static void
13907f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013908 typval_T *argvars;
13909 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013910{
13911 linenr_T lnum;
13912
13913 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013915 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013916 {
13917 lnum = 0;
13918 break;
13919 }
13920 if (*skipwhite(ml_get(lnum)) != NUL)
13921 break;
13922 }
13923 rettv->vval.v_number = lnum;
13924}
13925
13926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 * "nr2char()" function
13928 */
13929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013930f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013931 typval_T *argvars;
13932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933{
13934 char_u buf[NUMBUFLEN];
13935
13936#ifdef FEAT_MBYTE
13937 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013938 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 else
13940#endif
13941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013942 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 buf[1] = NUL;
13944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013945 rettv->v_type = VAR_STRING;
13946 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013947}
13948
13949/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013950 * "pathshorten()" function
13951 */
13952 static void
13953f_pathshorten(argvars, rettv)
13954 typval_T *argvars;
13955 typval_T *rettv;
13956{
13957 char_u *p;
13958
13959 rettv->v_type = VAR_STRING;
13960 p = get_tv_string_chk(&argvars[0]);
13961 if (p == NULL)
13962 rettv->vval.v_string = NULL;
13963 else
13964 {
13965 p = vim_strsave(p);
13966 rettv->vval.v_string = p;
13967 if (p != NULL)
13968 shorten_dir(p);
13969 }
13970}
13971
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013972#ifdef FEAT_FLOAT
13973/*
13974 * "pow()" function
13975 */
13976 static void
13977f_pow(argvars, rettv)
13978 typval_T *argvars;
13979 typval_T *rettv;
13980{
13981 float_T fx, fy;
13982
13983 rettv->v_type = VAR_FLOAT;
13984 if (get_float_arg(argvars, &fx) == OK
13985 && get_float_arg(&argvars[1], &fy) == OK)
13986 rettv->vval.v_float = pow(fx, fy);
13987 else
13988 rettv->vval.v_float = 0.0;
13989}
13990#endif
13991
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013992/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013993 * "prevnonblank()" function
13994 */
13995 static void
13996f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013997 typval_T *argvars;
13998 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013999{
14000 linenr_T lnum;
14001
14002 lnum = get_tv_lnum(argvars);
14003 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14004 lnum = 0;
14005 else
14006 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14007 --lnum;
14008 rettv->vval.v_number = lnum;
14009}
14010
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014011#ifdef HAVE_STDARG_H
14012/* This dummy va_list is here because:
14013 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14014 * - locally in the function results in a "used before set" warning
14015 * - using va_start() to initialize it gives "function with fixed args" error */
14016static va_list ap;
14017#endif
14018
Bram Moolenaar8c711452005-01-14 21:53:12 +000014019/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014020 * "printf()" function
14021 */
14022 static void
14023f_printf(argvars, rettv)
14024 typval_T *argvars;
14025 typval_T *rettv;
14026{
14027 rettv->v_type = VAR_STRING;
14028 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014029#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014030 {
14031 char_u buf[NUMBUFLEN];
14032 int len;
14033 char_u *s;
14034 int saved_did_emsg = did_emsg;
14035 char *fmt;
14036
14037 /* Get the required length, allocate the buffer and do it for real. */
14038 did_emsg = FALSE;
14039 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014040 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014041 if (!did_emsg)
14042 {
14043 s = alloc(len + 1);
14044 if (s != NULL)
14045 {
14046 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014047 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014048 }
14049 }
14050 did_emsg |= saved_did_emsg;
14051 }
14052#endif
14053}
14054
14055/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014056 * "pumvisible()" function
14057 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014058 static void
14059f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014060 typval_T *argvars UNUSED;
14061 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014062{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014063#ifdef FEAT_INS_EXPAND
14064 if (pum_visible())
14065 rettv->vval.v_number = 1;
14066#endif
14067}
14068
14069/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014070 * "range()" function
14071 */
14072 static void
14073f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014074 typval_T *argvars;
14075 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014076{
14077 long start;
14078 long end;
14079 long stride = 1;
14080 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014081 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014083 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014084 if (argvars[1].v_type == VAR_UNKNOWN)
14085 {
14086 end = start - 1;
14087 start = 0;
14088 }
14089 else
14090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014091 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014092 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014093 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014094 }
14095
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014096 if (error)
14097 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014098 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014099 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014100 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014101 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014102 else
14103 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014104 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014105 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014106 if (list_append_number(rettv->vval.v_list,
14107 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014108 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014109 }
14110}
14111
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014112/*
14113 * "readfile()" function
14114 */
14115 static void
14116f_readfile(argvars, rettv)
14117 typval_T *argvars;
14118 typval_T *rettv;
14119{
14120 int binary = FALSE;
14121 char_u *fname;
14122 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014123 listitem_T *li;
14124#define FREAD_SIZE 200 /* optimized for text lines */
14125 char_u buf[FREAD_SIZE];
14126 int readlen; /* size of last fread() */
14127 int buflen; /* nr of valid chars in buf[] */
14128 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14129 int tolist; /* first byte in buf[] still to be put in list */
14130 int chop; /* how many CR to chop off */
14131 char_u *prev = NULL; /* previously read bytes, if any */
14132 int prevlen = 0; /* length of "prev" if not NULL */
14133 char_u *s;
14134 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014135 long maxline = MAXLNUM;
14136 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014137
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014138 if (argvars[1].v_type != VAR_UNKNOWN)
14139 {
14140 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14141 binary = TRUE;
14142 if (argvars[2].v_type != VAR_UNKNOWN)
14143 maxline = get_tv_number(&argvars[2]);
14144 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014145
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014146 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014147 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014148
14149 /* Always open the file in binary mode, library functions have a mind of
14150 * their own about CR-LF conversion. */
14151 fname = get_tv_string(&argvars[0]);
14152 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14153 {
14154 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14155 return;
14156 }
14157
14158 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014159 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014160 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014161 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014162 buflen = filtd + readlen;
14163 tolist = 0;
14164 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14165 {
14166 if (buf[filtd] == '\n' || readlen <= 0)
14167 {
14168 /* Only when in binary mode add an empty list item when the
14169 * last line ends in a '\n'. */
14170 if (!binary && readlen == 0 && filtd == 0)
14171 break;
14172
14173 /* Found end-of-line or end-of-file: add a text line to the
14174 * list. */
14175 chop = 0;
14176 if (!binary)
14177 while (filtd - chop - 1 >= tolist
14178 && buf[filtd - chop - 1] == '\r')
14179 ++chop;
14180 len = filtd - tolist - chop;
14181 if (prev == NULL)
14182 s = vim_strnsave(buf + tolist, len);
14183 else
14184 {
14185 s = alloc((unsigned)(prevlen + len + 1));
14186 if (s != NULL)
14187 {
14188 mch_memmove(s, prev, prevlen);
14189 vim_free(prev);
14190 prev = NULL;
14191 mch_memmove(s + prevlen, buf + tolist, len);
14192 s[prevlen + len] = NUL;
14193 }
14194 }
14195 tolist = filtd + 1;
14196
14197 li = listitem_alloc();
14198 if (li == NULL)
14199 {
14200 vim_free(s);
14201 break;
14202 }
14203 li->li_tv.v_type = VAR_STRING;
14204 li->li_tv.v_lock = 0;
14205 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014206 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014207
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014208 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014209 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014210 if (readlen <= 0)
14211 break;
14212 }
14213 else if (buf[filtd] == NUL)
14214 buf[filtd] = '\n';
14215 }
14216 if (readlen <= 0)
14217 break;
14218
14219 if (tolist == 0)
14220 {
14221 /* "buf" is full, need to move text to an allocated buffer */
14222 if (prev == NULL)
14223 {
14224 prev = vim_strnsave(buf, buflen);
14225 prevlen = buflen;
14226 }
14227 else
14228 {
14229 s = alloc((unsigned)(prevlen + buflen));
14230 if (s != NULL)
14231 {
14232 mch_memmove(s, prev, prevlen);
14233 mch_memmove(s + prevlen, buf, buflen);
14234 vim_free(prev);
14235 prev = s;
14236 prevlen += buflen;
14237 }
14238 }
14239 filtd = 0;
14240 }
14241 else
14242 {
14243 mch_memmove(buf, buf + tolist, buflen - tolist);
14244 filtd -= tolist;
14245 }
14246 }
14247
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014248 /*
14249 * For a negative line count use only the lines at the end of the file,
14250 * free the rest.
14251 */
14252 if (maxline < 0)
14253 while (cnt > -maxline)
14254 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014255 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014256 --cnt;
14257 }
14258
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014259 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014260 fclose(fd);
14261}
14262
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014263#if defined(FEAT_RELTIME)
14264static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14265
14266/*
14267 * Convert a List to proftime_T.
14268 * Return FAIL when there is something wrong.
14269 */
14270 static int
14271list2proftime(arg, tm)
14272 typval_T *arg;
14273 proftime_T *tm;
14274{
14275 long n1, n2;
14276 int error = FALSE;
14277
14278 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14279 || arg->vval.v_list->lv_len != 2)
14280 return FAIL;
14281 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14282 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14283# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014284 tm->HighPart = n1;
14285 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014286# else
14287 tm->tv_sec = n1;
14288 tm->tv_usec = n2;
14289# endif
14290 return error ? FAIL : OK;
14291}
14292#endif /* FEAT_RELTIME */
14293
14294/*
14295 * "reltime()" function
14296 */
14297 static void
14298f_reltime(argvars, rettv)
14299 typval_T *argvars;
14300 typval_T *rettv;
14301{
14302#ifdef FEAT_RELTIME
14303 proftime_T res;
14304 proftime_T start;
14305
14306 if (argvars[0].v_type == VAR_UNKNOWN)
14307 {
14308 /* No arguments: get current time. */
14309 profile_start(&res);
14310 }
14311 else if (argvars[1].v_type == VAR_UNKNOWN)
14312 {
14313 if (list2proftime(&argvars[0], &res) == FAIL)
14314 return;
14315 profile_end(&res);
14316 }
14317 else
14318 {
14319 /* Two arguments: compute the difference. */
14320 if (list2proftime(&argvars[0], &start) == FAIL
14321 || list2proftime(&argvars[1], &res) == FAIL)
14322 return;
14323 profile_sub(&res, &start);
14324 }
14325
14326 if (rettv_list_alloc(rettv) == OK)
14327 {
14328 long n1, n2;
14329
14330# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014331 n1 = res.HighPart;
14332 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014333# else
14334 n1 = res.tv_sec;
14335 n2 = res.tv_usec;
14336# endif
14337 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14338 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14339 }
14340#endif
14341}
14342
14343/*
14344 * "reltimestr()" function
14345 */
14346 static void
14347f_reltimestr(argvars, rettv)
14348 typval_T *argvars;
14349 typval_T *rettv;
14350{
14351#ifdef FEAT_RELTIME
14352 proftime_T tm;
14353#endif
14354
14355 rettv->v_type = VAR_STRING;
14356 rettv->vval.v_string = NULL;
14357#ifdef FEAT_RELTIME
14358 if (list2proftime(&argvars[0], &tm) == OK)
14359 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14360#endif
14361}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014362
Bram Moolenaar0d660222005-01-07 21:51:51 +000014363#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14364static void make_connection __ARGS((void));
14365static int check_connection __ARGS((void));
14366
14367 static void
14368make_connection()
14369{
14370 if (X_DISPLAY == NULL
14371# ifdef FEAT_GUI
14372 && !gui.in_use
14373# endif
14374 )
14375 {
14376 x_force_connect = TRUE;
14377 setup_term_clip();
14378 x_force_connect = FALSE;
14379 }
14380}
14381
14382 static int
14383check_connection()
14384{
14385 make_connection();
14386 if (X_DISPLAY == NULL)
14387 {
14388 EMSG(_("E240: No connection to Vim server"));
14389 return FAIL;
14390 }
14391 return OK;
14392}
14393#endif
14394
14395#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014396static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014397
14398 static void
14399remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014400 typval_T *argvars;
14401 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014402 int expr;
14403{
14404 char_u *server_name;
14405 char_u *keys;
14406 char_u *r = NULL;
14407 char_u buf[NUMBUFLEN];
14408# ifdef WIN32
14409 HWND w;
14410# else
14411 Window w;
14412# endif
14413
14414 if (check_restricted() || check_secure())
14415 return;
14416
14417# ifdef FEAT_X11
14418 if (check_connection() == FAIL)
14419 return;
14420# endif
14421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014422 server_name = get_tv_string_chk(&argvars[0]);
14423 if (server_name == NULL)
14424 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014425 keys = get_tv_string_buf(&argvars[1], buf);
14426# ifdef WIN32
14427 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14428# else
14429 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14430 < 0)
14431# endif
14432 {
14433 if (r != NULL)
14434 EMSG(r); /* sending worked but evaluation failed */
14435 else
14436 EMSG2(_("E241: Unable to send to %s"), server_name);
14437 return;
14438 }
14439
14440 rettv->vval.v_string = r;
14441
14442 if (argvars[2].v_type != VAR_UNKNOWN)
14443 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014444 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014445 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014446 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014447
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014448 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014449 v.di_tv.v_type = VAR_STRING;
14450 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 idvar = get_tv_string_chk(&argvars[2]);
14452 if (idvar != NULL)
14453 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014454 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014455 }
14456}
14457#endif
14458
14459/*
14460 * "remote_expr()" function
14461 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014462 static void
14463f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014464 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014465 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014466{
14467 rettv->v_type = VAR_STRING;
14468 rettv->vval.v_string = NULL;
14469#ifdef FEAT_CLIENTSERVER
14470 remote_common(argvars, rettv, TRUE);
14471#endif
14472}
14473
14474/*
14475 * "remote_foreground()" function
14476 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014477 static void
14478f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014479 typval_T *argvars UNUSED;
14480 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014481{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014482#ifdef FEAT_CLIENTSERVER
14483# ifdef WIN32
14484 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014485 {
14486 char_u *server_name = get_tv_string_chk(&argvars[0]);
14487
14488 if (server_name != NULL)
14489 serverForeground(server_name);
14490 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014491# else
14492 /* Send a foreground() expression to the server. */
14493 argvars[1].v_type = VAR_STRING;
14494 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14495 argvars[2].v_type = VAR_UNKNOWN;
14496 remote_common(argvars, rettv, TRUE);
14497 vim_free(argvars[1].vval.v_string);
14498# endif
14499#endif
14500}
14501
Bram Moolenaar0d660222005-01-07 21:51:51 +000014502 static void
14503f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014504 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014505 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014506{
14507#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014508 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014509 char_u *s = NULL;
14510# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014511 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014512# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014513 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014514
14515 if (check_restricted() || check_secure())
14516 {
14517 rettv->vval.v_number = -1;
14518 return;
14519 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014520 serverid = get_tv_string_chk(&argvars[0]);
14521 if (serverid == NULL)
14522 {
14523 rettv->vval.v_number = -1;
14524 return; /* type error; errmsg already given */
14525 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014526# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014527 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014528 if (n == 0)
14529 rettv->vval.v_number = -1;
14530 else
14531 {
14532 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14533 rettv->vval.v_number = (s != NULL);
14534 }
14535# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014536 if (check_connection() == FAIL)
14537 return;
14538
14539 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014540 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014541# endif
14542
14543 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14544 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014545 char_u *retvar;
14546
Bram Moolenaar33570922005-01-25 22:26:29 +000014547 v.di_tv.v_type = VAR_STRING;
14548 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014549 retvar = get_tv_string_chk(&argvars[1]);
14550 if (retvar != NULL)
14551 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014552 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014553 }
14554#else
14555 rettv->vval.v_number = -1;
14556#endif
14557}
14558
Bram Moolenaar0d660222005-01-07 21:51:51 +000014559 static void
14560f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014561 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014562 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014563{
14564 char_u *r = NULL;
14565
14566#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014567 char_u *serverid = get_tv_string_chk(&argvars[0]);
14568
14569 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570 {
14571# ifdef WIN32
14572 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014573 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014574
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014575 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014576 if (n != 0)
14577 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14578 if (r == NULL)
14579# else
14580 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014581 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014582# endif
14583 EMSG(_("E277: Unable to read a server reply"));
14584 }
14585#endif
14586 rettv->v_type = VAR_STRING;
14587 rettv->vval.v_string = r;
14588}
14589
14590/*
14591 * "remote_send()" function
14592 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014593 static void
14594f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014595 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014596 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014597{
14598 rettv->v_type = VAR_STRING;
14599 rettv->vval.v_string = NULL;
14600#ifdef FEAT_CLIENTSERVER
14601 remote_common(argvars, rettv, FALSE);
14602#endif
14603}
14604
14605/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014606 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014607 */
14608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014609f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014610 typval_T *argvars;
14611 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014612{
Bram Moolenaar33570922005-01-25 22:26:29 +000014613 list_T *l;
14614 listitem_T *item, *item2;
14615 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014616 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014617 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014618 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014619 dict_T *d;
14620 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014621
Bram Moolenaar8c711452005-01-14 21:53:12 +000014622 if (argvars[0].v_type == VAR_DICT)
14623 {
14624 if (argvars[2].v_type != VAR_UNKNOWN)
14625 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014626 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014627 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014629 key = get_tv_string_chk(&argvars[1]);
14630 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014631 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014632 di = dict_find(d, key, -1);
14633 if (di == NULL)
14634 EMSG2(_(e_dictkey), key);
14635 else
14636 {
14637 *rettv = di->di_tv;
14638 init_tv(&di->di_tv);
14639 dictitem_remove(d, di);
14640 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014642 }
14643 }
14644 else if (argvars[0].v_type != VAR_LIST)
14645 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014646 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014647 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014649 int error = FALSE;
14650
14651 idx = get_tv_number_chk(&argvars[1], &error);
14652 if (error)
14653 ; /* type error: do nothing, errmsg already given */
14654 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014655 EMSGN(_(e_listidx), idx);
14656 else
14657 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014658 if (argvars[2].v_type == VAR_UNKNOWN)
14659 {
14660 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014661 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014662 *rettv = item->li_tv;
14663 vim_free(item);
14664 }
14665 else
14666 {
14667 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014668 end = get_tv_number_chk(&argvars[2], &error);
14669 if (error)
14670 ; /* type error: do nothing */
14671 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014672 EMSGN(_(e_listidx), end);
14673 else
14674 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014675 int cnt = 0;
14676
14677 for (li = item; li != NULL; li = li->li_next)
14678 {
14679 ++cnt;
14680 if (li == item2)
14681 break;
14682 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014683 if (li == NULL) /* didn't find "item2" after "item" */
14684 EMSG(_(e_invrange));
14685 else
14686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014687 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014688 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014689 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014690 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014691 l->lv_first = item;
14692 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014693 item->li_prev = NULL;
14694 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014695 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014696 }
14697 }
14698 }
14699 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014700 }
14701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702}
14703
14704/*
14705 * "rename({from}, {to})" function
14706 */
14707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014708f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014709 typval_T *argvars;
14710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711{
14712 char_u buf[NUMBUFLEN];
14713
14714 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014715 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014717 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14718 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719}
14720
14721/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014722 * "repeat()" function
14723 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014724 static void
14725f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014726 typval_T *argvars;
14727 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014728{
14729 char_u *p;
14730 int n;
14731 int slen;
14732 int len;
14733 char_u *r;
14734 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014735
14736 n = get_tv_number(&argvars[1]);
14737 if (argvars[0].v_type == VAR_LIST)
14738 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014739 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014740 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014741 if (list_extend(rettv->vval.v_list,
14742 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014743 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014744 }
14745 else
14746 {
14747 p = get_tv_string(&argvars[0]);
14748 rettv->v_type = VAR_STRING;
14749 rettv->vval.v_string = NULL;
14750
14751 slen = (int)STRLEN(p);
14752 len = slen * n;
14753 if (len <= 0)
14754 return;
14755
14756 r = alloc(len + 1);
14757 if (r != NULL)
14758 {
14759 for (i = 0; i < n; i++)
14760 mch_memmove(r + i * slen, p, (size_t)slen);
14761 r[len] = NUL;
14762 }
14763
14764 rettv->vval.v_string = r;
14765 }
14766}
14767
14768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 * "resolve()" function
14770 */
14771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014772f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014773 typval_T *argvars;
14774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775{
14776 char_u *p;
14777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014778 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779#ifdef FEAT_SHORTCUT
14780 {
14781 char_u *v = NULL;
14782
14783 v = mch_resolve_shortcut(p);
14784 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014785 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014787 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788 }
14789#else
14790# ifdef HAVE_READLINK
14791 {
14792 char_u buf[MAXPATHL + 1];
14793 char_u *cpy;
14794 int len;
14795 char_u *remain = NULL;
14796 char_u *q;
14797 int is_relative_to_current = FALSE;
14798 int has_trailing_pathsep = FALSE;
14799 int limit = 100;
14800
14801 p = vim_strsave(p);
14802
14803 if (p[0] == '.' && (vim_ispathsep(p[1])
14804 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14805 is_relative_to_current = TRUE;
14806
14807 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014808 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 has_trailing_pathsep = TRUE;
14810
14811 q = getnextcomp(p);
14812 if (*q != NUL)
14813 {
14814 /* Separate the first path component in "p", and keep the
14815 * remainder (beginning with the path separator). */
14816 remain = vim_strsave(q - 1);
14817 q[-1] = NUL;
14818 }
14819
14820 for (;;)
14821 {
14822 for (;;)
14823 {
14824 len = readlink((char *)p, (char *)buf, MAXPATHL);
14825 if (len <= 0)
14826 break;
14827 buf[len] = NUL;
14828
14829 if (limit-- == 0)
14830 {
14831 vim_free(p);
14832 vim_free(remain);
14833 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014834 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 goto fail;
14836 }
14837
14838 /* Ensure that the result will have a trailing path separator
14839 * if the argument has one. */
14840 if (remain == NULL && has_trailing_pathsep)
14841 add_pathsep(buf);
14842
14843 /* Separate the first path component in the link value and
14844 * concatenate the remainders. */
14845 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14846 if (*q != NUL)
14847 {
14848 if (remain == NULL)
14849 remain = vim_strsave(q - 1);
14850 else
14851 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014852 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853 if (cpy != NULL)
14854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855 vim_free(remain);
14856 remain = cpy;
14857 }
14858 }
14859 q[-1] = NUL;
14860 }
14861
14862 q = gettail(p);
14863 if (q > p && *q == NUL)
14864 {
14865 /* Ignore trailing path separator. */
14866 q[-1] = NUL;
14867 q = gettail(p);
14868 }
14869 if (q > p && !mch_isFullName(buf))
14870 {
14871 /* symlink is relative to directory of argument */
14872 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14873 if (cpy != NULL)
14874 {
14875 STRCPY(cpy, p);
14876 STRCPY(gettail(cpy), buf);
14877 vim_free(p);
14878 p = cpy;
14879 }
14880 }
14881 else
14882 {
14883 vim_free(p);
14884 p = vim_strsave(buf);
14885 }
14886 }
14887
14888 if (remain == NULL)
14889 break;
14890
14891 /* Append the first path component of "remain" to "p". */
14892 q = getnextcomp(remain + 1);
14893 len = q - remain - (*q != NUL);
14894 cpy = vim_strnsave(p, STRLEN(p) + len);
14895 if (cpy != NULL)
14896 {
14897 STRNCAT(cpy, remain, len);
14898 vim_free(p);
14899 p = cpy;
14900 }
14901 /* Shorten "remain". */
14902 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014903 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 else
14905 {
14906 vim_free(remain);
14907 remain = NULL;
14908 }
14909 }
14910
14911 /* If the result is a relative path name, make it explicitly relative to
14912 * the current directory if and only if the argument had this form. */
14913 if (!vim_ispathsep(*p))
14914 {
14915 if (is_relative_to_current
14916 && *p != NUL
14917 && !(p[0] == '.'
14918 && (p[1] == NUL
14919 || vim_ispathsep(p[1])
14920 || (p[1] == '.'
14921 && (p[2] == NUL
14922 || vim_ispathsep(p[2]))))))
14923 {
14924 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014925 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926 if (cpy != NULL)
14927 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928 vim_free(p);
14929 p = cpy;
14930 }
14931 }
14932 else if (!is_relative_to_current)
14933 {
14934 /* Strip leading "./". */
14935 q = p;
14936 while (q[0] == '.' && vim_ispathsep(q[1]))
14937 q += 2;
14938 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014939 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940 }
14941 }
14942
14943 /* Ensure that the result will have no trailing path separator
14944 * if the argument had none. But keep "/" or "//". */
14945 if (!has_trailing_pathsep)
14946 {
14947 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014948 if (after_pathsep(p, q))
14949 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950 }
14951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014952 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953 }
14954# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014955 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956# endif
14957#endif
14958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014959 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960
14961#ifdef HAVE_READLINK
14962fail:
14963#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014964 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965}
14966
14967/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014968 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969 */
14970 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014971f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014972 typval_T *argvars;
14973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974{
Bram Moolenaar33570922005-01-25 22:26:29 +000014975 list_T *l;
14976 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977
Bram Moolenaar0d660222005-01-07 21:51:51 +000014978 if (argvars[0].v_type != VAR_LIST)
14979 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014980 else if ((l = argvars[0].vval.v_list) != NULL
14981 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014982 {
14983 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014984 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014985 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014986 while (li != NULL)
14987 {
14988 ni = li->li_prev;
14989 list_append(l, li);
14990 li = ni;
14991 }
14992 rettv->vval.v_list = l;
14993 rettv->v_type = VAR_LIST;
14994 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014995 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997}
14998
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014999#define SP_NOMOVE 0x01 /* don't move cursor */
15000#define SP_REPEAT 0x02 /* repeat to find outer pair */
15001#define SP_RETCOUNT 0x04 /* return matchcount */
15002#define SP_SETPCMARK 0x08 /* set previous context mark */
15003#define SP_START 0x10 /* accept match at start position */
15004#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15005#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015006
Bram Moolenaar33570922005-01-25 22:26:29 +000015007static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015008
15009/*
15010 * Get flags for a search function.
15011 * Possibly sets "p_ws".
15012 * Returns BACKWARD, FORWARD or zero (for an error).
15013 */
15014 static int
15015get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015016 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015017 int *flagsp;
15018{
15019 int dir = FORWARD;
15020 char_u *flags;
15021 char_u nbuf[NUMBUFLEN];
15022 int mask;
15023
15024 if (varp->v_type != VAR_UNKNOWN)
15025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015026 flags = get_tv_string_buf_chk(varp, nbuf);
15027 if (flags == NULL)
15028 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015029 while (*flags != NUL)
15030 {
15031 switch (*flags)
15032 {
15033 case 'b': dir = BACKWARD; break;
15034 case 'w': p_ws = TRUE; break;
15035 case 'W': p_ws = FALSE; break;
15036 default: mask = 0;
15037 if (flagsp != NULL)
15038 switch (*flags)
15039 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015040 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015041 case 'e': mask = SP_END; break;
15042 case 'm': mask = SP_RETCOUNT; break;
15043 case 'n': mask = SP_NOMOVE; break;
15044 case 'p': mask = SP_SUBPAT; break;
15045 case 'r': mask = SP_REPEAT; break;
15046 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015047 }
15048 if (mask == 0)
15049 {
15050 EMSG2(_(e_invarg2), flags);
15051 dir = 0;
15052 }
15053 else
15054 *flagsp |= mask;
15055 }
15056 if (dir == 0)
15057 break;
15058 ++flags;
15059 }
15060 }
15061 return dir;
15062}
15063
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015065 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015067 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015068search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015069 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015070 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015071 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015073 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074 char_u *pat;
15075 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015076 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077 int save_p_ws = p_ws;
15078 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015079 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015080 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015081 proftime_T tm;
15082#ifdef FEAT_RELTIME
15083 long time_limit = 0;
15084#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015085 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015086 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015088 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015089 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015090 if (dir == 0)
15091 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015092 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015093 if (flags & SP_START)
15094 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015095 if (flags & SP_END)
15096 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015097
Bram Moolenaar76929292008-01-06 19:07:36 +000015098 /* Optional arguments: line number to stop searching and timeout. */
15099 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015100 {
15101 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15102 if (lnum_stop < 0)
15103 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015104#ifdef FEAT_RELTIME
15105 if (argvars[3].v_type != VAR_UNKNOWN)
15106 {
15107 time_limit = get_tv_number_chk(&argvars[3], NULL);
15108 if (time_limit < 0)
15109 goto theend;
15110 }
15111#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015112 }
15113
Bram Moolenaar76929292008-01-06 19:07:36 +000015114#ifdef FEAT_RELTIME
15115 /* Set the time limit, if there is one. */
15116 profile_setlimit(time_limit, &tm);
15117#endif
15118
Bram Moolenaar231334e2005-07-25 20:46:57 +000015119 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015120 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015121 * Check to make sure only those flags are set.
15122 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15123 * flags cannot be set. Check for that condition also.
15124 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015125 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015126 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015128 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015129 goto theend;
15130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015132 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015133 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015134 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015135 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015137 if (flags & SP_SUBPAT)
15138 retval = subpatnum;
15139 else
15140 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015141 if (flags & SP_SETPCMARK)
15142 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015144 if (match_pos != NULL)
15145 {
15146 /* Store the match cursor position */
15147 match_pos->lnum = pos.lnum;
15148 match_pos->col = pos.col + 1;
15149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 /* "/$" will put the cursor after the end of the line, may need to
15151 * correct that here */
15152 check_cursor();
15153 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015154
15155 /* If 'n' flag is used: restore cursor position. */
15156 if (flags & SP_NOMOVE)
15157 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015158 else
15159 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015160theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015162
15163 return retval;
15164}
15165
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015166#ifdef FEAT_FLOAT
15167/*
15168 * "round({float})" function
15169 */
15170 static void
15171f_round(argvars, rettv)
15172 typval_T *argvars;
15173 typval_T *rettv;
15174{
15175 float_T f;
15176
15177 rettv->v_type = VAR_FLOAT;
15178 if (get_float_arg(argvars, &f) == OK)
15179 /* round() is not in C90, use ceil() or floor() instead. */
15180 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15181 else
15182 rettv->vval.v_float = 0.0;
15183}
15184#endif
15185
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015186/*
15187 * "search()" function
15188 */
15189 static void
15190f_search(argvars, rettv)
15191 typval_T *argvars;
15192 typval_T *rettv;
15193{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015194 int flags = 0;
15195
15196 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197}
15198
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015200 * "searchdecl()" function
15201 */
15202 static void
15203f_searchdecl(argvars, rettv)
15204 typval_T *argvars;
15205 typval_T *rettv;
15206{
15207 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015208 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015209 int error = FALSE;
15210 char_u *name;
15211
15212 rettv->vval.v_number = 1; /* default: FAIL */
15213
15214 name = get_tv_string_chk(&argvars[0]);
15215 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015216 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015217 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015218 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15219 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15220 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015221 if (!error && name != NULL)
15222 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015223 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015224}
15225
15226/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015227 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015229 static int
15230searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015231 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015232 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233{
15234 char_u *spat, *mpat, *epat;
15235 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237 int dir;
15238 int flags = 0;
15239 char_u nbuf1[NUMBUFLEN];
15240 char_u nbuf2[NUMBUFLEN];
15241 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015242 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015243 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015244 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015245
Bram Moolenaar071d4272004-06-13 20:20:40 +000015246 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015247 spat = get_tv_string_chk(&argvars[0]);
15248 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15249 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15250 if (spat == NULL || mpat == NULL || epat == NULL)
15251 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253 /* Handle the optional fourth argument: flags */
15254 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015255 if (dir == 0)
15256 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015257
15258 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015259 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15260 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015261 if ((flags & (SP_END | SP_SUBPAT)) != 0
15262 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015263 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015264 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015265 goto theend;
15266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015268 /* Using 'r' implies 'W', otherwise it doesn't work. */
15269 if (flags & SP_REPEAT)
15270 p_ws = FALSE;
15271
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015272 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015273 if (argvars[3].v_type == VAR_UNKNOWN
15274 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275 skip = (char_u *)"";
15276 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015278 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015279 if (argvars[5].v_type != VAR_UNKNOWN)
15280 {
15281 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15282 if (lnum_stop < 0)
15283 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015284#ifdef FEAT_RELTIME
15285 if (argvars[6].v_type != VAR_UNKNOWN)
15286 {
15287 time_limit = get_tv_number_chk(&argvars[6], NULL);
15288 if (time_limit < 0)
15289 goto theend;
15290 }
15291#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015292 }
15293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015294 if (skip == NULL)
15295 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015296
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015297 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015298 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015299
15300theend:
15301 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015302
15303 return retval;
15304}
15305
15306/*
15307 * "searchpair()" function
15308 */
15309 static void
15310f_searchpair(argvars, rettv)
15311 typval_T *argvars;
15312 typval_T *rettv;
15313{
15314 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15315}
15316
15317/*
15318 * "searchpairpos()" function
15319 */
15320 static void
15321f_searchpairpos(argvars, rettv)
15322 typval_T *argvars;
15323 typval_T *rettv;
15324{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015325 pos_T match_pos;
15326 int lnum = 0;
15327 int col = 0;
15328
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015329 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015330 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015331
15332 if (searchpair_cmn(argvars, &match_pos) > 0)
15333 {
15334 lnum = match_pos.lnum;
15335 col = match_pos.col;
15336 }
15337
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015338 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15339 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015340}
15341
15342/*
15343 * Search for a start/middle/end thing.
15344 * Used by searchpair(), see its documentation for the details.
15345 * Returns 0 or -1 for no match,
15346 */
15347 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015348do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15349 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015350 char_u *spat; /* start pattern */
15351 char_u *mpat; /* middle pattern */
15352 char_u *epat; /* end pattern */
15353 int dir; /* BACKWARD or FORWARD */
15354 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015355 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015356 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015357 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015358 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015359{
15360 char_u *save_cpo;
15361 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15362 long retval = 0;
15363 pos_T pos;
15364 pos_T firstpos;
15365 pos_T foundpos;
15366 pos_T save_cursor;
15367 pos_T save_pos;
15368 int n;
15369 int r;
15370 int nest = 1;
15371 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015372 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015373 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015374
15375 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15376 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015377 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015378
Bram Moolenaar76929292008-01-06 19:07:36 +000015379#ifdef FEAT_RELTIME
15380 /* Set the time limit, if there is one. */
15381 profile_setlimit(time_limit, &tm);
15382#endif
15383
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015384 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15385 * start/middle/end (pat3, for the top pair). */
15386 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15387 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15388 if (pat2 == NULL || pat3 == NULL)
15389 goto theend;
15390 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15391 if (*mpat == NUL)
15392 STRCPY(pat3, pat2);
15393 else
15394 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15395 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015396 if (flags & SP_START)
15397 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015398
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 save_cursor = curwin->w_cursor;
15400 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015401 clearpos(&firstpos);
15402 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 pat = pat3;
15404 for (;;)
15405 {
15406 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015407 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15409 /* didn't find it or found the first match again: FAIL */
15410 break;
15411
15412 if (firstpos.lnum == 0)
15413 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015414 if (equalpos(pos, foundpos))
15415 {
15416 /* Found the same position again. Can happen with a pattern that
15417 * has "\zs" at the end and searching backwards. Advance one
15418 * character and try again. */
15419 if (dir == BACKWARD)
15420 decl(&pos);
15421 else
15422 incl(&pos);
15423 }
15424 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015426 /* clear the start flag to avoid getting stuck here */
15427 options &= ~SEARCH_START;
15428
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 /* If the skip pattern matches, ignore this match. */
15430 if (*skip != NUL)
15431 {
15432 save_pos = curwin->w_cursor;
15433 curwin->w_cursor = pos;
15434 r = eval_to_bool(skip, &err, NULL, FALSE);
15435 curwin->w_cursor = save_pos;
15436 if (err)
15437 {
15438 /* Evaluating {skip} caused an error, break here. */
15439 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015440 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 break;
15442 }
15443 if (r)
15444 continue;
15445 }
15446
15447 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15448 {
15449 /* Found end when searching backwards or start when searching
15450 * forward: nested pair. */
15451 ++nest;
15452 pat = pat2; /* nested, don't search for middle */
15453 }
15454 else
15455 {
15456 /* Found end when searching forward or start when searching
15457 * backward: end of (nested) pair; or found middle in outer pair. */
15458 if (--nest == 1)
15459 pat = pat3; /* outer level, search for middle */
15460 }
15461
15462 if (nest == 0)
15463 {
15464 /* Found the match: return matchcount or line number. */
15465 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015466 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015468 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015469 if (flags & SP_SETPCMARK)
15470 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471 curwin->w_cursor = pos;
15472 if (!(flags & SP_REPEAT))
15473 break;
15474 nest = 1; /* search for next unmatched */
15475 }
15476 }
15477
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015478 if (match_pos != NULL)
15479 {
15480 /* Store the match cursor position */
15481 match_pos->lnum = curwin->w_cursor.lnum;
15482 match_pos->col = curwin->w_cursor.col + 1;
15483 }
15484
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015486 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487 curwin->w_cursor = save_cursor;
15488
15489theend:
15490 vim_free(pat2);
15491 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015492 if (p_cpo == empty_option)
15493 p_cpo = save_cpo;
15494 else
15495 /* Darn, evaluating the {skip} expression changed the value. */
15496 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015497
15498 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499}
15500
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015501/*
15502 * "searchpos()" function
15503 */
15504 static void
15505f_searchpos(argvars, rettv)
15506 typval_T *argvars;
15507 typval_T *rettv;
15508{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015509 pos_T match_pos;
15510 int lnum = 0;
15511 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015512 int n;
15513 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015514
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015515 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015516 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015517
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015518 n = search_cmn(argvars, &match_pos, &flags);
15519 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015520 {
15521 lnum = match_pos.lnum;
15522 col = match_pos.col;
15523 }
15524
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015525 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15526 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015527 if (flags & SP_SUBPAT)
15528 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015529}
15530
15531
Bram Moolenaar0d660222005-01-07 21:51:51 +000015532 static void
15533f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015534 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015537#ifdef FEAT_CLIENTSERVER
15538 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015539 char_u *server = get_tv_string_chk(&argvars[0]);
15540 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541
Bram Moolenaar0d660222005-01-07 21:51:51 +000015542 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015543 if (server == NULL || reply == NULL)
15544 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015545 if (check_restricted() || check_secure())
15546 return;
15547# ifdef FEAT_X11
15548 if (check_connection() == FAIL)
15549 return;
15550# endif
15551
15552 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015554 EMSG(_("E258: Unable to send to client"));
15555 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015557 rettv->vval.v_number = 0;
15558#else
15559 rettv->vval.v_number = -1;
15560#endif
15561}
15562
Bram Moolenaar0d660222005-01-07 21:51:51 +000015563 static void
15564f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015565 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015566 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015567{
15568 char_u *r = NULL;
15569
15570#ifdef FEAT_CLIENTSERVER
15571# ifdef WIN32
15572 r = serverGetVimNames();
15573# else
15574 make_connection();
15575 if (X_DISPLAY != NULL)
15576 r = serverGetVimNames(X_DISPLAY);
15577# endif
15578#endif
15579 rettv->v_type = VAR_STRING;
15580 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581}
15582
15583/*
15584 * "setbufvar()" function
15585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015587f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015588 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015589 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590{
15591 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015594 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595 char_u nbuf[NUMBUFLEN];
15596
15597 if (check_restricted() || check_secure())
15598 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015599 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15600 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015601 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 varp = &argvars[2];
15603
15604 if (buf != NULL && varname != NULL && varp != NULL)
15605 {
15606 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608
15609 if (*varname == '&')
15610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015611 long numval;
15612 char_u *strval;
15613 int error = FALSE;
15614
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015616 numval = get_tv_number_chk(varp, &error);
15617 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015618 if (!error && strval != NULL)
15619 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 }
15621 else
15622 {
15623 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15624 if (bufvarname != NULL)
15625 {
15626 STRCPY(bufvarname, "b:");
15627 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015628 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629 vim_free(bufvarname);
15630 }
15631 }
15632
15633 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636}
15637
15638/*
15639 * "setcmdpos()" function
15640 */
15641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015642f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015643 typval_T *argvars;
15644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015646 int pos = (int)get_tv_number(&argvars[0]) - 1;
15647
15648 if (pos >= 0)
15649 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650}
15651
15652/*
15653 * "setline()" function
15654 */
15655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015656f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015657 typval_T *argvars;
15658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659{
15660 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015661 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015662 list_T *l = NULL;
15663 listitem_T *li = NULL;
15664 long added = 0;
15665 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015667 lnum = get_tv_lnum(&argvars[0]);
15668 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015670 l = argvars[1].vval.v_list;
15671 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015673 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015674 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015675
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015676 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015677 for (;;)
15678 {
15679 if (l != NULL)
15680 {
15681 /* list argument, get next string */
15682 if (li == NULL)
15683 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015684 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015685 li = li->li_next;
15686 }
15687
15688 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015689 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015690 break;
15691 if (lnum <= curbuf->b_ml.ml_line_count)
15692 {
15693 /* existing line, replace it */
15694 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15695 {
15696 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015697 if (lnum == curwin->w_cursor.lnum)
15698 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015699 rettv->vval.v_number = 0; /* OK */
15700 }
15701 }
15702 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15703 {
15704 /* lnum is one past the last line, append the line */
15705 ++added;
15706 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15707 rettv->vval.v_number = 0; /* OK */
15708 }
15709
15710 if (l == NULL) /* only one string argument */
15711 break;
15712 ++lnum;
15713 }
15714
15715 if (added > 0)
15716 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717}
15718
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015719static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15720
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015722 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015723 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015724 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015725set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015726 win_T *wp UNUSED;
15727 typval_T *list_arg UNUSED;
15728 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015729 typval_T *rettv;
15730{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015731#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015732 char_u *act;
15733 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015734#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015735
Bram Moolenaar2641f772005-03-25 21:58:17 +000015736 rettv->vval.v_number = -1;
15737
15738#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015739 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015740 EMSG(_(e_listreq));
15741 else
15742 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015743 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015744
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015745 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015746 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015747 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015748 if (act == NULL)
15749 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015750 if (*act == 'a' || *act == 'r')
15751 action = *act;
15752 }
15753
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015754 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015755 rettv->vval.v_number = 0;
15756 }
15757#endif
15758}
15759
15760/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015761 * "setloclist()" function
15762 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015763 static void
15764f_setloclist(argvars, rettv)
15765 typval_T *argvars;
15766 typval_T *rettv;
15767{
15768 win_T *win;
15769
15770 rettv->vval.v_number = -1;
15771
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015772 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015773 if (win != NULL)
15774 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15775}
15776
15777/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015778 * "setmatches()" function
15779 */
15780 static void
15781f_setmatches(argvars, rettv)
15782 typval_T *argvars;
15783 typval_T *rettv;
15784{
15785#ifdef FEAT_SEARCH_EXTRA
15786 list_T *l;
15787 listitem_T *li;
15788 dict_T *d;
15789
15790 rettv->vval.v_number = -1;
15791 if (argvars[0].v_type != VAR_LIST)
15792 {
15793 EMSG(_(e_listreq));
15794 return;
15795 }
15796 if ((l = argvars[0].vval.v_list) != NULL)
15797 {
15798
15799 /* To some extent make sure that we are dealing with a list from
15800 * "getmatches()". */
15801 li = l->lv_first;
15802 while (li != NULL)
15803 {
15804 if (li->li_tv.v_type != VAR_DICT
15805 || (d = li->li_tv.vval.v_dict) == NULL)
15806 {
15807 EMSG(_(e_invarg));
15808 return;
15809 }
15810 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15811 && dict_find(d, (char_u *)"pattern", -1) != NULL
15812 && dict_find(d, (char_u *)"priority", -1) != NULL
15813 && dict_find(d, (char_u *)"id", -1) != NULL))
15814 {
15815 EMSG(_(e_invarg));
15816 return;
15817 }
15818 li = li->li_next;
15819 }
15820
15821 clear_matches(curwin);
15822 li = l->lv_first;
15823 while (li != NULL)
15824 {
15825 d = li->li_tv.vval.v_dict;
15826 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15827 get_dict_string(d, (char_u *)"pattern", FALSE),
15828 (int)get_dict_number(d, (char_u *)"priority"),
15829 (int)get_dict_number(d, (char_u *)"id"));
15830 li = li->li_next;
15831 }
15832 rettv->vval.v_number = 0;
15833 }
15834#endif
15835}
15836
15837/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015838 * "setpos()" function
15839 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015840 static void
15841f_setpos(argvars, rettv)
15842 typval_T *argvars;
15843 typval_T *rettv;
15844{
15845 pos_T pos;
15846 int fnum;
15847 char_u *name;
15848
Bram Moolenaar08250432008-02-13 11:42:46 +000015849 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015850 name = get_tv_string_chk(argvars);
15851 if (name != NULL)
15852 {
15853 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15854 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015855 if (--pos.col < 0)
15856 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015857 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015858 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015859 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015860 if (fnum == curbuf->b_fnum)
15861 {
15862 curwin->w_cursor = pos;
15863 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015864 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015865 }
15866 else
15867 EMSG(_(e_invarg));
15868 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015869 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15870 {
15871 /* set mark */
15872 if (setmark_pos(name[1], &pos, fnum) == OK)
15873 rettv->vval.v_number = 0;
15874 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015875 else
15876 EMSG(_(e_invarg));
15877 }
15878 }
15879}
15880
15881/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015882 * "setqflist()" function
15883 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015884 static void
15885f_setqflist(argvars, rettv)
15886 typval_T *argvars;
15887 typval_T *rettv;
15888{
15889 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15890}
15891
15892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893 * "setreg()" function
15894 */
15895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015896f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015897 typval_T *argvars;
15898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015899{
15900 int regname;
15901 char_u *strregname;
15902 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015903 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 int append;
15905 char_u yank_type;
15906 long block_len;
15907
15908 block_len = -1;
15909 yank_type = MAUTO;
15910 append = FALSE;
15911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015912 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015913 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015915 if (strregname == NULL)
15916 return; /* type error; errmsg already given */
15917 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918 if (regname == 0 || regname == '@')
15919 regname = '"';
15920 else if (regname == '=')
15921 return;
15922
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015923 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015925 stropt = get_tv_string_chk(&argvars[2]);
15926 if (stropt == NULL)
15927 return; /* type error */
15928 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 switch (*stropt)
15930 {
15931 case 'a': case 'A': /* append */
15932 append = TRUE;
15933 break;
15934 case 'v': case 'c': /* character-wise selection */
15935 yank_type = MCHAR;
15936 break;
15937 case 'V': case 'l': /* line-wise selection */
15938 yank_type = MLINE;
15939 break;
15940#ifdef FEAT_VISUAL
15941 case 'b': case Ctrl_V: /* block-wise selection */
15942 yank_type = MBLOCK;
15943 if (VIM_ISDIGIT(stropt[1]))
15944 {
15945 ++stropt;
15946 block_len = getdigits(&stropt) - 1;
15947 --stropt;
15948 }
15949 break;
15950#endif
15951 }
15952 }
15953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015954 strval = get_tv_string_chk(&argvars[1]);
15955 if (strval != NULL)
15956 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015958 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015959}
15960
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015961/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020015962 * "settabvar()" function
15963 */
15964 static void
15965f_settabvar(argvars, rettv)
15966 typval_T *argvars;
15967 typval_T *rettv;
15968{
15969 tabpage_T *save_curtab;
15970 char_u *varname, *tabvarname;
15971 typval_T *varp;
15972 tabpage_T *tp;
15973
15974 rettv->vval.v_number = 0;
15975
15976 if (check_restricted() || check_secure())
15977 return;
15978
15979 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15980 varname = get_tv_string_chk(&argvars[1]);
15981 varp = &argvars[2];
15982
15983 if (tp != NULL && varname != NULL && varp != NULL)
15984 {
15985 save_curtab = curtab;
15986 goto_tabpage_tp(tp);
15987
15988 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
15989 if (tabvarname != NULL)
15990 {
15991 STRCPY(tabvarname, "t:");
15992 STRCPY(tabvarname + 2, varname);
15993 set_var(tabvarname, varp, TRUE);
15994 vim_free(tabvarname);
15995 }
15996
15997 /* Restore current tabpage */
15998 if (valid_tabpage(save_curtab))
15999 goto_tabpage_tp(save_curtab);
16000 }
16001}
16002
16003/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016004 * "settabwinvar()" function
16005 */
16006 static void
16007f_settabwinvar(argvars, rettv)
16008 typval_T *argvars;
16009 typval_T *rettv;
16010{
16011 setwinvar(argvars, rettv, 1);
16012}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013
16014/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016015 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016018f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016019 typval_T *argvars;
16020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016022 setwinvar(argvars, rettv, 0);
16023}
16024
16025/*
16026 * "setwinvar()" and "settabwinvar()" functions
16027 */
16028 static void
16029setwinvar(argvars, rettv, off)
16030 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016031 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016032 int off;
16033{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 win_T *win;
16035#ifdef FEAT_WINDOWS
16036 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016037 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038#endif
16039 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016040 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016042 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043
16044 if (check_restricted() || check_secure())
16045 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016046
16047#ifdef FEAT_WINDOWS
16048 if (off == 1)
16049 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16050 else
16051 tp = curtab;
16052#endif
16053 win = find_win_by_nr(&argvars[off], tp);
16054 varname = get_tv_string_chk(&argvars[off + 1]);
16055 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056
16057 if (win != NULL && varname != NULL && varp != NULL)
16058 {
16059#ifdef FEAT_WINDOWS
16060 /* set curwin to be our win, temporarily */
16061 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016062 save_curtab = curtab;
16063 goto_tabpage_tp(tp);
16064 if (!win_valid(win))
16065 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066 curwin = win;
16067 curbuf = curwin->w_buffer;
16068#endif
16069
16070 if (*varname == '&')
16071 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016072 long numval;
16073 char_u *strval;
16074 int error = FALSE;
16075
Bram Moolenaar071d4272004-06-13 20:20:40 +000016076 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016077 numval = get_tv_number_chk(varp, &error);
16078 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016079 if (!error && strval != NULL)
16080 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 }
16082 else
16083 {
16084 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16085 if (winvarname != NULL)
16086 {
16087 STRCPY(winvarname, "w:");
16088 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016089 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090 vim_free(winvarname);
16091 }
16092 }
16093
16094#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016095 /* Restore current tabpage and window, if still valid (autocomands can
16096 * make them invalid). */
16097 if (valid_tabpage(save_curtab))
16098 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 if (win_valid(save_curwin))
16100 {
16101 curwin = save_curwin;
16102 curbuf = curwin->w_buffer;
16103 }
16104#endif
16105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106}
16107
16108/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016109 * "shellescape({string})" function
16110 */
16111 static void
16112f_shellescape(argvars, rettv)
16113 typval_T *argvars;
16114 typval_T *rettv;
16115{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016116 rettv->vval.v_string = vim_strsave_shellescape(
16117 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016118 rettv->v_type = VAR_STRING;
16119}
16120
16121/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016122 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123 */
16124 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016125f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016126 typval_T *argvars;
16127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016129 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130
Bram Moolenaar0d660222005-01-07 21:51:51 +000016131 p = get_tv_string(&argvars[0]);
16132 rettv->vval.v_string = vim_strsave(p);
16133 simplify_filename(rettv->vval.v_string); /* simplify in place */
16134 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135}
16136
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016137#ifdef FEAT_FLOAT
16138/*
16139 * "sin()" function
16140 */
16141 static void
16142f_sin(argvars, rettv)
16143 typval_T *argvars;
16144 typval_T *rettv;
16145{
16146 float_T f;
16147
16148 rettv->v_type = VAR_FLOAT;
16149 if (get_float_arg(argvars, &f) == OK)
16150 rettv->vval.v_float = sin(f);
16151 else
16152 rettv->vval.v_float = 0.0;
16153}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016154
16155/*
16156 * "sinh()" function
16157 */
16158 static void
16159f_sinh(argvars, rettv)
16160 typval_T *argvars;
16161 typval_T *rettv;
16162{
16163 float_T f;
16164
16165 rettv->v_type = VAR_FLOAT;
16166 if (get_float_arg(argvars, &f) == OK)
16167 rettv->vval.v_float = sinh(f);
16168 else
16169 rettv->vval.v_float = 0.0;
16170}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016171#endif
16172
Bram Moolenaar0d660222005-01-07 21:51:51 +000016173static int
16174#ifdef __BORLANDC__
16175 _RTLENTRYF
16176#endif
16177 item_compare __ARGS((const void *s1, const void *s2));
16178static int
16179#ifdef __BORLANDC__
16180 _RTLENTRYF
16181#endif
16182 item_compare2 __ARGS((const void *s1, const void *s2));
16183
16184static int item_compare_ic;
16185static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016186static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016187#define ITEM_COMPARE_FAIL 999
16188
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016190 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016192 static int
16193#ifdef __BORLANDC__
16194_RTLENTRYF
16195#endif
16196item_compare(s1, s2)
16197 const void *s1;
16198 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016200 char_u *p1, *p2;
16201 char_u *tofree1, *tofree2;
16202 int res;
16203 char_u numbuf1[NUMBUFLEN];
16204 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016206 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16207 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016208 if (p1 == NULL)
16209 p1 = (char_u *)"";
16210 if (p2 == NULL)
16211 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016212 if (item_compare_ic)
16213 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016215 res = STRCMP(p1, p2);
16216 vim_free(tofree1);
16217 vim_free(tofree2);
16218 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219}
16220
16221 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016222#ifdef __BORLANDC__
16223_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016225item_compare2(s1, s2)
16226 const void *s1;
16227 const void *s2;
16228{
16229 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016230 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016231 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016232 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016234 /* shortcut after failure in previous call; compare all items equal */
16235 if (item_compare_func_err)
16236 return 0;
16237
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016238 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16239 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016240 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16241 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016242
16243 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016244 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016245 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016246 clear_tv(&argv[0]);
16247 clear_tv(&argv[1]);
16248
16249 if (res == FAIL)
16250 res = ITEM_COMPARE_FAIL;
16251 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016252 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16253 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016254 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016255 clear_tv(&rettv);
16256 return res;
16257}
16258
16259/*
16260 * "sort({list})" function
16261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016263f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016264 typval_T *argvars;
16265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266{
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 list_T *l;
16268 listitem_T *li;
16269 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016270 long len;
16271 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272
Bram Moolenaar0d660222005-01-07 21:51:51 +000016273 if (argvars[0].v_type != VAR_LIST)
16274 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 else
16276 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016277 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016278 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016279 return;
16280 rettv->vval.v_list = l;
16281 rettv->v_type = VAR_LIST;
16282 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016283
Bram Moolenaar0d660222005-01-07 21:51:51 +000016284 len = list_len(l);
16285 if (len <= 1)
16286 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287
Bram Moolenaar0d660222005-01-07 21:51:51 +000016288 item_compare_ic = FALSE;
16289 item_compare_func = NULL;
16290 if (argvars[1].v_type != VAR_UNKNOWN)
16291 {
16292 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016293 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016294 else
16295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016296 int error = FALSE;
16297
16298 i = get_tv_number_chk(&argvars[1], &error);
16299 if (error)
16300 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016301 if (i == 1)
16302 item_compare_ic = TRUE;
16303 else
16304 item_compare_func = get_tv_string(&argvars[1]);
16305 }
16306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016309 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016310 if (ptrs == NULL)
16311 return;
16312 i = 0;
16313 for (li = l->lv_first; li != NULL; li = li->li_next)
16314 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016316 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016317 /* test the compare function */
16318 if (item_compare_func != NULL
16319 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16320 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016321 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016323 {
16324 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016325 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016326 item_compare_func == NULL ? item_compare : item_compare2);
16327
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016328 if (!item_compare_func_err)
16329 {
16330 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016331 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016332 l->lv_len = 0;
16333 for (i = 0; i < len; ++i)
16334 list_append(l, ptrs[i]);
16335 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016336 }
16337
16338 vim_free(ptrs);
16339 }
16340}
16341
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016342/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016343 * "soundfold({word})" function
16344 */
16345 static void
16346f_soundfold(argvars, rettv)
16347 typval_T *argvars;
16348 typval_T *rettv;
16349{
16350 char_u *s;
16351
16352 rettv->v_type = VAR_STRING;
16353 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016354#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016355 rettv->vval.v_string = eval_soundfold(s);
16356#else
16357 rettv->vval.v_string = vim_strsave(s);
16358#endif
16359}
16360
16361/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016362 * "spellbadword()" function
16363 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016364 static void
16365f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016366 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016367 typval_T *rettv;
16368{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016369 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016370 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016371 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016372
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016373 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016374 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016375
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016376#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016377 if (argvars[0].v_type == VAR_UNKNOWN)
16378 {
16379 /* Find the start and length of the badly spelled word. */
16380 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16381 if (len != 0)
16382 word = ml_get_cursor();
16383 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016384 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016385 {
16386 char_u *str = get_tv_string_chk(&argvars[0]);
16387 int capcol = -1;
16388
16389 if (str != NULL)
16390 {
16391 /* Check the argument for spelling. */
16392 while (*str != NUL)
16393 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016394 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016395 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016396 {
16397 word = str;
16398 break;
16399 }
16400 str += len;
16401 }
16402 }
16403 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016404#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016405
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016406 list_append_string(rettv->vval.v_list, word, len);
16407 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016408 attr == HLF_SPB ? "bad" :
16409 attr == HLF_SPR ? "rare" :
16410 attr == HLF_SPL ? "local" :
16411 attr == HLF_SPC ? "caps" :
16412 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016413}
16414
16415/*
16416 * "spellsuggest()" function
16417 */
16418 static void
16419f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016420 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016421 typval_T *rettv;
16422{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016423#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016424 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016425 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016426 int maxcount;
16427 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016428 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016429 listitem_T *li;
16430 int need_capital = FALSE;
16431#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016432
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016433 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016434 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016435
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016436#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016437 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016438 {
16439 str = get_tv_string(&argvars[0]);
16440 if (argvars[1].v_type != VAR_UNKNOWN)
16441 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016442 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016443 if (maxcount <= 0)
16444 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016445 if (argvars[2].v_type != VAR_UNKNOWN)
16446 {
16447 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16448 if (typeerr)
16449 return;
16450 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016451 }
16452 else
16453 maxcount = 25;
16454
Bram Moolenaar4770d092006-01-12 23:22:24 +000016455 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016456
16457 for (i = 0; i < ga.ga_len; ++i)
16458 {
16459 str = ((char_u **)ga.ga_data)[i];
16460
16461 li = listitem_alloc();
16462 if (li == NULL)
16463 vim_free(str);
16464 else
16465 {
16466 li->li_tv.v_type = VAR_STRING;
16467 li->li_tv.v_lock = 0;
16468 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016469 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016470 }
16471 }
16472 ga_clear(&ga);
16473 }
16474#endif
16475}
16476
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016478f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016479 typval_T *argvars;
16480 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016481{
16482 char_u *str;
16483 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016484 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016485 regmatch_T regmatch;
16486 char_u patbuf[NUMBUFLEN];
16487 char_u *save_cpo;
16488 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016490 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016491 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016492
16493 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16494 save_cpo = p_cpo;
16495 p_cpo = (char_u *)"";
16496
16497 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016498 if (argvars[1].v_type != VAR_UNKNOWN)
16499 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016500 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16501 if (pat == NULL)
16502 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016503 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016504 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016505 }
16506 if (pat == NULL || *pat == NUL)
16507 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016508
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016509 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016511 if (typeerr)
16512 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513
Bram Moolenaar0d660222005-01-07 21:51:51 +000016514 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16515 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016518 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016519 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016520 if (*str == NUL)
16521 match = FALSE; /* empty item at the end */
16522 else
16523 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016524 if (match)
16525 end = regmatch.startp[0];
16526 else
16527 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016528 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16529 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016530 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016531 if (list_append_string(rettv->vval.v_list, str,
16532 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016533 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016534 }
16535 if (!match)
16536 break;
16537 /* Advance to just after the match. */
16538 if (regmatch.endp[0] > str)
16539 col = 0;
16540 else
16541 {
16542 /* Don't get stuck at the same match. */
16543#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016544 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016545#else
16546 col = 1;
16547#endif
16548 }
16549 str = regmatch.endp[0];
16550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551
Bram Moolenaar0d660222005-01-07 21:51:51 +000016552 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554
Bram Moolenaar0d660222005-01-07 21:51:51 +000016555 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016556}
16557
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016558#ifdef FEAT_FLOAT
16559/*
16560 * "sqrt()" function
16561 */
16562 static void
16563f_sqrt(argvars, rettv)
16564 typval_T *argvars;
16565 typval_T *rettv;
16566{
16567 float_T f;
16568
16569 rettv->v_type = VAR_FLOAT;
16570 if (get_float_arg(argvars, &f) == OK)
16571 rettv->vval.v_float = sqrt(f);
16572 else
16573 rettv->vval.v_float = 0.0;
16574}
16575
16576/*
16577 * "str2float()" function
16578 */
16579 static void
16580f_str2float(argvars, rettv)
16581 typval_T *argvars;
16582 typval_T *rettv;
16583{
16584 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16585
16586 if (*p == '+')
16587 p = skipwhite(p + 1);
16588 (void)string2float(p, &rettv->vval.v_float);
16589 rettv->v_type = VAR_FLOAT;
16590}
16591#endif
16592
Bram Moolenaar2c932302006-03-18 21:42:09 +000016593/*
16594 * "str2nr()" function
16595 */
16596 static void
16597f_str2nr(argvars, rettv)
16598 typval_T *argvars;
16599 typval_T *rettv;
16600{
16601 int base = 10;
16602 char_u *p;
16603 long n;
16604
16605 if (argvars[1].v_type != VAR_UNKNOWN)
16606 {
16607 base = get_tv_number(&argvars[1]);
16608 if (base != 8 && base != 10 && base != 16)
16609 {
16610 EMSG(_(e_invarg));
16611 return;
16612 }
16613 }
16614
16615 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016616 if (*p == '+')
16617 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016618 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16619 rettv->vval.v_number = n;
16620}
16621
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622#ifdef HAVE_STRFTIME
16623/*
16624 * "strftime({format}[, {time}])" function
16625 */
16626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016627f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016628 typval_T *argvars;
16629 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630{
16631 char_u result_buf[256];
16632 struct tm *curtime;
16633 time_t seconds;
16634 char_u *p;
16635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016636 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016638 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016639 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640 seconds = time(NULL);
16641 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016642 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 curtime = localtime(&seconds);
16644 /* MSVC returns NULL for an invalid value of seconds. */
16645 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016646 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647 else
16648 {
16649# ifdef FEAT_MBYTE
16650 vimconv_T conv;
16651 char_u *enc;
16652
16653 conv.vc_type = CONV_NONE;
16654 enc = enc_locale();
16655 convert_setup(&conv, p_enc, enc);
16656 if (conv.vc_type != CONV_NONE)
16657 p = string_convert(&conv, p, NULL);
16658# endif
16659 if (p != NULL)
16660 (void)strftime((char *)result_buf, sizeof(result_buf),
16661 (char *)p, curtime);
16662 else
16663 result_buf[0] = NUL;
16664
16665# ifdef FEAT_MBYTE
16666 if (conv.vc_type != CONV_NONE)
16667 vim_free(p);
16668 convert_setup(&conv, enc, p_enc);
16669 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016670 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671 else
16672# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016673 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674
16675# ifdef FEAT_MBYTE
16676 /* Release conversion descriptors */
16677 convert_setup(&conv, NULL, NULL);
16678 vim_free(enc);
16679# endif
16680 }
16681}
16682#endif
16683
16684/*
16685 * "stridx()" function
16686 */
16687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016688f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016689 typval_T *argvars;
16690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691{
16692 char_u buf[NUMBUFLEN];
16693 char_u *needle;
16694 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016695 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016697 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016699 needle = get_tv_string_chk(&argvars[1]);
16700 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016701 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016702 if (needle == NULL || haystack == NULL)
16703 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704
Bram Moolenaar33570922005-01-25 22:26:29 +000016705 if (argvars[2].v_type != VAR_UNKNOWN)
16706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016707 int error = FALSE;
16708
16709 start_idx = get_tv_number_chk(&argvars[2], &error);
16710 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016711 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016712 if (start_idx >= 0)
16713 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016714 }
16715
16716 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16717 if (pos != NULL)
16718 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719}
16720
16721/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016722 * "string()" function
16723 */
16724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016725f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016726 typval_T *argvars;
16727 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016728{
16729 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016730 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016732 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016733 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016734 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016735 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016736 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737}
16738
16739/*
16740 * "strlen()" function
16741 */
16742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016743f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016744 typval_T *argvars;
16745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016747 rettv->vval.v_number = (varnumber_T)(STRLEN(
16748 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749}
16750
16751/*
16752 * "strpart()" function
16753 */
16754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016755f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016756 typval_T *argvars;
16757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758{
16759 char_u *p;
16760 int n;
16761 int len;
16762 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016763 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016765 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 slen = (int)STRLEN(p);
16767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016768 n = get_tv_number_chk(&argvars[1], &error);
16769 if (error)
16770 len = 0;
16771 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016772 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 else
16774 len = slen - n; /* default len: all bytes that are available. */
16775
16776 /*
16777 * Only return the overlap between the specified part and the actual
16778 * string.
16779 */
16780 if (n < 0)
16781 {
16782 len += n;
16783 n = 0;
16784 }
16785 else if (n > slen)
16786 n = slen;
16787 if (len < 0)
16788 len = 0;
16789 else if (n + len > slen)
16790 len = slen - n;
16791
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016792 rettv->v_type = VAR_STRING;
16793 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794}
16795
16796/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016797 * "strridx()" function
16798 */
16799 static void
16800f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016801 typval_T *argvars;
16802 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016803{
16804 char_u buf[NUMBUFLEN];
16805 char_u *needle;
16806 char_u *haystack;
16807 char_u *rest;
16808 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016809 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016811 needle = get_tv_string_chk(&argvars[1]);
16812 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016813
16814 rettv->vval.v_number = -1;
16815 if (needle == NULL || haystack == NULL)
16816 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016817
16818 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016819 if (argvars[2].v_type != VAR_UNKNOWN)
16820 {
16821 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016822 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016823 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016824 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016825 }
16826 else
16827 end_idx = haystack_len;
16828
Bram Moolenaar0d660222005-01-07 21:51:51 +000016829 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016830 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016831 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016832 lastmatch = haystack + end_idx;
16833 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016834 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016835 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016836 for (rest = haystack; *rest != '\0'; ++rest)
16837 {
16838 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016839 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016840 break;
16841 lastmatch = rest;
16842 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016843 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016844
16845 if (lastmatch == NULL)
16846 rettv->vval.v_number = -1;
16847 else
16848 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16849}
16850
16851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 * "strtrans()" function
16853 */
16854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016855f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016856 typval_T *argvars;
16857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016859 rettv->v_type = VAR_STRING;
16860 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861}
16862
16863/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016864 * "submatch()" function
16865 */
16866 static void
16867f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016868 typval_T *argvars;
16869 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016870{
16871 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016872 rettv->vval.v_string =
16873 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874}
16875
16876/*
16877 * "substitute()" function
16878 */
16879 static void
16880f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016881 typval_T *argvars;
16882 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016883{
16884 char_u patbuf[NUMBUFLEN];
16885 char_u subbuf[NUMBUFLEN];
16886 char_u flagsbuf[NUMBUFLEN];
16887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016888 char_u *str = get_tv_string_chk(&argvars[0]);
16889 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16890 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16891 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16892
Bram Moolenaar0d660222005-01-07 21:51:51 +000016893 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016894 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16895 rettv->vval.v_string = NULL;
16896 else
16897 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016898}
16899
16900/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016901 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016904f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016905 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016906 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907{
16908 int id = 0;
16909#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016910 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911 long col;
16912 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016913 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016915 lnum = get_tv_lnum(argvars); /* -1 on type error */
16916 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16917 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016919 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016920 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016921 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922#endif
16923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016924 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925}
16926
16927/*
16928 * "synIDattr(id, what [, mode])" function
16929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016931f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016932 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934{
16935 char_u *p = NULL;
16936#ifdef FEAT_SYN_HL
16937 int id;
16938 char_u *what;
16939 char_u *mode;
16940 char_u modebuf[NUMBUFLEN];
16941 int modec;
16942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016943 id = get_tv_number(&argvars[0]);
16944 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016945 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016947 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 modec = TOLOWER_ASC(mode[0]);
16949 if (modec != 't' && modec != 'c'
16950#ifdef FEAT_GUI
16951 && modec != 'g'
16952#endif
16953 )
16954 modec = 0; /* replace invalid with current */
16955 }
16956 else
16957 {
16958#ifdef FEAT_GUI
16959 if (gui.in_use)
16960 modec = 'g';
16961 else
16962#endif
16963 if (t_colors > 1)
16964 modec = 'c';
16965 else
16966 modec = 't';
16967 }
16968
16969
16970 switch (TOLOWER_ASC(what[0]))
16971 {
16972 case 'b':
16973 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16974 p = highlight_color(id, what, modec);
16975 else /* bold */
16976 p = highlight_has_attr(id, HL_BOLD, modec);
16977 break;
16978
Bram Moolenaar12682fd2010-03-10 13:43:49 +010016979 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980 p = highlight_color(id, what, modec);
16981 break;
16982
16983 case 'i':
16984 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16985 p = highlight_has_attr(id, HL_INVERSE, modec);
16986 else /* italic */
16987 p = highlight_has_attr(id, HL_ITALIC, modec);
16988 break;
16989
16990 case 'n': /* name */
16991 p = get_highlight_name(NULL, id - 1);
16992 break;
16993
16994 case 'r': /* reverse */
16995 p = highlight_has_attr(id, HL_INVERSE, modec);
16996 break;
16997
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016998 case 's':
16999 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17000 p = highlight_color(id, what, modec);
17001 else /* standout */
17002 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 break;
17004
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017005 case 'u':
17006 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17007 /* underline */
17008 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17009 else
17010 /* undercurl */
17011 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012 break;
17013 }
17014
17015 if (p != NULL)
17016 p = vim_strsave(p);
17017#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017018 rettv->v_type = VAR_STRING;
17019 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020}
17021
17022/*
17023 * "synIDtrans(id)" function
17024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017026f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017027 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029{
17030 int id;
17031
17032#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017033 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034
17035 if (id > 0)
17036 id = syn_get_final_id(id);
17037 else
17038#endif
17039 id = 0;
17040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017041 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042}
17043
17044/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017045 * "synstack(lnum, col)" function
17046 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017047 static void
17048f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017049 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017050 typval_T *rettv;
17051{
17052#ifdef FEAT_SYN_HL
17053 long lnum;
17054 long col;
17055 int i;
17056 int id;
17057#endif
17058
17059 rettv->v_type = VAR_LIST;
17060 rettv->vval.v_list = NULL;
17061
17062#ifdef FEAT_SYN_HL
17063 lnum = get_tv_lnum(argvars); /* -1 on type error */
17064 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17065
17066 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017067 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017068 && rettv_list_alloc(rettv) != FAIL)
17069 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017070 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017071 for (i = 0; ; ++i)
17072 {
17073 id = syn_get_stack_item(i);
17074 if (id < 0)
17075 break;
17076 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17077 break;
17078 }
17079 }
17080#endif
17081}
17082
17083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 * "system()" function
17085 */
17086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017087f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017088 typval_T *argvars;
17089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017091 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017093 char_u *infile = NULL;
17094 char_u buf[NUMBUFLEN];
17095 int err = FALSE;
17096 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017098 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017099 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017100
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017101 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017102 {
17103 /*
17104 * Write the string to a temp file, to be used for input of the shell
17105 * command.
17106 */
17107 if ((infile = vim_tempname('i')) == NULL)
17108 {
17109 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017110 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017111 }
17112
17113 fd = mch_fopen((char *)infile, WRITEBIN);
17114 if (fd == NULL)
17115 {
17116 EMSG2(_(e_notopen), infile);
17117 goto done;
17118 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017119 p = get_tv_string_buf_chk(&argvars[1], buf);
17120 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017121 {
17122 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017123 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017124 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017125 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17126 err = TRUE;
17127 if (fclose(fd) != 0)
17128 err = TRUE;
17129 if (err)
17130 {
17131 EMSG(_("E677: Error writing temp file"));
17132 goto done;
17133 }
17134 }
17135
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017136 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17137 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017138
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139#ifdef USE_CR
17140 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017141 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 {
17143 char_u *s;
17144
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017145 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 {
17147 if (*s == CAR)
17148 *s = NL;
17149 }
17150 }
17151#else
17152# ifdef USE_CRNL
17153 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017154 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 {
17156 char_u *s, *d;
17157
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017158 d = res;
17159 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017160 {
17161 if (s[0] == CAR && s[1] == NL)
17162 ++s;
17163 *d++ = *s;
17164 }
17165 *d = NUL;
17166 }
17167# endif
17168#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017169
17170done:
17171 if (infile != NULL)
17172 {
17173 mch_remove(infile);
17174 vim_free(infile);
17175 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017176 rettv->v_type = VAR_STRING;
17177 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178}
17179
17180/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017181 * "tabpagebuflist()" function
17182 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017183 static void
17184f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017185 typval_T *argvars UNUSED;
17186 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017187{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017188#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017189 tabpage_T *tp;
17190 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017191
17192 if (argvars[0].v_type == VAR_UNKNOWN)
17193 wp = firstwin;
17194 else
17195 {
17196 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17197 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017198 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017199 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017200 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017201 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017202 for (; wp != NULL; wp = wp->w_next)
17203 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017204 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017205 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017206 }
17207#endif
17208}
17209
17210
17211/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017212 * "tabpagenr()" function
17213 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017214 static void
17215f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017216 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017217 typval_T *rettv;
17218{
17219 int nr = 1;
17220#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017221 char_u *arg;
17222
17223 if (argvars[0].v_type != VAR_UNKNOWN)
17224 {
17225 arg = get_tv_string_chk(&argvars[0]);
17226 nr = 0;
17227 if (arg != NULL)
17228 {
17229 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017230 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017231 else
17232 EMSG2(_(e_invexpr2), arg);
17233 }
17234 }
17235 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017236 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017237#endif
17238 rettv->vval.v_number = nr;
17239}
17240
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017241
17242#ifdef FEAT_WINDOWS
17243static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17244
17245/*
17246 * Common code for tabpagewinnr() and winnr().
17247 */
17248 static int
17249get_winnr(tp, argvar)
17250 tabpage_T *tp;
17251 typval_T *argvar;
17252{
17253 win_T *twin;
17254 int nr = 1;
17255 win_T *wp;
17256 char_u *arg;
17257
17258 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17259 if (argvar->v_type != VAR_UNKNOWN)
17260 {
17261 arg = get_tv_string_chk(argvar);
17262 if (arg == NULL)
17263 nr = 0; /* type error; errmsg already given */
17264 else if (STRCMP(arg, "$") == 0)
17265 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17266 else if (STRCMP(arg, "#") == 0)
17267 {
17268 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17269 if (twin == NULL)
17270 nr = 0;
17271 }
17272 else
17273 {
17274 EMSG2(_(e_invexpr2), arg);
17275 nr = 0;
17276 }
17277 }
17278
17279 if (nr > 0)
17280 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17281 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017282 {
17283 if (wp == NULL)
17284 {
17285 /* didn't find it in this tabpage */
17286 nr = 0;
17287 break;
17288 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017289 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017290 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017291 return nr;
17292}
17293#endif
17294
17295/*
17296 * "tabpagewinnr()" function
17297 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017298 static void
17299f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017300 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017301 typval_T *rettv;
17302{
17303 int nr = 1;
17304#ifdef FEAT_WINDOWS
17305 tabpage_T *tp;
17306
17307 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17308 if (tp == NULL)
17309 nr = 0;
17310 else
17311 nr = get_winnr(tp, &argvars[1]);
17312#endif
17313 rettv->vval.v_number = nr;
17314}
17315
17316
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017317/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017318 * "tagfiles()" function
17319 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017320 static void
17321f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017322 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017323 typval_T *rettv;
17324{
17325 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017326 tagname_T tn;
17327 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017328
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017329 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017330 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017331
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017332 for (first = TRUE; ; first = FALSE)
17333 if (get_tagfname(&tn, first, fname) == FAIL
17334 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017335 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017336 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017337}
17338
17339/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017340 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017341 */
17342 static void
17343f_taglist(argvars, rettv)
17344 typval_T *argvars;
17345 typval_T *rettv;
17346{
17347 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017348
17349 tag_pattern = get_tv_string(&argvars[0]);
17350
17351 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017352 if (*tag_pattern == NUL)
17353 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017354
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017355 if (rettv_list_alloc(rettv) == OK)
17356 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017357}
17358
17359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 * "tempname()" function
17361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017363f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017364 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366{
17367 static int x = 'A';
17368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017369 rettv->v_type = VAR_STRING;
17370 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371
17372 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17373 * names. Skip 'I' and 'O', they are used for shell redirection. */
17374 do
17375 {
17376 if (x == 'Z')
17377 x = '0';
17378 else if (x == '9')
17379 x = 'A';
17380 else
17381 {
17382#ifdef EBCDIC
17383 if (x == 'I')
17384 x = 'J';
17385 else if (x == 'R')
17386 x = 'S';
17387 else
17388#endif
17389 ++x;
17390 }
17391 } while (x == 'I' || x == 'O');
17392}
17393
17394/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017395 * "test(list)" function: Just checking the walls...
17396 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017397 static void
17398f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017399 typval_T *argvars UNUSED;
17400 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017401{
17402 /* Used for unit testing. Change the code below to your liking. */
17403#if 0
17404 listitem_T *li;
17405 list_T *l;
17406 char_u *bad, *good;
17407
17408 if (argvars[0].v_type != VAR_LIST)
17409 return;
17410 l = argvars[0].vval.v_list;
17411 if (l == NULL)
17412 return;
17413 li = l->lv_first;
17414 if (li == NULL)
17415 return;
17416 bad = get_tv_string(&li->li_tv);
17417 li = li->li_next;
17418 if (li == NULL)
17419 return;
17420 good = get_tv_string(&li->li_tv);
17421 rettv->vval.v_number = test_edit_score(bad, good);
17422#endif
17423}
17424
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017425#ifdef FEAT_FLOAT
17426/*
17427 * "tan()" function
17428 */
17429 static void
17430f_tan(argvars, rettv)
17431 typval_T *argvars;
17432 typval_T *rettv;
17433{
17434 float_T f;
17435
17436 rettv->v_type = VAR_FLOAT;
17437 if (get_float_arg(argvars, &f) == OK)
17438 rettv->vval.v_float = tan(f);
17439 else
17440 rettv->vval.v_float = 0.0;
17441}
17442
17443/*
17444 * "tanh()" function
17445 */
17446 static void
17447f_tanh(argvars, rettv)
17448 typval_T *argvars;
17449 typval_T *rettv;
17450{
17451 float_T f;
17452
17453 rettv->v_type = VAR_FLOAT;
17454 if (get_float_arg(argvars, &f) == OK)
17455 rettv->vval.v_float = tanh(f);
17456 else
17457 rettv->vval.v_float = 0.0;
17458}
17459#endif
17460
Bram Moolenaard52d9742005-08-21 22:20:28 +000017461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462 * "tolower(string)" function
17463 */
17464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017465f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017466 typval_T *argvars;
17467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468{
17469 char_u *p;
17470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017471 p = vim_strsave(get_tv_string(&argvars[0]));
17472 rettv->v_type = VAR_STRING;
17473 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474
17475 if (p != NULL)
17476 while (*p != NUL)
17477 {
17478#ifdef FEAT_MBYTE
17479 int l;
17480
17481 if (enc_utf8)
17482 {
17483 int c, lc;
17484
17485 c = utf_ptr2char(p);
17486 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017487 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 /* TODO: reallocate string when byte count changes. */
17489 if (utf_char2len(lc) == l)
17490 utf_char2bytes(lc, p);
17491 p += l;
17492 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017493 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 p += l; /* skip multi-byte character */
17495 else
17496#endif
17497 {
17498 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17499 ++p;
17500 }
17501 }
17502}
17503
17504/*
17505 * "toupper(string)" function
17506 */
17507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017508f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017509 typval_T *argvars;
17510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017512 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017513 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514}
17515
17516/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017517 * "tr(string, fromstr, tostr)" function
17518 */
17519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017520f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017521 typval_T *argvars;
17522 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017523{
17524 char_u *instr;
17525 char_u *fromstr;
17526 char_u *tostr;
17527 char_u *p;
17528#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017529 int inlen;
17530 int fromlen;
17531 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017532 int idx;
17533 char_u *cpstr;
17534 int cplen;
17535 int first = TRUE;
17536#endif
17537 char_u buf[NUMBUFLEN];
17538 char_u buf2[NUMBUFLEN];
17539 garray_T ga;
17540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017541 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017542 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17543 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017544
17545 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017546 rettv->v_type = VAR_STRING;
17547 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017548 if (fromstr == NULL || tostr == NULL)
17549 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017550 ga_init2(&ga, (int)sizeof(char), 80);
17551
17552#ifdef FEAT_MBYTE
17553 if (!has_mbyte)
17554#endif
17555 /* not multi-byte: fromstr and tostr must be the same length */
17556 if (STRLEN(fromstr) != STRLEN(tostr))
17557 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017558#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017559error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017560#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017561 EMSG2(_(e_invarg2), fromstr);
17562 ga_clear(&ga);
17563 return;
17564 }
17565
17566 /* fromstr and tostr have to contain the same number of chars */
17567 while (*instr != NUL)
17568 {
17569#ifdef FEAT_MBYTE
17570 if (has_mbyte)
17571 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017572 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017573 cpstr = instr;
17574 cplen = inlen;
17575 idx = 0;
17576 for (p = fromstr; *p != NUL; p += fromlen)
17577 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017578 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017579 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17580 {
17581 for (p = tostr; *p != NUL; p += tolen)
17582 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017583 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017584 if (idx-- == 0)
17585 {
17586 cplen = tolen;
17587 cpstr = p;
17588 break;
17589 }
17590 }
17591 if (*p == NUL) /* tostr is shorter than fromstr */
17592 goto error;
17593 break;
17594 }
17595 ++idx;
17596 }
17597
17598 if (first && cpstr == instr)
17599 {
17600 /* Check that fromstr and tostr have the same number of
17601 * (multi-byte) characters. Done only once when a character
17602 * of instr doesn't appear in fromstr. */
17603 first = FALSE;
17604 for (p = tostr; *p != NUL; p += tolen)
17605 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017606 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017607 --idx;
17608 }
17609 if (idx != 0)
17610 goto error;
17611 }
17612
17613 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017614 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017615 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017616
17617 instr += inlen;
17618 }
17619 else
17620#endif
17621 {
17622 /* When not using multi-byte chars we can do it faster. */
17623 p = vim_strchr(fromstr, *instr);
17624 if (p != NULL)
17625 ga_append(&ga, tostr[p - fromstr]);
17626 else
17627 ga_append(&ga, *instr);
17628 ++instr;
17629 }
17630 }
17631
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017632 /* add a terminating NUL */
17633 ga_grow(&ga, 1);
17634 ga_append(&ga, NUL);
17635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017636 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017637}
17638
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017639#ifdef FEAT_FLOAT
17640/*
17641 * "trunc({float})" function
17642 */
17643 static void
17644f_trunc(argvars, rettv)
17645 typval_T *argvars;
17646 typval_T *rettv;
17647{
17648 float_T f;
17649
17650 rettv->v_type = VAR_FLOAT;
17651 if (get_float_arg(argvars, &f) == OK)
17652 /* trunc() is not in C90, use floor() or ceil() instead. */
17653 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17654 else
17655 rettv->vval.v_float = 0.0;
17656}
17657#endif
17658
Bram Moolenaar8299df92004-07-10 09:47:34 +000017659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 * "type(expr)" function
17661 */
17662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017663f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017664 typval_T *argvars;
17665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017667 int n;
17668
17669 switch (argvars[0].v_type)
17670 {
17671 case VAR_NUMBER: n = 0; break;
17672 case VAR_STRING: n = 1; break;
17673 case VAR_FUNC: n = 2; break;
17674 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017675 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017676#ifdef FEAT_FLOAT
17677 case VAR_FLOAT: n = 5; break;
17678#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017679 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17680 }
17681 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682}
17683
17684/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017685 * "undofile(name)" function
17686 */
17687 static void
17688f_undofile(argvars, rettv)
17689 typval_T *argvars;
17690 typval_T *rettv;
17691{
17692 rettv->v_type = VAR_STRING;
17693#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017694 {
17695 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17696
17697 if (ffname != NULL)
17698 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17699 vim_free(ffname);
17700 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017701#else
17702 rettv->vval.v_string = NULL;
17703#endif
17704}
17705
17706/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017707 * "undotree()" function
17708 */
17709 static void
17710f_undotree(argvars, rettv)
17711 typval_T *argvars UNUSED;
17712 typval_T *rettv;
17713{
17714 if (rettv_dict_alloc(rettv) == OK)
17715 {
17716 dict_T *dict = rettv->vval.v_dict;
17717 list_T *list;
17718
Bram Moolenaar730cde92010-06-27 05:18:54 +020017719 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017720 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017721 dict_add_nr_str(dict, "save_last",
17722 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017723 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17724 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017725 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017726
17727 list = list_alloc();
17728 if (list != NULL)
17729 {
17730 u_eval_tree(curbuf->b_u_oldhead, list);
17731 dict_add_list(dict, "entries", list);
17732 }
17733 }
17734}
17735
17736/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017737 * "values(dict)" function
17738 */
17739 static void
17740f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017741 typval_T *argvars;
17742 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017743{
17744 dict_list(argvars, rettv, 1);
17745}
17746
17747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748 * "virtcol(string)" function
17749 */
17750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017751f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017752 typval_T *argvars;
17753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754{
17755 colnr_T vcol = 0;
17756 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017757 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017759 fp = var2fpos(&argvars[0], FALSE, &fnum);
17760 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17761 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 {
17763 getvvcol(curwin, fp, NULL, NULL, &vcol);
17764 ++vcol;
17765 }
17766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017767 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768}
17769
17770/*
17771 * "visualmode()" function
17772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017774f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017775 typval_T *argvars UNUSED;
17776 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777{
17778#ifdef FEAT_VISUAL
17779 char_u str[2];
17780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017781 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 str[0] = curbuf->b_visual_mode_eval;
17783 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017784 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785
17786 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017787 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789#endif
17790}
17791
17792/*
17793 * "winbufnr(nr)" function
17794 */
17795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017796f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017797 typval_T *argvars;
17798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799{
17800 win_T *wp;
17801
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017802 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017804 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017806 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807}
17808
17809/*
17810 * "wincol()" function
17811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017813f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017814 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816{
17817 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017818 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819}
17820
17821/*
17822 * "winheight(nr)" function
17823 */
17824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017825f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017826 typval_T *argvars;
17827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828{
17829 win_T *wp;
17830
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017831 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017833 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017835 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017836}
17837
17838/*
17839 * "winline()" function
17840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017842f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017843 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845{
17846 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017847 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848}
17849
17850/*
17851 * "winnr()" function
17852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017854f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017855 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857{
17858 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017859
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017861 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017863 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864}
17865
17866/*
17867 * "winrestcmd()" function
17868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017870f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017871 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873{
17874#ifdef FEAT_WINDOWS
17875 win_T *wp;
17876 int winnr = 1;
17877 garray_T ga;
17878 char_u buf[50];
17879
17880 ga_init2(&ga, (int)sizeof(char), 70);
17881 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17882 {
17883 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17884 ga_concat(&ga, buf);
17885# ifdef FEAT_VERTSPLIT
17886 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17887 ga_concat(&ga, buf);
17888# endif
17889 ++winnr;
17890 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017891 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017893 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017895 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017897 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898}
17899
17900/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017901 * "winrestview()" function
17902 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017903 static void
17904f_winrestview(argvars, rettv)
17905 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017906 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017907{
17908 dict_T *dict;
17909
17910 if (argvars[0].v_type != VAR_DICT
17911 || (dict = argvars[0].vval.v_dict) == NULL)
17912 EMSG(_(e_invarg));
17913 else
17914 {
17915 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17916 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17917#ifdef FEAT_VIRTUALEDIT
17918 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17919#endif
17920 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017921 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017922
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017923 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017924#ifdef FEAT_DIFF
17925 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17926#endif
17927 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17928 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17929
17930 check_cursor();
17931 changed_cline_bef_curs();
17932 invalidate_botline();
17933 redraw_later(VALID);
17934
17935 if (curwin->w_topline == 0)
17936 curwin->w_topline = 1;
17937 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17938 curwin->w_topline = curbuf->b_ml.ml_line_count;
17939#ifdef FEAT_DIFF
17940 check_topfill(curwin, TRUE);
17941#endif
17942 }
17943}
17944
17945/*
17946 * "winsaveview()" function
17947 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017948 static void
17949f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017950 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017951 typval_T *rettv;
17952{
17953 dict_T *dict;
17954
Bram Moolenaara800b422010-06-27 01:15:55 +020017955 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017956 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020017957 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017958
17959 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17960 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17961#ifdef FEAT_VIRTUALEDIT
17962 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17963#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017964 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017965 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17966
17967 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17968#ifdef FEAT_DIFF
17969 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17970#endif
17971 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17972 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17973}
17974
17975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 * "winwidth(nr)" function
17977 */
17978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017979f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017980 typval_T *argvars;
17981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982{
17983 win_T *wp;
17984
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017985 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017987 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988 else
17989#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017990 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017992 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993#endif
17994}
17995
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017997 * "writefile()" function
17998 */
17999 static void
18000f_writefile(argvars, rettv)
18001 typval_T *argvars;
18002 typval_T *rettv;
18003{
18004 int binary = FALSE;
18005 char_u *fname;
18006 FILE *fd;
18007 listitem_T *li;
18008 char_u *s;
18009 int ret = 0;
18010 int c;
18011
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018012 if (check_restricted() || check_secure())
18013 return;
18014
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018015 if (argvars[0].v_type != VAR_LIST)
18016 {
18017 EMSG2(_(e_listarg), "writefile()");
18018 return;
18019 }
18020 if (argvars[0].vval.v_list == NULL)
18021 return;
18022
18023 if (argvars[2].v_type != VAR_UNKNOWN
18024 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18025 binary = TRUE;
18026
18027 /* Always open the file in binary mode, library functions have a mind of
18028 * their own about CR-LF conversion. */
18029 fname = get_tv_string(&argvars[1]);
18030 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18031 {
18032 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18033 ret = -1;
18034 }
18035 else
18036 {
18037 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18038 li = li->li_next)
18039 {
18040 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18041 {
18042 if (*s == '\n')
18043 c = putc(NUL, fd);
18044 else
18045 c = putc(*s, fd);
18046 if (c == EOF)
18047 {
18048 ret = -1;
18049 break;
18050 }
18051 }
18052 if (!binary || li->li_next != NULL)
18053 if (putc('\n', fd) == EOF)
18054 {
18055 ret = -1;
18056 break;
18057 }
18058 if (ret < 0)
18059 {
18060 EMSG(_(e_write));
18061 break;
18062 }
18063 }
18064 fclose(fd);
18065 }
18066
18067 rettv->vval.v_number = ret;
18068}
18069
18070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018072 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 */
18074 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018075var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018076 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018077 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018078 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018080 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018082 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083
Bram Moolenaara5525202006-03-02 22:52:09 +000018084 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018085 if (varp->v_type == VAR_LIST)
18086 {
18087 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018088 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018089 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018090 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018091
18092 l = varp->vval.v_list;
18093 if (l == NULL)
18094 return NULL;
18095
18096 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018097 pos.lnum = list_find_nr(l, 0L, &error);
18098 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018099 return NULL; /* invalid line number */
18100
18101 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018102 pos.col = list_find_nr(l, 1L, &error);
18103 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018104 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018105 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018106
18107 /* We accept "$" for the column number: last column. */
18108 li = list_find(l, 1L);
18109 if (li != NULL && li->li_tv.v_type == VAR_STRING
18110 && li->li_tv.vval.v_string != NULL
18111 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18112 pos.col = len + 1;
18113
Bram Moolenaara5525202006-03-02 22:52:09 +000018114 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018115 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018116 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018117 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018118
Bram Moolenaara5525202006-03-02 22:52:09 +000018119#ifdef FEAT_VIRTUALEDIT
18120 /* Get the virtual offset. Defaults to zero. */
18121 pos.coladd = list_find_nr(l, 2L, &error);
18122 if (error)
18123 pos.coladd = 0;
18124#endif
18125
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018126 return &pos;
18127 }
18128
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018129 name = get_tv_string_chk(varp);
18130 if (name == NULL)
18131 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018132 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018134#ifdef FEAT_VISUAL
18135 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18136 {
18137 if (VIsual_active)
18138 return &VIsual;
18139 return &curwin->w_cursor;
18140 }
18141#endif
18142 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018144 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18146 return NULL;
18147 return pp;
18148 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018149
18150#ifdef FEAT_VIRTUALEDIT
18151 pos.coladd = 0;
18152#endif
18153
Bram Moolenaar477933c2007-07-17 14:32:23 +000018154 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018155 {
18156 pos.col = 0;
18157 if (name[1] == '0') /* "w0": first visible line */
18158 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018159 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018160 pos.lnum = curwin->w_topline;
18161 return &pos;
18162 }
18163 else if (name[1] == '$') /* "w$": last visible line */
18164 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018165 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018166 pos.lnum = curwin->w_botline - 1;
18167 return &pos;
18168 }
18169 }
18170 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018172 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 {
18174 pos.lnum = curbuf->b_ml.ml_line_count;
18175 pos.col = 0;
18176 }
18177 else
18178 {
18179 pos.lnum = curwin->w_cursor.lnum;
18180 pos.col = (colnr_T)STRLEN(ml_get_curline());
18181 }
18182 return &pos;
18183 }
18184 return NULL;
18185}
18186
18187/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018188 * Convert list in "arg" into a position and optional file number.
18189 * When "fnump" is NULL there is no file number, only 3 items.
18190 * Note that the column is passed on as-is, the caller may want to decrement
18191 * it to use 1 for the first column.
18192 * Return FAIL when conversion is not possible, doesn't check the position for
18193 * validity.
18194 */
18195 static int
18196list2fpos(arg, posp, fnump)
18197 typval_T *arg;
18198 pos_T *posp;
18199 int *fnump;
18200{
18201 list_T *l = arg->vval.v_list;
18202 long i = 0;
18203 long n;
18204
Bram Moolenaarbde35262006-07-23 20:12:24 +000018205 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18206 * when "fnump" isn't NULL and "coladd" is optional. */
18207 if (arg->v_type != VAR_LIST
18208 || l == NULL
18209 || l->lv_len < (fnump == NULL ? 2 : 3)
18210 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018211 return FAIL;
18212
18213 if (fnump != NULL)
18214 {
18215 n = list_find_nr(l, i++, NULL); /* fnum */
18216 if (n < 0)
18217 return FAIL;
18218 if (n == 0)
18219 n = curbuf->b_fnum; /* current buffer */
18220 *fnump = n;
18221 }
18222
18223 n = list_find_nr(l, i++, NULL); /* lnum */
18224 if (n < 0)
18225 return FAIL;
18226 posp->lnum = n;
18227
18228 n = list_find_nr(l, i++, NULL); /* col */
18229 if (n < 0)
18230 return FAIL;
18231 posp->col = n;
18232
18233#ifdef FEAT_VIRTUALEDIT
18234 n = list_find_nr(l, i, NULL);
18235 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018236 posp->coladd = 0;
18237 else
18238 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018239#endif
18240
18241 return OK;
18242}
18243
18244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245 * Get the length of an environment variable name.
18246 * Advance "arg" to the first character after the name.
18247 * Return 0 for error.
18248 */
18249 static int
18250get_env_len(arg)
18251 char_u **arg;
18252{
18253 char_u *p;
18254 int len;
18255
18256 for (p = *arg; vim_isIDc(*p); ++p)
18257 ;
18258 if (p == *arg) /* no name found */
18259 return 0;
18260
18261 len = (int)(p - *arg);
18262 *arg = p;
18263 return len;
18264}
18265
18266/*
18267 * Get the length of the name of a function or internal variable.
18268 * "arg" is advanced to the first non-white character after the name.
18269 * Return 0 if something is wrong.
18270 */
18271 static int
18272get_id_len(arg)
18273 char_u **arg;
18274{
18275 char_u *p;
18276 int len;
18277
18278 /* Find the end of the name. */
18279 for (p = *arg; eval_isnamec(*p); ++p)
18280 ;
18281 if (p == *arg) /* no name found */
18282 return 0;
18283
18284 len = (int)(p - *arg);
18285 *arg = skipwhite(p);
18286
18287 return len;
18288}
18289
18290/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018291 * Get the length of the name of a variable or function.
18292 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018294 * Return -1 if curly braces expansion failed.
18295 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 * If the name contains 'magic' {}'s, expand them and return the
18297 * expanded name in an allocated string via 'alias' - caller must free.
18298 */
18299 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018300get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 char_u **arg;
18302 char_u **alias;
18303 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018304 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305{
18306 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307 char_u *p;
18308 char_u *expr_start;
18309 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310
18311 *alias = NULL; /* default to no alias */
18312
18313 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18314 && (*arg)[2] == (int)KE_SNR)
18315 {
18316 /* hard coded <SNR>, already translated */
18317 *arg += 3;
18318 return get_id_len(arg) + 3;
18319 }
18320 len = eval_fname_script(*arg);
18321 if (len > 0)
18322 {
18323 /* literal "<SID>", "s:" or "<SNR>" */
18324 *arg += len;
18325 }
18326
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018328 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018330 p = find_name_end(*arg, &expr_start, &expr_end,
18331 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 if (expr_start != NULL)
18333 {
18334 char_u *temp_string;
18335
18336 if (!evaluate)
18337 {
18338 len += (int)(p - *arg);
18339 *arg = skipwhite(p);
18340 return len;
18341 }
18342
18343 /*
18344 * Include any <SID> etc in the expanded string:
18345 * Thus the -len here.
18346 */
18347 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18348 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018349 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 *alias = temp_string;
18351 *arg = skipwhite(p);
18352 return (int)STRLEN(temp_string);
18353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354
18355 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018356 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 EMSG2(_(e_invexpr2), *arg);
18358
18359 return len;
18360}
18361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018362/*
18363 * Find the end of a variable or function name, taking care of magic braces.
18364 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18365 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018366 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018367 * Return a pointer to just after the name. Equal to "arg" if there is no
18368 * valid name.
18369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018371find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372 char_u *arg;
18373 char_u **expr_start;
18374 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018375 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018377 int mb_nest = 0;
18378 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 char_u *p;
18380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018381 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018383 *expr_start = NULL;
18384 *expr_end = NULL;
18385 }
18386
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018387 /* Quick check for valid starting character. */
18388 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18389 return arg;
18390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018391 for (p = arg; *p != NUL
18392 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018393 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018394 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018395 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018396 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018397 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018398 if (*p == '\'')
18399 {
18400 /* skip over 'string' to avoid counting [ and ] inside it. */
18401 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18402 ;
18403 if (*p == NUL)
18404 break;
18405 }
18406 else if (*p == '"')
18407 {
18408 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18409 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18410 if (*p == '\\' && p[1] != NUL)
18411 ++p;
18412 if (*p == NUL)
18413 break;
18414 }
18415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018416 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018418 if (*p == '[')
18419 ++br_nest;
18420 else if (*p == ']')
18421 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018422 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018424 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018426 if (*p == '{')
18427 {
18428 mb_nest++;
18429 if (expr_start != NULL && *expr_start == NULL)
18430 *expr_start = p;
18431 }
18432 else if (*p == '}')
18433 {
18434 mb_nest--;
18435 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18436 *expr_end = p;
18437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439 }
18440
18441 return p;
18442}
18443
18444/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018445 * Expands out the 'magic' {}'s in a variable/function name.
18446 * Note that this can call itself recursively, to deal with
18447 * constructs like foo{bar}{baz}{bam}
18448 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18449 * "in_start" ^
18450 * "expr_start" ^
18451 * "expr_end" ^
18452 * "in_end" ^
18453 *
18454 * Returns a new allocated string, which the caller must free.
18455 * Returns NULL for failure.
18456 */
18457 static char_u *
18458make_expanded_name(in_start, expr_start, expr_end, in_end)
18459 char_u *in_start;
18460 char_u *expr_start;
18461 char_u *expr_end;
18462 char_u *in_end;
18463{
18464 char_u c1;
18465 char_u *retval = NULL;
18466 char_u *temp_result;
18467 char_u *nextcmd = NULL;
18468
18469 if (expr_end == NULL || in_end == NULL)
18470 return NULL;
18471 *expr_start = NUL;
18472 *expr_end = NUL;
18473 c1 = *in_end;
18474 *in_end = NUL;
18475
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018476 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018477 if (temp_result != NULL && nextcmd == NULL)
18478 {
18479 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18480 + (in_end - expr_end) + 1));
18481 if (retval != NULL)
18482 {
18483 STRCPY(retval, in_start);
18484 STRCAT(retval, temp_result);
18485 STRCAT(retval, expr_end + 1);
18486 }
18487 }
18488 vim_free(temp_result);
18489
18490 *in_end = c1; /* put char back for error messages */
18491 *expr_start = '{';
18492 *expr_end = '}';
18493
18494 if (retval != NULL)
18495 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018496 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018497 if (expr_start != NULL)
18498 {
18499 /* Further expansion! */
18500 temp_result = make_expanded_name(retval, expr_start,
18501 expr_end, temp_result);
18502 vim_free(retval);
18503 retval = temp_result;
18504 }
18505 }
18506
18507 return retval;
18508}
18509
18510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018512 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513 */
18514 static int
18515eval_isnamec(c)
18516 int c;
18517{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018518 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18519}
18520
18521/*
18522 * Return TRUE if character "c" can be used as the first character in a
18523 * variable or function name (excluding '{' and '}').
18524 */
18525 static int
18526eval_isnamec1(c)
18527 int c;
18528{
18529 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530}
18531
18532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533 * Set number v: variable to "val".
18534 */
18535 void
18536set_vim_var_nr(idx, val)
18537 int idx;
18538 long val;
18539{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018540 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541}
18542
18543/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018544 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 */
18546 long
18547get_vim_var_nr(idx)
18548 int idx;
18549{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018550 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551}
18552
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018553/*
18554 * Get string v: variable value. Uses a static buffer, can only be used once.
18555 */
18556 char_u *
18557get_vim_var_str(idx)
18558 int idx;
18559{
18560 return get_tv_string(&vimvars[idx].vv_tv);
18561}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018562
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018564 * Get List v: variable value. Caller must take care of reference count when
18565 * needed.
18566 */
18567 list_T *
18568get_vim_var_list(idx)
18569 int idx;
18570{
18571 return vimvars[idx].vv_list;
18572}
18573
18574/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018575 * Set v:char to character "c".
18576 */
18577 void
18578set_vim_var_char(c)
18579 int c;
18580{
18581#ifdef FEAT_MBYTE
18582 char_u buf[MB_MAXBYTES];
18583#else
18584 char_u buf[2];
18585#endif
18586
18587#ifdef FEAT_MBYTE
18588 if (has_mbyte)
18589 buf[(*mb_char2bytes)(c, buf)] = NUL;
18590 else
18591#endif
18592 {
18593 buf[0] = c;
18594 buf[1] = NUL;
18595 }
18596 set_vim_var_string(VV_CHAR, buf, -1);
18597}
18598
18599/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018600 * Set v:count to "count" and v:count1 to "count1".
18601 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 */
18603 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018604set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 long count;
18606 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018607 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018609 if (set_prevcount)
18610 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018611 vimvars[VV_COUNT].vv_nr = count;
18612 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613}
18614
18615/*
18616 * Set string v: variable to a copy of "val".
18617 */
18618 void
18619set_vim_var_string(idx, val, len)
18620 int idx;
18621 char_u *val;
18622 int len; /* length of "val" to use or -1 (whole string) */
18623{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018624 /* Need to do this (at least) once, since we can't initialize a union.
18625 * Will always be invoked when "v:progname" is set. */
18626 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18627
Bram Moolenaare9a41262005-01-15 22:18:47 +000018628 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018630 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018632 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018634 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635}
18636
18637/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018638 * Set List v: variable to "val".
18639 */
18640 void
18641set_vim_var_list(idx, val)
18642 int idx;
18643 list_T *val;
18644{
18645 list_unref(vimvars[idx].vv_list);
18646 vimvars[idx].vv_list = val;
18647 if (val != NULL)
18648 ++val->lv_refcount;
18649}
18650
18651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 * Set v:register if needed.
18653 */
18654 void
18655set_reg_var(c)
18656 int c;
18657{
18658 char_u regname;
18659
18660 if (c == 0 || c == ' ')
18661 regname = '"';
18662 else
18663 regname = c;
18664 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018665 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 set_vim_var_string(VV_REG, &regname, 1);
18667}
18668
18669/*
18670 * Get or set v:exception. If "oldval" == NULL, return the current value.
18671 * Otherwise, restore the value to "oldval" and return NULL.
18672 * Must always be called in pairs to save and restore v:exception! Does not
18673 * take care of memory allocations.
18674 */
18675 char_u *
18676v_exception(oldval)
18677 char_u *oldval;
18678{
18679 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018680 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681
Bram Moolenaare9a41262005-01-15 22:18:47 +000018682 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683 return NULL;
18684}
18685
18686/*
18687 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18688 * Otherwise, restore the value to "oldval" and return NULL.
18689 * Must always be called in pairs to save and restore v:throwpoint! Does not
18690 * take care of memory allocations.
18691 */
18692 char_u *
18693v_throwpoint(oldval)
18694 char_u *oldval;
18695{
18696 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018697 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698
Bram Moolenaare9a41262005-01-15 22:18:47 +000018699 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700 return NULL;
18701}
18702
18703#if defined(FEAT_AUTOCMD) || defined(PROTO)
18704/*
18705 * Set v:cmdarg.
18706 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18707 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18708 * Must always be called in pairs!
18709 */
18710 char_u *
18711set_cmdarg(eap, oldarg)
18712 exarg_T *eap;
18713 char_u *oldarg;
18714{
18715 char_u *oldval;
18716 char_u *newval;
18717 unsigned len;
18718
Bram Moolenaare9a41262005-01-15 22:18:47 +000018719 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018720 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018722 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018723 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018724 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 }
18726
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018727 if (eap->force_bin == FORCE_BIN)
18728 len = 6;
18729 else if (eap->force_bin == FORCE_NOBIN)
18730 len = 8;
18731 else
18732 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018733
18734 if (eap->read_edit)
18735 len += 7;
18736
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018737 if (eap->force_ff != 0)
18738 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18739# ifdef FEAT_MBYTE
18740 if (eap->force_enc != 0)
18741 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018742 if (eap->bad_char != 0)
18743 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018744# endif
18745
18746 newval = alloc(len + 1);
18747 if (newval == NULL)
18748 return NULL;
18749
18750 if (eap->force_bin == FORCE_BIN)
18751 sprintf((char *)newval, " ++bin");
18752 else if (eap->force_bin == FORCE_NOBIN)
18753 sprintf((char *)newval, " ++nobin");
18754 else
18755 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018756
18757 if (eap->read_edit)
18758 STRCAT(newval, " ++edit");
18759
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018760 if (eap->force_ff != 0)
18761 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18762 eap->cmd + eap->force_ff);
18763# ifdef FEAT_MBYTE
18764 if (eap->force_enc != 0)
18765 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18766 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018767 if (eap->bad_char == BAD_KEEP)
18768 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18769 else if (eap->bad_char == BAD_DROP)
18770 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18771 else if (eap->bad_char != 0)
18772 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018773# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018774 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018775 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776}
18777#endif
18778
18779/*
18780 * Get the value of internal variable "name".
18781 * Return OK or FAIL.
18782 */
18783 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018784get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 char_u *name;
18786 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018787 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018788 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789{
18790 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 typval_T *tv = NULL;
18792 typval_T atv;
18793 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795
18796 /* truncate the name, so that we can use strcmp() */
18797 cc = name[len];
18798 name[len] = NUL;
18799
18800 /*
18801 * Check for "b:changedtick".
18802 */
18803 if (STRCMP(name, "b:changedtick") == 0)
18804 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018805 atv.v_type = VAR_NUMBER;
18806 atv.vval.v_number = curbuf->b_changedtick;
18807 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808 }
18809
18810 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 * Check for user-defined variables.
18812 */
18813 else
18814 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018815 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018817 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 }
18819
Bram Moolenaare9a41262005-01-15 22:18:47 +000018820 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018822 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018823 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 ret = FAIL;
18825 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018826 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018827 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828
18829 name[len] = cc;
18830
18831 return ret;
18832}
18833
18834/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018835 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18836 * Also handle function call with Funcref variable: func(expr)
18837 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18838 */
18839 static int
18840handle_subscript(arg, rettv, evaluate, verbose)
18841 char_u **arg;
18842 typval_T *rettv;
18843 int evaluate; /* do more than finding the end */
18844 int verbose; /* give error messages */
18845{
18846 int ret = OK;
18847 dict_T *selfdict = NULL;
18848 char_u *s;
18849 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018850 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018851
18852 while (ret == OK
18853 && (**arg == '['
18854 || (**arg == '.' && rettv->v_type == VAR_DICT)
18855 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18856 && !vim_iswhite(*(*arg - 1)))
18857 {
18858 if (**arg == '(')
18859 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018860 /* need to copy the funcref so that we can clear rettv */
18861 functv = *rettv;
18862 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018863
18864 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018865 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018866 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018867 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18868 &len, evaluate, selfdict);
18869
18870 /* Clear the funcref afterwards, so that deleting it while
18871 * evaluating the arguments is possible (see test55). */
18872 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018873
18874 /* Stop the expression evaluation when immediately aborting on
18875 * error, or when an interrupt occurred or an exception was thrown
18876 * but not caught. */
18877 if (aborting())
18878 {
18879 if (ret == OK)
18880 clear_tv(rettv);
18881 ret = FAIL;
18882 }
18883 dict_unref(selfdict);
18884 selfdict = NULL;
18885 }
18886 else /* **arg == '[' || **arg == '.' */
18887 {
18888 dict_unref(selfdict);
18889 if (rettv->v_type == VAR_DICT)
18890 {
18891 selfdict = rettv->vval.v_dict;
18892 if (selfdict != NULL)
18893 ++selfdict->dv_refcount;
18894 }
18895 else
18896 selfdict = NULL;
18897 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18898 {
18899 clear_tv(rettv);
18900 ret = FAIL;
18901 }
18902 }
18903 }
18904 dict_unref(selfdict);
18905 return ret;
18906}
18907
18908/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018909 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018910 * value).
18911 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018912 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018913alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018914{
Bram Moolenaar33570922005-01-25 22:26:29 +000018915 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018916}
18917
18918/*
18919 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 * The string "s" must have been allocated, it is consumed.
18921 * Return NULL for out of memory, the variable otherwise.
18922 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018923 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018924alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925 char_u *s;
18926{
Bram Moolenaar33570922005-01-25 22:26:29 +000018927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018929 rettv = alloc_tv();
18930 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018932 rettv->v_type = VAR_STRING;
18933 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934 }
18935 else
18936 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018937 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938}
18939
18940/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018941 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018943 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018944free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018945 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946{
18947 if (varp != NULL)
18948 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018949 switch (varp->v_type)
18950 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018951 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018952 func_unref(varp->vval.v_string);
18953 /*FALLTHROUGH*/
18954 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018955 vim_free(varp->vval.v_string);
18956 break;
18957 case VAR_LIST:
18958 list_unref(varp->vval.v_list);
18959 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018960 case VAR_DICT:
18961 dict_unref(varp->vval.v_dict);
18962 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018963 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018964#ifdef FEAT_FLOAT
18965 case VAR_FLOAT:
18966#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018967 case VAR_UNKNOWN:
18968 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018969 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018970 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018971 break;
18972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 vim_free(varp);
18974 }
18975}
18976
18977/*
18978 * Free the memory for a variable value and set the value to NULL or 0.
18979 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018980 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018981clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018982 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983{
18984 if (varp != NULL)
18985 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018986 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018988 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018989 func_unref(varp->vval.v_string);
18990 /*FALLTHROUGH*/
18991 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018992 vim_free(varp->vval.v_string);
18993 varp->vval.v_string = NULL;
18994 break;
18995 case VAR_LIST:
18996 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018997 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018998 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018999 case VAR_DICT:
19000 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019001 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019002 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019003 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019004 varp->vval.v_number = 0;
19005 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019006#ifdef FEAT_FLOAT
19007 case VAR_FLOAT:
19008 varp->vval.v_float = 0.0;
19009 break;
19010#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019011 case VAR_UNKNOWN:
19012 break;
19013 default:
19014 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019016 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017 }
19018}
19019
19020/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019021 * Set the value of a variable to NULL without freeing items.
19022 */
19023 static void
19024init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019025 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019026{
19027 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019028 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019029}
19030
19031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 * Get the number value of a variable.
19033 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019034 * For incompatible types, return 0.
19035 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19036 * caller of incompatible types: it sets *denote to TRUE if "denote"
19037 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038 */
19039 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019040get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019041 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019043 int error = FALSE;
19044
19045 return get_tv_number_chk(varp, &error); /* return 0L on error */
19046}
19047
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019048 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019049get_tv_number_chk(varp, denote)
19050 typval_T *varp;
19051 int *denote;
19052{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019053 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019055 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019057 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019058 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019059#ifdef FEAT_FLOAT
19060 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019061 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019062 break;
19063#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019064 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019065 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019066 break;
19067 case VAR_STRING:
19068 if (varp->vval.v_string != NULL)
19069 vim_str2nr(varp->vval.v_string, NULL, NULL,
19070 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019071 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019072 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019073 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019074 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019075 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019076 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019077 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019078 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019079 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019082 if (denote == NULL) /* useful for values that must be unsigned */
19083 n = -1;
19084 else
19085 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019086 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087}
19088
19089/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019090 * Get the lnum from the first argument.
19091 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019092 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 */
19094 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019095get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019096 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097{
Bram Moolenaar33570922005-01-25 22:26:29 +000019098 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099 linenr_T lnum;
19100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019101 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 if (lnum == 0) /* no valid number, try using line() */
19103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019104 rettv.v_type = VAR_NUMBER;
19105 f_line(argvars, &rettv);
19106 lnum = rettv.vval.v_number;
19107 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108 }
19109 return lnum;
19110}
19111
19112/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019113 * Get the lnum from the first argument.
19114 * Also accepts "$", then "buf" is used.
19115 * Returns 0 on error.
19116 */
19117 static linenr_T
19118get_tv_lnum_buf(argvars, buf)
19119 typval_T *argvars;
19120 buf_T *buf;
19121{
19122 if (argvars[0].v_type == VAR_STRING
19123 && argvars[0].vval.v_string != NULL
19124 && argvars[0].vval.v_string[0] == '$'
19125 && buf != NULL)
19126 return buf->b_ml.ml_line_count;
19127 return get_tv_number_chk(&argvars[0], NULL);
19128}
19129
19130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 * Get the string value of a variable.
19132 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019133 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19134 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 * If the String variable has never been set, return an empty string.
19136 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019137 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19138 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139 */
19140 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019141get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019142 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143{
19144 static char_u mybuf[NUMBUFLEN];
19145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019146 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147}
19148
19149 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019150get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019151 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019152 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019154 char_u *res = get_tv_string_buf_chk(varp, buf);
19155
19156 return res != NULL ? res : (char_u *)"";
19157}
19158
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019159 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019160get_tv_string_chk(varp)
19161 typval_T *varp;
19162{
19163 static char_u mybuf[NUMBUFLEN];
19164
19165 return get_tv_string_buf_chk(varp, mybuf);
19166}
19167
19168 static char_u *
19169get_tv_string_buf_chk(varp, buf)
19170 typval_T *varp;
19171 char_u *buf;
19172{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019173 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019175 case VAR_NUMBER:
19176 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19177 return buf;
19178 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019179 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019180 break;
19181 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019182 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019183 break;
19184 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019185 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019186 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019187#ifdef FEAT_FLOAT
19188 case VAR_FLOAT:
19189 EMSG(_("E806: using Float as a String"));
19190 break;
19191#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019192 case VAR_STRING:
19193 if (varp->vval.v_string != NULL)
19194 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019195 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019196 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019197 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019198 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019200 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201}
19202
19203/*
19204 * Find variable "name" in the list of variables.
19205 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019206 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019207 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019208 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019210 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019211find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019213 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019216 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217
Bram Moolenaara7043832005-01-21 11:56:39 +000019218 ht = find_var_ht(name, &varname);
19219 if (htp != NULL)
19220 *htp = ht;
19221 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019222 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019223 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224}
19225
19226/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019227 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019228 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019230 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019231find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019232 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019233 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019234 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019235{
Bram Moolenaar33570922005-01-25 22:26:29 +000019236 hashitem_T *hi;
19237
19238 if (*varname == NUL)
19239 {
19240 /* Must be something like "s:", otherwise "ht" would be NULL. */
19241 switch (varname[-2])
19242 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019243 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019244 case 'g': return &globvars_var;
19245 case 'v': return &vimvars_var;
19246 case 'b': return &curbuf->b_bufvar;
19247 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019248#ifdef FEAT_WINDOWS
19249 case 't': return &curtab->tp_winvar;
19250#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019251 case 'l': return current_funccal == NULL
19252 ? NULL : &current_funccal->l_vars_var;
19253 case 'a': return current_funccal == NULL
19254 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019255 }
19256 return NULL;
19257 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019258
19259 hi = hash_find(ht, varname);
19260 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019261 {
19262 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019263 * worked find the variable again. Don't auto-load a script if it was
19264 * loaded already, otherwise it would be loaded every time when
19265 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019266 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019267 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019268 hi = hash_find(ht, varname);
19269 if (HASHITEM_EMPTY(hi))
19270 return NULL;
19271 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019272 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019273}
19274
19275/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019276 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019277 * Set "varname" to the start of name without ':'.
19278 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019279 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019280find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281 char_u *name;
19282 char_u **varname;
19283{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019284 hashitem_T *hi;
19285
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286 if (name[1] != ':')
19287 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019288 /* The name must not start with a colon or #. */
19289 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290 return NULL;
19291 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019292
19293 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019294 hi = hash_find(&compat_hashtab, name);
19295 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019296 return &compat_hashtab;
19297
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019299 return &globvarht; /* global variable */
19300 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 }
19302 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019303 if (*name == 'g') /* global variable */
19304 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019305 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19306 */
19307 if (vim_strchr(name + 2, ':') != NULL
19308 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019309 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019311 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019313 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019314#ifdef FEAT_WINDOWS
19315 if (*name == 't') /* tab page variable */
19316 return &curtab->tp_vars.dv_hashtab;
19317#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019318 if (*name == 'v') /* v: variable */
19319 return &vimvarht;
19320 if (*name == 'a' && current_funccal != NULL) /* function argument */
19321 return &current_funccal->l_avars.dv_hashtab;
19322 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19323 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 if (*name == 's' /* script variable */
19325 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19326 return &SCRIPT_VARS(current_SID);
19327 return NULL;
19328}
19329
19330/*
19331 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019332 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333 * Returns NULL when it doesn't exist.
19334 */
19335 char_u *
19336get_var_value(name)
19337 char_u *name;
19338{
Bram Moolenaar33570922005-01-25 22:26:29 +000019339 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340
Bram Moolenaara7043832005-01-21 11:56:39 +000019341 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 if (v == NULL)
19343 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019344 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345}
19346
19347/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019348 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349 * sourcing this script and when executing functions defined in the script.
19350 */
19351 void
19352new_script_vars(id)
19353 scid_T id;
19354{
Bram Moolenaara7043832005-01-21 11:56:39 +000019355 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019356 hashtab_T *ht;
19357 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019358
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19360 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019361 /* Re-allocating ga_data means that an ht_array pointing to
19362 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019363 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019364 for (i = 1; i <= ga_scripts.ga_len; ++i)
19365 {
19366 ht = &SCRIPT_VARS(i);
19367 if (ht->ht_mask == HT_INIT_SIZE - 1)
19368 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019369 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019370 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019371 }
19372
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373 while (ga_scripts.ga_len < id)
19374 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019375 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019376 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019377 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 }
19380 }
19381}
19382
19383/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019384 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19385 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386 */
19387 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019388init_var_dict(dict, dict_var)
19389 dict_T *dict;
19390 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391{
Bram Moolenaar33570922005-01-25 22:26:29 +000019392 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019393 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019394 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019395 dict_var->di_tv.vval.v_dict = dict;
19396 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019397 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019398 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19399 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400}
19401
19402/*
19403 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019404 * Frees all allocated variables and the value they contain.
19405 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 */
19407 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019408vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019409 hashtab_T *ht;
19410{
19411 vars_clear_ext(ht, TRUE);
19412}
19413
19414/*
19415 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19416 */
19417 static void
19418vars_clear_ext(ht, free_val)
19419 hashtab_T *ht;
19420 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421{
Bram Moolenaara7043832005-01-21 11:56:39 +000019422 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019423 hashitem_T *hi;
19424 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425
Bram Moolenaar33570922005-01-25 22:26:29 +000019426 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019427 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019428 for (hi = ht->ht_array; todo > 0; ++hi)
19429 {
19430 if (!HASHITEM_EMPTY(hi))
19431 {
19432 --todo;
19433
Bram Moolenaar33570922005-01-25 22:26:29 +000019434 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019435 * ht_array might change then. hash_clear() takes care of it
19436 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019437 v = HI2DI(hi);
19438 if (free_val)
19439 clear_tv(&v->di_tv);
19440 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19441 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019442 }
19443 }
19444 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019445 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446}
19447
Bram Moolenaara7043832005-01-21 11:56:39 +000019448/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019449 * Delete a variable from hashtab "ht" at item "hi".
19450 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019453delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019454 hashtab_T *ht;
19455 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456{
Bram Moolenaar33570922005-01-25 22:26:29 +000019457 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019458
19459 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019460 clear_tv(&di->di_tv);
19461 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462}
19463
19464/*
19465 * List the value of one internal variable.
19466 */
19467 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019468list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019469 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019471 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019473 char_u *tofree;
19474 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019475 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019476
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019477 current_copyID += COPYID_INC;
19478 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019479 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019480 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019481 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482}
19483
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019485list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486 char_u *prefix;
19487 char_u *name;
19488 int type;
19489 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019490 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491{
Bram Moolenaar31859182007-08-14 20:41:13 +000019492 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19493 msg_start();
19494 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495 if (name != NULL) /* "a:" vars don't have a name stored */
19496 msg_puts(name);
19497 msg_putchar(' ');
19498 msg_advance(22);
19499 if (type == VAR_NUMBER)
19500 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019501 else if (type == VAR_FUNC)
19502 msg_putchar('*');
19503 else if (type == VAR_LIST)
19504 {
19505 msg_putchar('[');
19506 if (*string == '[')
19507 ++string;
19508 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019509 else if (type == VAR_DICT)
19510 {
19511 msg_putchar('{');
19512 if (*string == '{')
19513 ++string;
19514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 else
19516 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019517
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019519
19520 if (type == VAR_FUNC)
19521 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019522 if (*first)
19523 {
19524 msg_clr_eos();
19525 *first = FALSE;
19526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527}
19528
19529/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019530 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531 * If the variable already exists, the value is updated.
19532 * Otherwise the variable is created.
19533 */
19534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019535set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019537 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019538 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539{
Bram Moolenaar33570922005-01-25 22:26:29 +000019540 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019542 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019543 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019545 ht = find_var_ht(name, &varname);
19546 if (ht == NULL || *varname == NUL)
19547 {
19548 EMSG2(_(e_illvar), name);
19549 return;
19550 }
19551 v = find_var_in_ht(ht, varname, TRUE);
19552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019553 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019554 {
19555 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19556 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19557 ? name[2] : name[0]))
19558 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019559 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019560 return;
19561 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019562 /* Don't allow hiding a function. When "v" is not NULL we migth be
19563 * assigning another function to the same var, the type is checked
19564 * below. */
19565 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019566 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019567 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019568 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019569 return;
19570 }
19571 }
19572
Bram Moolenaar33570922005-01-25 22:26:29 +000019573 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019575 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019576 if (var_check_ro(v->di_flags, name)
19577 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019578 return;
19579 if (v->di_tv.v_type != tv->v_type
19580 && !((v->di_tv.v_type == VAR_STRING
19581 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019582 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019583 || tv->v_type == VAR_NUMBER))
19584#ifdef FEAT_FLOAT
19585 && !((v->di_tv.v_type == VAR_NUMBER
19586 || v->di_tv.v_type == VAR_FLOAT)
19587 && (tv->v_type == VAR_NUMBER
19588 || tv->v_type == VAR_FLOAT))
19589#endif
19590 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019591 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019592 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019593 return;
19594 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019595
19596 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019597 * Handle setting internal v: variables separately: we don't change
19598 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019599 */
19600 if (ht == &vimvarht)
19601 {
19602 if (v->di_tv.v_type == VAR_STRING)
19603 {
19604 vim_free(v->di_tv.vval.v_string);
19605 if (copy || tv->v_type != VAR_STRING)
19606 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19607 else
19608 {
19609 /* Take over the string to avoid an extra alloc/free. */
19610 v->di_tv.vval.v_string = tv->vval.v_string;
19611 tv->vval.v_string = NULL;
19612 }
19613 }
19614 else if (v->di_tv.v_type != VAR_NUMBER)
19615 EMSG2(_(e_intern2), "set_var()");
19616 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019617 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019618 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019619 if (STRCMP(varname, "searchforward") == 0)
19620 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19621 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019622 return;
19623 }
19624
19625 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 }
19627 else /* add a new variable */
19628 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019629 /* Can't add "v:" variable. */
19630 if (ht == &vimvarht)
19631 {
19632 EMSG2(_(e_illvar), name);
19633 return;
19634 }
19635
Bram Moolenaar92124a32005-06-17 22:03:40 +000019636 /* Make sure the variable name is valid. */
19637 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019638 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19639 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019640 {
19641 EMSG2(_(e_illvar), varname);
19642 return;
19643 }
19644
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019645 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19646 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019647 if (v == NULL)
19648 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019649 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019650 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019652 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 return;
19654 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019655 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019657
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019658 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019659 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019660 else
19661 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019662 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019663 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019664 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666}
19667
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019668/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019669 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 * Also give an error message.
19671 */
19672 static int
19673var_check_ro(flags, name)
19674 int flags;
19675 char_u *name;
19676{
19677 if (flags & DI_FLAGS_RO)
19678 {
19679 EMSG2(_(e_readonlyvar), name);
19680 return TRUE;
19681 }
19682 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19683 {
19684 EMSG2(_(e_readonlysbx), name);
19685 return TRUE;
19686 }
19687 return FALSE;
19688}
19689
19690/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019691 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19692 * Also give an error message.
19693 */
19694 static int
19695var_check_fixed(flags, name)
19696 int flags;
19697 char_u *name;
19698{
19699 if (flags & DI_FLAGS_FIX)
19700 {
19701 EMSG2(_("E795: Cannot delete variable %s"), name);
19702 return TRUE;
19703 }
19704 return FALSE;
19705}
19706
19707/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019708 * Return TRUE if typeval "tv" is set to be locked (immutable).
19709 * Also give an error message, using "name".
19710 */
19711 static int
19712tv_check_lock(lock, name)
19713 int lock;
19714 char_u *name;
19715{
19716 if (lock & VAR_LOCKED)
19717 {
19718 EMSG2(_("E741: Value is locked: %s"),
19719 name == NULL ? (char_u *)_("Unknown") : name);
19720 return TRUE;
19721 }
19722 if (lock & VAR_FIXED)
19723 {
19724 EMSG2(_("E742: Cannot change value of %s"),
19725 name == NULL ? (char_u *)_("Unknown") : name);
19726 return TRUE;
19727 }
19728 return FALSE;
19729}
19730
19731/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019732 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019733 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019734 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019735 * It is OK for "from" and "to" to point to the same item. This is used to
19736 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019737 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019738 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019739copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019740 typval_T *from;
19741 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019743 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019744 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019745 switch (from->v_type)
19746 {
19747 case VAR_NUMBER:
19748 to->vval.v_number = from->vval.v_number;
19749 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019750#ifdef FEAT_FLOAT
19751 case VAR_FLOAT:
19752 to->vval.v_float = from->vval.v_float;
19753 break;
19754#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019755 case VAR_STRING:
19756 case VAR_FUNC:
19757 if (from->vval.v_string == NULL)
19758 to->vval.v_string = NULL;
19759 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019760 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019761 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019762 if (from->v_type == VAR_FUNC)
19763 func_ref(to->vval.v_string);
19764 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019765 break;
19766 case VAR_LIST:
19767 if (from->vval.v_list == NULL)
19768 to->vval.v_list = NULL;
19769 else
19770 {
19771 to->vval.v_list = from->vval.v_list;
19772 ++to->vval.v_list->lv_refcount;
19773 }
19774 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019775 case VAR_DICT:
19776 if (from->vval.v_dict == NULL)
19777 to->vval.v_dict = NULL;
19778 else
19779 {
19780 to->vval.v_dict = from->vval.v_dict;
19781 ++to->vval.v_dict->dv_refcount;
19782 }
19783 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019784 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019785 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019786 break;
19787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788}
19789
19790/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019791 * Make a copy of an item.
19792 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019793 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19794 * reference to an already copied list/dict can be used.
19795 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019796 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019797 static int
19798item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019799 typval_T *from;
19800 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019801 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019802 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019803{
19804 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019805 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019806
Bram Moolenaar33570922005-01-25 22:26:29 +000019807 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019808 {
19809 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019810 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019811 }
19812 ++recurse;
19813
19814 switch (from->v_type)
19815 {
19816 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019817#ifdef FEAT_FLOAT
19818 case VAR_FLOAT:
19819#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019820 case VAR_STRING:
19821 case VAR_FUNC:
19822 copy_tv(from, to);
19823 break;
19824 case VAR_LIST:
19825 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019826 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019827 if (from->vval.v_list == NULL)
19828 to->vval.v_list = NULL;
19829 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19830 {
19831 /* use the copy made earlier */
19832 to->vval.v_list = from->vval.v_list->lv_copylist;
19833 ++to->vval.v_list->lv_refcount;
19834 }
19835 else
19836 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19837 if (to->vval.v_list == NULL)
19838 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019839 break;
19840 case VAR_DICT:
19841 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019842 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019843 if (from->vval.v_dict == NULL)
19844 to->vval.v_dict = NULL;
19845 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19846 {
19847 /* use the copy made earlier */
19848 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19849 ++to->vval.v_dict->dv_refcount;
19850 }
19851 else
19852 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19853 if (to->vval.v_dict == NULL)
19854 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019855 break;
19856 default:
19857 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019858 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019859 }
19860 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019861 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019862}
19863
19864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865 * ":echo expr1 ..." print each argument separated with a space, add a
19866 * newline at the end.
19867 * ":echon expr1 ..." print each argument plain.
19868 */
19869 void
19870ex_echo(eap)
19871 exarg_T *eap;
19872{
19873 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019874 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019875 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 char_u *p;
19877 int needclr = TRUE;
19878 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019879 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880
19881 if (eap->skip)
19882 ++emsg_skip;
19883 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19884 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019885 /* If eval1() causes an error message the text from the command may
19886 * still need to be cleared. E.g., "echo 22,44". */
19887 need_clr_eos = needclr;
19888
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019890 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891 {
19892 /*
19893 * Report the invalid expression unless the expression evaluation
19894 * has been cancelled due to an aborting error, an interrupt, or an
19895 * exception.
19896 */
19897 if (!aborting())
19898 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019899 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019900 break;
19901 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019902 need_clr_eos = FALSE;
19903
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 if (!eap->skip)
19905 {
19906 if (atstart)
19907 {
19908 atstart = FALSE;
19909 /* Call msg_start() after eval1(), evaluating the expression
19910 * may cause a message to appear. */
19911 if (eap->cmdidx == CMD_echo)
19912 msg_start();
19913 }
19914 else if (eap->cmdidx == CMD_echo)
19915 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019916 current_copyID += COPYID_INC;
19917 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019918 if (p != NULL)
19919 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019921 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019923 if (*p != TAB && needclr)
19924 {
19925 /* remove any text still there from the command */
19926 msg_clr_eos();
19927 needclr = FALSE;
19928 }
19929 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930 }
19931 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019932 {
19933#ifdef FEAT_MBYTE
19934 if (has_mbyte)
19935 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019936 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019937
19938 (void)msg_outtrans_len_attr(p, i, echo_attr);
19939 p += i - 1;
19940 }
19941 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019943 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019946 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019948 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949 arg = skipwhite(arg);
19950 }
19951 eap->nextcmd = check_nextcmd(arg);
19952
19953 if (eap->skip)
19954 --emsg_skip;
19955 else
19956 {
19957 /* remove text that may still be there from the command */
19958 if (needclr)
19959 msg_clr_eos();
19960 if (eap->cmdidx == CMD_echo)
19961 msg_end();
19962 }
19963}
19964
19965/*
19966 * ":echohl {name}".
19967 */
19968 void
19969ex_echohl(eap)
19970 exarg_T *eap;
19971{
19972 int id;
19973
19974 id = syn_name2id(eap->arg);
19975 if (id == 0)
19976 echo_attr = 0;
19977 else
19978 echo_attr = syn_id2attr(id);
19979}
19980
19981/*
19982 * ":execute expr1 ..." execute the result of an expression.
19983 * ":echomsg expr1 ..." Print a message
19984 * ":echoerr expr1 ..." Print an error
19985 * Each gets spaces around each argument and a newline at the end for
19986 * echo commands
19987 */
19988 void
19989ex_execute(eap)
19990 exarg_T *eap;
19991{
19992 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019993 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 int ret = OK;
19995 char_u *p;
19996 garray_T ga;
19997 int len;
19998 int save_did_emsg;
19999
20000 ga_init2(&ga, 1, 80);
20001
20002 if (eap->skip)
20003 ++emsg_skip;
20004 while (*arg != NUL && *arg != '|' && *arg != '\n')
20005 {
20006 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020007 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008 {
20009 /*
20010 * Report the invalid expression unless the expression evaluation
20011 * has been cancelled due to an aborting error, an interrupt, or an
20012 * exception.
20013 */
20014 if (!aborting())
20015 EMSG2(_(e_invexpr2), p);
20016 ret = FAIL;
20017 break;
20018 }
20019
20020 if (!eap->skip)
20021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020022 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 len = (int)STRLEN(p);
20024 if (ga_grow(&ga, len + 2) == FAIL)
20025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020026 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 ret = FAIL;
20028 break;
20029 }
20030 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 ga.ga_len += len;
20034 }
20035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020036 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 arg = skipwhite(arg);
20038 }
20039
20040 if (ret != FAIL && ga.ga_data != NULL)
20041 {
20042 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020045 out_flush();
20046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 else if (eap->cmdidx == CMD_echoerr)
20048 {
20049 /* We don't want to abort following commands, restore did_emsg. */
20050 save_did_emsg = did_emsg;
20051 EMSG((char_u *)ga.ga_data);
20052 if (!force_abort)
20053 did_emsg = save_did_emsg;
20054 }
20055 else if (eap->cmdidx == CMD_execute)
20056 do_cmdline((char_u *)ga.ga_data,
20057 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20058 }
20059
20060 ga_clear(&ga);
20061
20062 if (eap->skip)
20063 --emsg_skip;
20064
20065 eap->nextcmd = check_nextcmd(arg);
20066}
20067
20068/*
20069 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20070 * "arg" points to the "&" or '+' when called, to "option" when returning.
20071 * Returns NULL when no option name found. Otherwise pointer to the char
20072 * after the option name.
20073 */
20074 static char_u *
20075find_option_end(arg, opt_flags)
20076 char_u **arg;
20077 int *opt_flags;
20078{
20079 char_u *p = *arg;
20080
20081 ++p;
20082 if (*p == 'g' && p[1] == ':')
20083 {
20084 *opt_flags = OPT_GLOBAL;
20085 p += 2;
20086 }
20087 else if (*p == 'l' && p[1] == ':')
20088 {
20089 *opt_flags = OPT_LOCAL;
20090 p += 2;
20091 }
20092 else
20093 *opt_flags = 0;
20094
20095 if (!ASCII_ISALPHA(*p))
20096 return NULL;
20097 *arg = p;
20098
20099 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20100 p += 4; /* termcap option */
20101 else
20102 while (ASCII_ISALPHA(*p))
20103 ++p;
20104 return p;
20105}
20106
20107/*
20108 * ":function"
20109 */
20110 void
20111ex_function(eap)
20112 exarg_T *eap;
20113{
20114 char_u *theline;
20115 int j;
20116 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 char_u *name = NULL;
20119 char_u *p;
20120 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020121 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 garray_T newargs;
20123 garray_T newlines;
20124 int varargs = FALSE;
20125 int mustend = FALSE;
20126 int flags = 0;
20127 ufunc_T *fp;
20128 int indent;
20129 int nesting;
20130 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020131 dictitem_T *v;
20132 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020133 static int func_nr = 0; /* number for nameless function */
20134 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020135 hashtab_T *ht;
20136 int todo;
20137 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020138 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139
20140 /*
20141 * ":function" without argument: list functions.
20142 */
20143 if (ends_excmd(*eap->arg))
20144 {
20145 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020146 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020147 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020148 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020149 {
20150 if (!HASHITEM_EMPTY(hi))
20151 {
20152 --todo;
20153 fp = HI2UF(hi);
20154 if (!isdigit(*fp->uf_name))
20155 list_func_head(fp, FALSE);
20156 }
20157 }
20158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 eap->nextcmd = check_nextcmd(eap->arg);
20160 return;
20161 }
20162
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020163 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020164 * ":function /pat": list functions matching pattern.
20165 */
20166 if (*eap->arg == '/')
20167 {
20168 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20169 if (!eap->skip)
20170 {
20171 regmatch_T regmatch;
20172
20173 c = *p;
20174 *p = NUL;
20175 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20176 *p = c;
20177 if (regmatch.regprog != NULL)
20178 {
20179 regmatch.rm_ic = p_ic;
20180
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020181 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020182 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20183 {
20184 if (!HASHITEM_EMPTY(hi))
20185 {
20186 --todo;
20187 fp = HI2UF(hi);
20188 if (!isdigit(*fp->uf_name)
20189 && vim_regexec(&regmatch, fp->uf_name, 0))
20190 list_func_head(fp, FALSE);
20191 }
20192 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020193 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020194 }
20195 }
20196 if (*p == '/')
20197 ++p;
20198 eap->nextcmd = check_nextcmd(p);
20199 return;
20200 }
20201
20202 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020203 * Get the function name. There are these situations:
20204 * func normal function name
20205 * "name" == func, "fudi.fd_dict" == NULL
20206 * dict.func new dictionary entry
20207 * "name" == NULL, "fudi.fd_dict" set,
20208 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20209 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020210 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020211 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20212 * dict.func existing dict entry that's not a Funcref
20213 * "name" == NULL, "fudi.fd_dict" set,
20214 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020217 name = trans_function_name(&p, eap->skip, 0, &fudi);
20218 paren = (vim_strchr(p, '(') != NULL);
20219 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 {
20221 /*
20222 * Return on an invalid expression in braces, unless the expression
20223 * evaluation has been cancelled due to an aborting error, an
20224 * interrupt, or an exception.
20225 */
20226 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020227 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020228 if (!eap->skip && fudi.fd_newkey != NULL)
20229 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020230 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233 else
20234 eap->skip = TRUE;
20235 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020236
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 /* An error in a function call during evaluation of an expression in magic
20238 * braces should not cause the function not to be defined. */
20239 saved_did_emsg = did_emsg;
20240 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241
20242 /*
20243 * ":function func" with only function name: list function.
20244 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020245 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 {
20247 if (!ends_excmd(*skipwhite(p)))
20248 {
20249 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020250 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 }
20252 eap->nextcmd = check_nextcmd(p);
20253 if (eap->nextcmd != NULL)
20254 *p = NUL;
20255 if (!eap->skip && !got_int)
20256 {
20257 fp = find_func(name);
20258 if (fp != NULL)
20259 {
20260 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020261 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020263 if (FUNCLINE(fp, j) == NULL)
20264 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265 msg_putchar('\n');
20266 msg_outnum((long)(j + 1));
20267 if (j < 9)
20268 msg_putchar(' ');
20269 if (j < 99)
20270 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020271 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 out_flush(); /* show a line at a time */
20273 ui_breakcheck();
20274 }
20275 if (!got_int)
20276 {
20277 msg_putchar('\n');
20278 msg_puts((char_u *)" endfunction");
20279 }
20280 }
20281 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020282 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020284 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 }
20286
20287 /*
20288 * ":function name(arg1, arg2)" Define function.
20289 */
20290 p = skipwhite(p);
20291 if (*p != '(')
20292 {
20293 if (!eap->skip)
20294 {
20295 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020296 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 }
20298 /* attempt to continue by skipping some text */
20299 if (vim_strchr(p, '(') != NULL)
20300 p = vim_strchr(p, '(');
20301 }
20302 p = skipwhite(p + 1);
20303
20304 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20305 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20306
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020307 if (!eap->skip)
20308 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020309 /* Check the name of the function. Unless it's a dictionary function
20310 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020311 if (name != NULL)
20312 arg = name;
20313 else
20314 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020315 if (arg != NULL && (fudi.fd_di == NULL
20316 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020317 {
20318 if (*arg == K_SPECIAL)
20319 j = 3;
20320 else
20321 j = 0;
20322 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20323 : eval_isnamec(arg[j])))
20324 ++j;
20325 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020326 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020327 }
20328 }
20329
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 /*
20331 * Isolate the arguments: "arg1, arg2, ...)"
20332 */
20333 while (*p != ')')
20334 {
20335 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20336 {
20337 varargs = TRUE;
20338 p += 3;
20339 mustend = TRUE;
20340 }
20341 else
20342 {
20343 arg = p;
20344 while (ASCII_ISALNUM(*p) || *p == '_')
20345 ++p;
20346 if (arg == p || isdigit(*arg)
20347 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20348 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20349 {
20350 if (!eap->skip)
20351 EMSG2(_("E125: Illegal argument: %s"), arg);
20352 break;
20353 }
20354 if (ga_grow(&newargs, 1) == FAIL)
20355 goto erret;
20356 c = *p;
20357 *p = NUL;
20358 arg = vim_strsave(arg);
20359 if (arg == NULL)
20360 goto erret;
20361 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20362 *p = c;
20363 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 if (*p == ',')
20365 ++p;
20366 else
20367 mustend = TRUE;
20368 }
20369 p = skipwhite(p);
20370 if (mustend && *p != ')')
20371 {
20372 if (!eap->skip)
20373 EMSG2(_(e_invarg2), eap->arg);
20374 break;
20375 }
20376 }
20377 ++p; /* skip the ')' */
20378
Bram Moolenaare9a41262005-01-15 22:18:47 +000020379 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 for (;;)
20381 {
20382 p = skipwhite(p);
20383 if (STRNCMP(p, "range", 5) == 0)
20384 {
20385 flags |= FC_RANGE;
20386 p += 5;
20387 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020388 else if (STRNCMP(p, "dict", 4) == 0)
20389 {
20390 flags |= FC_DICT;
20391 p += 4;
20392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 else if (STRNCMP(p, "abort", 5) == 0)
20394 {
20395 flags |= FC_ABORT;
20396 p += 5;
20397 }
20398 else
20399 break;
20400 }
20401
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020402 /* When there is a line break use what follows for the function body.
20403 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20404 if (*p == '\n')
20405 line_arg = p + 1;
20406 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 EMSG(_(e_trailing));
20408
20409 /*
20410 * Read the body of the function, until ":endfunction" is found.
20411 */
20412 if (KeyTyped)
20413 {
20414 /* Check if the function already exists, don't let the user type the
20415 * whole function before telling him it doesn't work! For a script we
20416 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020417 if (!eap->skip && !eap->forceit)
20418 {
20419 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20420 EMSG(_(e_funcdict));
20421 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020422 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020425 if (!eap->skip && did_emsg)
20426 goto erret;
20427
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 msg_putchar('\n'); /* don't overwrite the function name */
20429 cmdline_row = msg_row;
20430 }
20431
20432 indent = 2;
20433 nesting = 0;
20434 for (;;)
20435 {
20436 msg_scroll = TRUE;
20437 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020438 sourcing_lnum_off = sourcing_lnum;
20439
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020440 if (line_arg != NULL)
20441 {
20442 /* Use eap->arg, split up in parts by line breaks. */
20443 theline = line_arg;
20444 p = vim_strchr(theline, '\n');
20445 if (p == NULL)
20446 line_arg += STRLEN(line_arg);
20447 else
20448 {
20449 *p = NUL;
20450 line_arg = p + 1;
20451 }
20452 }
20453 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 theline = getcmdline(':', 0L, indent);
20455 else
20456 theline = eap->getline(':', eap->cookie, indent);
20457 if (KeyTyped)
20458 lines_left = Rows - 1;
20459 if (theline == NULL)
20460 {
20461 EMSG(_("E126: Missing :endfunction"));
20462 goto erret;
20463 }
20464
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020465 /* Detect line continuation: sourcing_lnum increased more than one. */
20466 if (sourcing_lnum > sourcing_lnum_off + 1)
20467 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20468 else
20469 sourcing_lnum_off = 0;
20470
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 if (skip_until != NULL)
20472 {
20473 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20474 * don't check for ":endfunc". */
20475 if (STRCMP(theline, skip_until) == 0)
20476 {
20477 vim_free(skip_until);
20478 skip_until = NULL;
20479 }
20480 }
20481 else
20482 {
20483 /* skip ':' and blanks*/
20484 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20485 ;
20486
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020487 /* Check for "endfunction". */
20488 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020490 if (line_arg == NULL)
20491 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 break;
20493 }
20494
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020495 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 * at "end". */
20497 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20498 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020499 else if (STRNCMP(p, "if", 2) == 0
20500 || STRNCMP(p, "wh", 2) == 0
20501 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502 || STRNCMP(p, "try", 3) == 0)
20503 indent += 2;
20504
20505 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020506 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020508 if (*p == '!')
20509 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 p += eval_fname_script(p);
20511 if (ASCII_ISALPHA(*p))
20512 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020513 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 if (*skipwhite(p) == '(')
20515 {
20516 ++nesting;
20517 indent += 2;
20518 }
20519 }
20520 }
20521
20522 /* Check for ":append" or ":insert". */
20523 p = skip_range(p, NULL);
20524 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20525 || (p[0] == 'i'
20526 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20527 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20528 skip_until = vim_strsave((char_u *)".");
20529
20530 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20531 arg = skipwhite(skiptowhite(p));
20532 if (arg[0] == '<' && arg[1] =='<'
20533 && ((p[0] == 'p' && p[1] == 'y'
20534 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20535 || (p[0] == 'p' && p[1] == 'e'
20536 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20537 || (p[0] == 't' && p[1] == 'c'
20538 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20539 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20540 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020541 || (p[0] == 'm' && p[1] == 'z'
20542 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543 ))
20544 {
20545 /* ":python <<" continues until a dot, like ":append" */
20546 p = skipwhite(arg + 2);
20547 if (*p == NUL)
20548 skip_until = vim_strsave((char_u *)".");
20549 else
20550 skip_until = vim_strsave(p);
20551 }
20552 }
20553
20554 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020555 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020556 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020557 if (line_arg == NULL)
20558 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020560 }
20561
20562 /* Copy the line to newly allocated memory. get_one_sourceline()
20563 * allocates 250 bytes per line, this saves 80% on average. The cost
20564 * is an extra alloc/free. */
20565 p = vim_strsave(theline);
20566 if (p != NULL)
20567 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020568 if (line_arg == NULL)
20569 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020570 theline = p;
20571 }
20572
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020573 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20574
20575 /* Add NULL lines for continuation lines, so that the line count is
20576 * equal to the index in the growarray. */
20577 while (sourcing_lnum_off-- > 0)
20578 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020579
20580 /* Check for end of eap->arg. */
20581 if (line_arg != NULL && *line_arg == NUL)
20582 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 }
20584
20585 /* Don't define the function when skipping commands or when an error was
20586 * detected. */
20587 if (eap->skip || did_emsg)
20588 goto erret;
20589
20590 /*
20591 * If there are no errors, add the function
20592 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020593 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020594 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020595 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020596 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020597 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020598 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020599 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020600 goto erret;
20601 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020602
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020603 fp = find_func(name);
20604 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020606 if (!eap->forceit)
20607 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020608 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020609 goto erret;
20610 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020611 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020612 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020613 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020614 name);
20615 goto erret;
20616 }
20617 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020618 ga_clear_strings(&(fp->uf_args));
20619 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020620 vim_free(name);
20621 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623 }
20624 else
20625 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020626 char numbuf[20];
20627
20628 fp = NULL;
20629 if (fudi.fd_newkey == NULL && !eap->forceit)
20630 {
20631 EMSG(_(e_funcdict));
20632 goto erret;
20633 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020634 if (fudi.fd_di == NULL)
20635 {
20636 /* Can't add a function to a locked dictionary */
20637 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20638 goto erret;
20639 }
20640 /* Can't change an existing function if it is locked */
20641 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20642 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020643
20644 /* Give the function a sequential number. Can only be used with a
20645 * Funcref! */
20646 vim_free(name);
20647 sprintf(numbuf, "%d", ++func_nr);
20648 name = vim_strsave((char_u *)numbuf);
20649 if (name == NULL)
20650 goto erret;
20651 }
20652
20653 if (fp == NULL)
20654 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020655 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020656 {
20657 int slen, plen;
20658 char_u *scriptname;
20659
20660 /* Check that the autoload name matches the script name. */
20661 j = FAIL;
20662 if (sourcing_name != NULL)
20663 {
20664 scriptname = autoload_name(name);
20665 if (scriptname != NULL)
20666 {
20667 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020668 plen = (int)STRLEN(p);
20669 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020670 if (slen > plen && fnamecmp(p,
20671 sourcing_name + slen - plen) == 0)
20672 j = OK;
20673 vim_free(scriptname);
20674 }
20675 }
20676 if (j == FAIL)
20677 {
20678 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20679 goto erret;
20680 }
20681 }
20682
20683 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 if (fp == NULL)
20685 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020686
20687 if (fudi.fd_dict != NULL)
20688 {
20689 if (fudi.fd_di == NULL)
20690 {
20691 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020692 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020693 if (fudi.fd_di == NULL)
20694 {
20695 vim_free(fp);
20696 goto erret;
20697 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020698 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20699 {
20700 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020701 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020702 goto erret;
20703 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020704 }
20705 else
20706 /* overwrite existing dict entry */
20707 clear_tv(&fudi.fd_di->di_tv);
20708 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020709 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020710 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020711 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020712
20713 /* behave like "dict" was used */
20714 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020715 }
20716
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020718 STRCPY(fp->uf_name, name);
20719 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020721 fp->uf_args = newargs;
20722 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020723#ifdef FEAT_PROFILE
20724 fp->uf_tml_count = NULL;
20725 fp->uf_tml_total = NULL;
20726 fp->uf_tml_self = NULL;
20727 fp->uf_profiling = FALSE;
20728 if (prof_def_func())
20729 func_do_profile(fp);
20730#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020731 fp->uf_varargs = varargs;
20732 fp->uf_flags = flags;
20733 fp->uf_calls = 0;
20734 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020735 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736
20737erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 ga_clear_strings(&newargs);
20739 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020740ret_free:
20741 vim_free(skip_until);
20742 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745}
20746
20747/*
20748 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020749 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020751 * flags:
20752 * TFN_INT: internal function name OK
20753 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 * Advances "pp" to just after the function name (if no error).
20755 */
20756 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020757trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 char_u **pp;
20759 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020760 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020761 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020763 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 char_u *start;
20765 char_u *end;
20766 int lead;
20767 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020769 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020770
20771 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020772 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020774
20775 /* Check for hard coded <SNR>: already translated function ID (from a user
20776 * command). */
20777 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20778 && (*pp)[2] == (int)KE_SNR)
20779 {
20780 *pp += 3;
20781 len = get_id_len(pp) + 3;
20782 return vim_strnsave(start, len);
20783 }
20784
20785 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20786 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020788 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020790
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020791 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20792 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020793 if (end == start)
20794 {
20795 if (!skip)
20796 EMSG(_("E129: Function name required"));
20797 goto theend;
20798 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020799 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020800 {
20801 /*
20802 * Report an invalid expression in braces, unless the expression
20803 * evaluation has been cancelled due to an aborting error, an
20804 * interrupt, or an exception.
20805 */
20806 if (!aborting())
20807 {
20808 if (end != NULL)
20809 EMSG2(_(e_invarg2), start);
20810 }
20811 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020812 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020813 goto theend;
20814 }
20815
20816 if (lv.ll_tv != NULL)
20817 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020818 if (fdp != NULL)
20819 {
20820 fdp->fd_dict = lv.ll_dict;
20821 fdp->fd_newkey = lv.ll_newkey;
20822 lv.ll_newkey = NULL;
20823 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020824 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020825 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20826 {
20827 name = vim_strsave(lv.ll_tv->vval.v_string);
20828 *pp = end;
20829 }
20830 else
20831 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020832 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20833 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020834 EMSG(_(e_funcref));
20835 else
20836 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020837 name = NULL;
20838 }
20839 goto theend;
20840 }
20841
20842 if (lv.ll_name == NULL)
20843 {
20844 /* Error found, but continue after the function name. */
20845 *pp = end;
20846 goto theend;
20847 }
20848
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020849 /* Check if the name is a Funcref. If so, use the value. */
20850 if (lv.ll_exp_name != NULL)
20851 {
20852 len = (int)STRLEN(lv.ll_exp_name);
20853 name = deref_func_name(lv.ll_exp_name, &len);
20854 if (name == lv.ll_exp_name)
20855 name = NULL;
20856 }
20857 else
20858 {
20859 len = (int)(end - *pp);
20860 name = deref_func_name(*pp, &len);
20861 if (name == *pp)
20862 name = NULL;
20863 }
20864 if (name != NULL)
20865 {
20866 name = vim_strsave(name);
20867 *pp = end;
20868 goto theend;
20869 }
20870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020871 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020872 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020873 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020874 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20875 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20876 {
20877 /* When there was "s:" already or the name expanded to get a
20878 * leading "s:" then remove it. */
20879 lv.ll_name += 2;
20880 len -= 2;
20881 lead = 2;
20882 }
20883 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020884 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020885 {
20886 if (lead == 2) /* skip over "s:" */
20887 lv.ll_name += 2;
20888 len = (int)(end - lv.ll_name);
20889 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020890
20891 /*
20892 * Copy the function name to allocated memory.
20893 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20894 * Accept <SNR>123_name() outside a script.
20895 */
20896 if (skip)
20897 lead = 0; /* do nothing */
20898 else if (lead > 0)
20899 {
20900 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020901 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20902 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020903 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020904 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020905 if (current_SID <= 0)
20906 {
20907 EMSG(_(e_usingsid));
20908 goto theend;
20909 }
20910 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20911 lead += (int)STRLEN(sid_buf);
20912 }
20913 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020914 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020915 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020916 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020917 goto theend;
20918 }
20919 name = alloc((unsigned)(len + lead + 1));
20920 if (name != NULL)
20921 {
20922 if (lead > 0)
20923 {
20924 name[0] = K_SPECIAL;
20925 name[1] = KS_EXTRA;
20926 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020927 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020928 STRCPY(name + 3, sid_buf);
20929 }
20930 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20931 name[len + lead] = NUL;
20932 }
20933 *pp = end;
20934
20935theend:
20936 clear_lval(&lv);
20937 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938}
20939
20940/*
20941 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20942 * Return 2 if "p" starts with "s:".
20943 * Return 0 otherwise.
20944 */
20945 static int
20946eval_fname_script(p)
20947 char_u *p;
20948{
20949 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20950 || STRNICMP(p + 1, "SNR>", 4) == 0))
20951 return 5;
20952 if (p[0] == 's' && p[1] == ':')
20953 return 2;
20954 return 0;
20955}
20956
20957/*
20958 * Return TRUE if "p" starts with "<SID>" or "s:".
20959 * Only works if eval_fname_script() returned non-zero for "p"!
20960 */
20961 static int
20962eval_fname_sid(p)
20963 char_u *p;
20964{
20965 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20966}
20967
20968/*
20969 * List the head of the function: "name(arg1, arg2)".
20970 */
20971 static void
20972list_func_head(fp, indent)
20973 ufunc_T *fp;
20974 int indent;
20975{
20976 int j;
20977
20978 msg_start();
20979 if (indent)
20980 MSG_PUTS(" ");
20981 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020982 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 {
20984 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020985 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 }
20987 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020988 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020990 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 {
20992 if (j)
20993 MSG_PUTS(", ");
20994 msg_puts(FUNCARG(fp, j));
20995 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020996 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 {
20998 if (j)
20999 MSG_PUTS(", ");
21000 MSG_PUTS("...");
21001 }
21002 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021003 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021004 if (p_verbose > 0)
21005 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006}
21007
21008/*
21009 * Find a function by name, return pointer to it in ufuncs.
21010 * Return NULL for unknown function.
21011 */
21012 static ufunc_T *
21013find_func(name)
21014 char_u *name;
21015{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021016 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021018 hi = hash_find(&func_hashtab, name);
21019 if (!HASHITEM_EMPTY(hi))
21020 return HI2UF(hi);
21021 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022}
21023
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021024#if defined(EXITFREE) || defined(PROTO)
21025 void
21026free_all_functions()
21027{
21028 hashitem_T *hi;
21029
21030 /* Need to start all over every time, because func_free() may change the
21031 * hash table. */
21032 while (func_hashtab.ht_used > 0)
21033 for (hi = func_hashtab.ht_array; ; ++hi)
21034 if (!HASHITEM_EMPTY(hi))
21035 {
21036 func_free(HI2UF(hi));
21037 break;
21038 }
21039}
21040#endif
21041
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021042/*
21043 * Return TRUE if a function "name" exists.
21044 */
21045 static int
21046function_exists(name)
21047 char_u *name;
21048{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021049 char_u *nm = name;
21050 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021051 int n = FALSE;
21052
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021053 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021054 nm = skipwhite(nm);
21055
21056 /* Only accept "funcname", "funcname ", "funcname (..." and
21057 * "funcname(...", not "funcname!...". */
21058 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021059 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021060 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021061 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021062 else
21063 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021064 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021065 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021066 return n;
21067}
21068
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021069/*
21070 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021071 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021072 */
21073 static int
21074builtin_function(name)
21075 char_u *name;
21076{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021077 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21078 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021079}
21080
Bram Moolenaar05159a02005-02-26 23:04:13 +000021081#if defined(FEAT_PROFILE) || defined(PROTO)
21082/*
21083 * Start profiling function "fp".
21084 */
21085 static void
21086func_do_profile(fp)
21087 ufunc_T *fp;
21088{
21089 fp->uf_tm_count = 0;
21090 profile_zero(&fp->uf_tm_self);
21091 profile_zero(&fp->uf_tm_total);
21092 if (fp->uf_tml_count == NULL)
21093 fp->uf_tml_count = (int *)alloc_clear((unsigned)
21094 (sizeof(int) * fp->uf_lines.ga_len));
21095 if (fp->uf_tml_total == NULL)
21096 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
21097 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21098 if (fp->uf_tml_self == NULL)
21099 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
21100 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21101 fp->uf_tml_idx = -1;
21102 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21103 || fp->uf_tml_self == NULL)
21104 return; /* out of memory */
21105
21106 fp->uf_profiling = TRUE;
21107}
21108
21109/*
21110 * Dump the profiling results for all functions in file "fd".
21111 */
21112 void
21113func_dump_profile(fd)
21114 FILE *fd;
21115{
21116 hashitem_T *hi;
21117 int todo;
21118 ufunc_T *fp;
21119 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021120 ufunc_T **sorttab;
21121 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021122
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021123 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021124 if (todo == 0)
21125 return; /* nothing to dump */
21126
Bram Moolenaar73830342005-02-28 22:48:19 +000021127 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21128
Bram Moolenaar05159a02005-02-26 23:04:13 +000021129 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21130 {
21131 if (!HASHITEM_EMPTY(hi))
21132 {
21133 --todo;
21134 fp = HI2UF(hi);
21135 if (fp->uf_profiling)
21136 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021137 if (sorttab != NULL)
21138 sorttab[st_len++] = fp;
21139
Bram Moolenaar05159a02005-02-26 23:04:13 +000021140 if (fp->uf_name[0] == K_SPECIAL)
21141 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21142 else
21143 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21144 if (fp->uf_tm_count == 1)
21145 fprintf(fd, "Called 1 time\n");
21146 else
21147 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21148 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21149 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21150 fprintf(fd, "\n");
21151 fprintf(fd, "count total (s) self (s)\n");
21152
21153 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21154 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021155 if (FUNCLINE(fp, i) == NULL)
21156 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021157 prof_func_line(fd, fp->uf_tml_count[i],
21158 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021159 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21160 }
21161 fprintf(fd, "\n");
21162 }
21163 }
21164 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021165
21166 if (sorttab != NULL && st_len > 0)
21167 {
21168 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21169 prof_total_cmp);
21170 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21171 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21172 prof_self_cmp);
21173 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21174 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021175
21176 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021177}
Bram Moolenaar73830342005-02-28 22:48:19 +000021178
21179 static void
21180prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21181 FILE *fd;
21182 ufunc_T **sorttab;
21183 int st_len;
21184 char *title;
21185 int prefer_self; /* when equal print only self time */
21186{
21187 int i;
21188 ufunc_T *fp;
21189
21190 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21191 fprintf(fd, "count total (s) self (s) function\n");
21192 for (i = 0; i < 20 && i < st_len; ++i)
21193 {
21194 fp = sorttab[i];
21195 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21196 prefer_self);
21197 if (fp->uf_name[0] == K_SPECIAL)
21198 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21199 else
21200 fprintf(fd, " %s()\n", fp->uf_name);
21201 }
21202 fprintf(fd, "\n");
21203}
21204
21205/*
21206 * Print the count and times for one function or function line.
21207 */
21208 static void
21209prof_func_line(fd, count, total, self, prefer_self)
21210 FILE *fd;
21211 int count;
21212 proftime_T *total;
21213 proftime_T *self;
21214 int prefer_self; /* when equal print only self time */
21215{
21216 if (count > 0)
21217 {
21218 fprintf(fd, "%5d ", count);
21219 if (prefer_self && profile_equal(total, self))
21220 fprintf(fd, " ");
21221 else
21222 fprintf(fd, "%s ", profile_msg(total));
21223 if (!prefer_self && profile_equal(total, self))
21224 fprintf(fd, " ");
21225 else
21226 fprintf(fd, "%s ", profile_msg(self));
21227 }
21228 else
21229 fprintf(fd, " ");
21230}
21231
21232/*
21233 * Compare function for total time sorting.
21234 */
21235 static int
21236#ifdef __BORLANDC__
21237_RTLENTRYF
21238#endif
21239prof_total_cmp(s1, s2)
21240 const void *s1;
21241 const void *s2;
21242{
21243 ufunc_T *p1, *p2;
21244
21245 p1 = *(ufunc_T **)s1;
21246 p2 = *(ufunc_T **)s2;
21247 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21248}
21249
21250/*
21251 * Compare function for self time sorting.
21252 */
21253 static int
21254#ifdef __BORLANDC__
21255_RTLENTRYF
21256#endif
21257prof_self_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_self, &p2->uf_tm_self);
21266}
21267
Bram Moolenaar05159a02005-02-26 23:04:13 +000021268#endif
21269
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021270/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021271 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021272 * Return TRUE if a package was loaded.
21273 */
21274 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021275script_autoload(name, reload)
21276 char_u *name;
21277 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021278{
21279 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021280 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021281 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021282 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021283
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021284 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021285 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021286 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021287 return FALSE;
21288
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021289 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021290
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021291 /* Find the name in the list of previously loaded package names. Skip
21292 * "autoload/", it's always the same. */
21293 for (i = 0; i < ga_loaded.ga_len; ++i)
21294 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21295 break;
21296 if (!reload && i < ga_loaded.ga_len)
21297 ret = FALSE; /* was loaded already */
21298 else
21299 {
21300 /* Remember the name if it wasn't loaded already. */
21301 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21302 {
21303 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21304 tofree = NULL;
21305 }
21306
21307 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021308 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021309 ret = TRUE;
21310 }
21311
21312 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021313 return ret;
21314}
21315
21316/*
21317 * Return the autoload script name for a function or variable name.
21318 * Returns NULL when out of memory.
21319 */
21320 static char_u *
21321autoload_name(name)
21322 char_u *name;
21323{
21324 char_u *p;
21325 char_u *scriptname;
21326
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021327 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021328 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21329 if (scriptname == NULL)
21330 return FALSE;
21331 STRCPY(scriptname, "autoload/");
21332 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021333 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021334 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021335 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021336 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021337 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021338}
21339
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21341
21342/*
21343 * Function given to ExpandGeneric() to obtain the list of user defined
21344 * function names.
21345 */
21346 char_u *
21347get_user_func_name(xp, idx)
21348 expand_T *xp;
21349 int idx;
21350{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021351 static long_u done;
21352 static hashitem_T *hi;
21353 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354
21355 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021357 done = 0;
21358 hi = func_hashtab.ht_array;
21359 }
21360 if (done < func_hashtab.ht_used)
21361 {
21362 if (done++ > 0)
21363 ++hi;
21364 while (HASHITEM_EMPTY(hi))
21365 ++hi;
21366 fp = HI2UF(hi);
21367
21368 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21369 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370
21371 cat_func_name(IObuff, fp);
21372 if (xp->xp_context != EXPAND_USER_FUNC)
21373 {
21374 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021375 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376 STRCAT(IObuff, ")");
21377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 return IObuff;
21379 }
21380 return NULL;
21381}
21382
21383#endif /* FEAT_CMDL_COMPL */
21384
21385/*
21386 * Copy the function name of "fp" to buffer "buf".
21387 * "buf" must be able to hold the function name plus three bytes.
21388 * Takes care of script-local function names.
21389 */
21390 static void
21391cat_func_name(buf, fp)
21392 char_u *buf;
21393 ufunc_T *fp;
21394{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021395 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 {
21397 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021398 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 }
21400 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021401 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402}
21403
21404/*
21405 * ":delfunction {name}"
21406 */
21407 void
21408ex_delfunction(eap)
21409 exarg_T *eap;
21410{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021411 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 char_u *p;
21413 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021414 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415
21416 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021417 name = trans_function_name(&p, eap->skip, 0, &fudi);
21418 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021420 {
21421 if (fudi.fd_dict != NULL && !eap->skip)
21422 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 if (!ends_excmd(*skipwhite(p)))
21426 {
21427 vim_free(name);
21428 EMSG(_(e_trailing));
21429 return;
21430 }
21431 eap->nextcmd = check_nextcmd(p);
21432 if (eap->nextcmd != NULL)
21433 *p = NUL;
21434
21435 if (!eap->skip)
21436 fp = find_func(name);
21437 vim_free(name);
21438
21439 if (!eap->skip)
21440 {
21441 if (fp == NULL)
21442 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021443 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444 return;
21445 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021446 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 {
21448 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21449 return;
21450 }
21451
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021452 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021454 /* Delete the dict item that refers to the function, it will
21455 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021456 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021458 else
21459 func_free(fp);
21460 }
21461}
21462
21463/*
21464 * Free a function and remove it from the list of functions.
21465 */
21466 static void
21467func_free(fp)
21468 ufunc_T *fp;
21469{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021470 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021471
21472 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021473 ga_clear_strings(&(fp->uf_args));
21474 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021475#ifdef FEAT_PROFILE
21476 vim_free(fp->uf_tml_count);
21477 vim_free(fp->uf_tml_total);
21478 vim_free(fp->uf_tml_self);
21479#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021480
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021481 /* remove the function from the function hashtable */
21482 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21483 if (HASHITEM_EMPTY(hi))
21484 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021485 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021486 hash_remove(&func_hashtab, hi);
21487
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021488 vim_free(fp);
21489}
21490
21491/*
21492 * Unreference a Function: decrement the reference count and free it when it
21493 * becomes zero. Only for numbered functions.
21494 */
21495 static void
21496func_unref(name)
21497 char_u *name;
21498{
21499 ufunc_T *fp;
21500
21501 if (name != NULL && isdigit(*name))
21502 {
21503 fp = find_func(name);
21504 if (fp == NULL)
21505 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021506 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021507 {
21508 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021509 * when "uf_calls" becomes zero. */
21510 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021511 func_free(fp);
21512 }
21513 }
21514}
21515
21516/*
21517 * Count a reference to a Function.
21518 */
21519 static void
21520func_ref(name)
21521 char_u *name;
21522{
21523 ufunc_T *fp;
21524
21525 if (name != NULL && isdigit(*name))
21526 {
21527 fp = find_func(name);
21528 if (fp == NULL)
21529 EMSG2(_(e_intern2), "func_ref()");
21530 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021531 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 }
21533}
21534
21535/*
21536 * Call a user function.
21537 */
21538 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021539call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540 ufunc_T *fp; /* pointer to function */
21541 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021542 typval_T *argvars; /* arguments */
21543 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 linenr_T firstline; /* first line of range */
21545 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021546 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547{
Bram Moolenaar33570922005-01-25 22:26:29 +000021548 char_u *save_sourcing_name;
21549 linenr_T save_sourcing_lnum;
21550 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021551 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021552 int save_did_emsg;
21553 static int depth = 0;
21554 dictitem_T *v;
21555 int fixvar_idx = 0; /* index in fixvar[] */
21556 int i;
21557 int ai;
21558 char_u numbuf[NUMBUFLEN];
21559 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021560#ifdef FEAT_PROFILE
21561 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021562 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564
21565 /* If depth of calling is getting too high, don't execute the function */
21566 if (depth >= p_mfd)
21567 {
21568 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021569 rettv->v_type = VAR_NUMBER;
21570 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021571 return;
21572 }
21573 ++depth;
21574
21575 line_breakcheck(); /* check for CTRL-C hit */
21576
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021577 fc = (funccall_T *)alloc(sizeof(funccall_T));
21578 fc->caller = current_funccal;
21579 current_funccal = fc;
21580 fc->func = fp;
21581 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021582 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021583 fc->linenr = 0;
21584 fc->returned = FALSE;
21585 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021587 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21588 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589
Bram Moolenaar33570922005-01-25 22:26:29 +000021590 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021591 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021592 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21593 * each argument variable and saves a lot of time.
21594 */
21595 /*
21596 * Init l: variables.
21597 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021598 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021599 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021600 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021601 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21602 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021603 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021604 name = v->di_key;
21605 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021606 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021607 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021608 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021609 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021610 v->di_tv.vval.v_dict = selfdict;
21611 ++selfdict->dv_refcount;
21612 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021613
Bram Moolenaar33570922005-01-25 22:26:29 +000021614 /*
21615 * Init a: variables.
21616 * Set a:0 to "argcount".
21617 * Set a:000 to a list with room for the "..." arguments.
21618 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021619 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21620 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021621 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021622 /* Use "name" to avoid a warning from some compiler that checks the
21623 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021624 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021625 name = v->di_key;
21626 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021627 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021628 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021629 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021630 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021631 v->di_tv.vval.v_list = &fc->l_varlist;
21632 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21633 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21634 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021635
21636 /*
21637 * Set a:firstline to "firstline" and a:lastline to "lastline".
21638 * Set a:name to named arguments.
21639 * Set a:N to the "..." arguments.
21640 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021641 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021642 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021643 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021644 (varnumber_T)lastline);
21645 for (i = 0; i < argcount; ++i)
21646 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021647 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021648 if (ai < 0)
21649 /* named argument a:name */
21650 name = FUNCARG(fp, i);
21651 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021652 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021653 /* "..." argument a:1, a:2, etc. */
21654 sprintf((char *)numbuf, "%d", ai + 1);
21655 name = numbuf;
21656 }
21657 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21658 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021659 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021660 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21661 }
21662 else
21663 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021664 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21665 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021666 if (v == NULL)
21667 break;
21668 v->di_flags = DI_FLAGS_RO;
21669 }
21670 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021671 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021672
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021673 /* Note: the values are copied directly to avoid alloc/free.
21674 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021675 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021676 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021677
21678 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21679 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021680 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21681 fc->l_listitems[ai].li_tv = argvars[i];
21682 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021683 }
21684 }
21685
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686 /* Don't redraw while executing the function. */
21687 ++RedrawingDisabled;
21688 save_sourcing_name = sourcing_name;
21689 save_sourcing_lnum = sourcing_lnum;
21690 sourcing_lnum = 1;
21691 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021692 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 if (sourcing_name != NULL)
21694 {
21695 if (save_sourcing_name != NULL
21696 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21697 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21698 else
21699 STRCPY(sourcing_name, "function ");
21700 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21701
21702 if (p_verbose >= 12)
21703 {
21704 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021705 verbose_enter_scroll();
21706
Bram Moolenaar555b2802005-05-19 21:08:39 +000021707 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 if (p_verbose >= 14)
21709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021711 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021712 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021713 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714
21715 msg_puts((char_u *)"(");
21716 for (i = 0; i < argcount; ++i)
21717 {
21718 if (i > 0)
21719 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021720 if (argvars[i].v_type == VAR_NUMBER)
21721 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 else
21723 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021724 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21725 if (s != NULL)
21726 {
21727 trunc_string(s, buf, MSG_BUF_CLEN);
21728 msg_puts(buf);
21729 vim_free(tofree);
21730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731 }
21732 }
21733 msg_puts((char_u *)")");
21734 }
21735 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021736
21737 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 --no_wait_return;
21739 }
21740 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021741#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021742 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021743 {
21744 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21745 func_do_profile(fp);
21746 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021747 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021748 {
21749 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021750 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021751 profile_zero(&fp->uf_tm_children);
21752 }
21753 script_prof_save(&wait_start);
21754 }
21755#endif
21756
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021758 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 save_did_emsg = did_emsg;
21760 did_emsg = FALSE;
21761
21762 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021763 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21765
21766 --RedrawingDisabled;
21767
21768 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021769 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021771 clear_tv(rettv);
21772 rettv->v_type = VAR_NUMBER;
21773 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774 }
21775
Bram Moolenaar05159a02005-02-26 23:04:13 +000021776#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021777 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021778 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021779 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021780 profile_end(&call_start);
21781 profile_sub_wait(&wait_start, &call_start);
21782 profile_add(&fp->uf_tm_total, &call_start);
21783 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021784 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021785 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021786 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21787 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021788 }
21789 }
21790#endif
21791
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 /* when being verbose, mention the return value */
21793 if (p_verbose >= 12)
21794 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021796 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021799 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021800 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021801 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021802 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021803 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021805 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021806 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021807 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021808 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021809
Bram Moolenaar555b2802005-05-19 21:08:39 +000021810 /* The value may be very long. Skip the middle part, so that we
21811 * have some idea how it starts and ends. smsg() would always
21812 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021813 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021814 if (s != NULL)
21815 {
21816 trunc_string(s, buf, MSG_BUF_CLEN);
21817 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21818 vim_free(tofree);
21819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 }
21821 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021822
21823 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 --no_wait_return;
21825 }
21826
21827 vim_free(sourcing_name);
21828 sourcing_name = save_sourcing_name;
21829 sourcing_lnum = save_sourcing_lnum;
21830 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021831#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021832 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021833 script_prof_restore(&wait_start);
21834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835
21836 if (p_verbose >= 12 && sourcing_name != NULL)
21837 {
21838 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021839 verbose_enter_scroll();
21840
Bram Moolenaar555b2802005-05-19 21:08:39 +000021841 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021843
21844 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 --no_wait_return;
21846 }
21847
21848 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021849 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021851
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021852 /* If the a:000 list and the l: and a: dicts are not referenced we can
21853 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021854 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21855 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21856 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21857 {
21858 free_funccal(fc, FALSE);
21859 }
21860 else
21861 {
21862 hashitem_T *hi;
21863 listitem_T *li;
21864 int todo;
21865
21866 /* "fc" is still in use. This can happen when returning "a:000" or
21867 * assigning "l:" to a global variable.
21868 * Link "fc" in the list for garbage collection later. */
21869 fc->caller = previous_funccal;
21870 previous_funccal = fc;
21871
21872 /* Make a copy of the a: variables, since we didn't do that above. */
21873 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21874 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21875 {
21876 if (!HASHITEM_EMPTY(hi))
21877 {
21878 --todo;
21879 v = HI2DI(hi);
21880 copy_tv(&v->di_tv, &v->di_tv);
21881 }
21882 }
21883
21884 /* Make a copy of the a:000 items, since we didn't do that above. */
21885 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21886 copy_tv(&li->li_tv, &li->li_tv);
21887 }
21888}
21889
21890/*
21891 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021892 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021893 */
21894 static int
21895can_free_funccal(fc, copyID)
21896 funccall_T *fc;
21897 int copyID;
21898{
21899 return (fc->l_varlist.lv_copyID != copyID
21900 && fc->l_vars.dv_copyID != copyID
21901 && fc->l_avars.dv_copyID != copyID);
21902}
21903
21904/*
21905 * Free "fc" and what it contains.
21906 */
21907 static void
21908free_funccal(fc, free_val)
21909 funccall_T *fc;
21910 int free_val; /* a: vars were allocated */
21911{
21912 listitem_T *li;
21913
21914 /* The a: variables typevals may not have been allocated, only free the
21915 * allocated variables. */
21916 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21917
21918 /* free all l: variables */
21919 vars_clear(&fc->l_vars.dv_hashtab);
21920
21921 /* Free the a:000 variables if they were allocated. */
21922 if (free_val)
21923 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21924 clear_tv(&li->li_tv);
21925
21926 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927}
21928
21929/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021930 * Add a number variable "name" to dict "dp" with value "nr".
21931 */
21932 static void
21933add_nr_var(dp, v, name, nr)
21934 dict_T *dp;
21935 dictitem_T *v;
21936 char *name;
21937 varnumber_T nr;
21938{
21939 STRCPY(v->di_key, name);
21940 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21941 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21942 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021943 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021944 v->di_tv.vval.v_number = nr;
21945}
21946
21947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 * ":return [expr]"
21949 */
21950 void
21951ex_return(eap)
21952 exarg_T *eap;
21953{
21954 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021955 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956 int returning = FALSE;
21957
21958 if (current_funccal == NULL)
21959 {
21960 EMSG(_("E133: :return not inside a function"));
21961 return;
21962 }
21963
21964 if (eap->skip)
21965 ++emsg_skip;
21966
21967 eap->nextcmd = NULL;
21968 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021969 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 {
21971 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021972 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021974 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975 }
21976 /* It's safer to return also on error. */
21977 else if (!eap->skip)
21978 {
21979 /*
21980 * Return unless the expression evaluation has been cancelled due to an
21981 * aborting error, an interrupt, or an exception.
21982 */
21983 if (!aborting())
21984 returning = do_return(eap, FALSE, TRUE, NULL);
21985 }
21986
21987 /* When skipping or the return gets pending, advance to the next command
21988 * in this line (!returning). Otherwise, ignore the rest of the line.
21989 * Following lines will be ignored by get_func_line(). */
21990 if (returning)
21991 eap->nextcmd = NULL;
21992 else if (eap->nextcmd == NULL) /* no argument */
21993 eap->nextcmd = check_nextcmd(arg);
21994
21995 if (eap->skip)
21996 --emsg_skip;
21997}
21998
21999/*
22000 * Return from a function. Possibly makes the return pending. Also called
22001 * for a pending return at the ":endtry" or after returning from an extra
22002 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022003 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022004 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005 * FALSE when the return gets pending.
22006 */
22007 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022008do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 exarg_T *eap;
22010 int reanimate;
22011 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022012 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013{
22014 int idx;
22015 struct condstack *cstack = eap->cstack;
22016
22017 if (reanimate)
22018 /* Undo the return. */
22019 current_funccal->returned = FALSE;
22020
22021 /*
22022 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22023 * not in its finally clause (which then is to be executed next) is found.
22024 * In this case, make the ":return" pending for execution at the ":endtry".
22025 * Otherwise, return normally.
22026 */
22027 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22028 if (idx >= 0)
22029 {
22030 cstack->cs_pending[idx] = CSTP_RETURN;
22031
22032 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022033 /* A pending return again gets pending. "rettv" points to an
22034 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022036 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037 else
22038 {
22039 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022040 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022042 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022044 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 {
22046 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022047 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022048 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 else
22050 EMSG(_(e_outofmem));
22051 }
22052 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022053 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054
22055 if (reanimate)
22056 {
22057 /* The pending return value could be overwritten by a ":return"
22058 * without argument in a finally clause; reset the default
22059 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022060 current_funccal->rettv->v_type = VAR_NUMBER;
22061 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 }
22063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022064 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 }
22066 else
22067 {
22068 current_funccal->returned = TRUE;
22069
22070 /* If the return is carried out now, store the return value. For
22071 * a return immediately after reanimation, the value is already
22072 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022073 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022075 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022076 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022078 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 }
22080 }
22081
22082 return idx < 0;
22083}
22084
22085/*
22086 * Free the variable with a pending return value.
22087 */
22088 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022089discard_pending_return(rettv)
22090 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091{
Bram Moolenaar33570922005-01-25 22:26:29 +000022092 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093}
22094
22095/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022096 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 * is an allocated string. Used by report_pending() for verbose messages.
22098 */
22099 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022100get_return_cmd(rettv)
22101 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022103 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022104 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022105 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022107 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022108 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022109 if (s == NULL)
22110 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022111
22112 STRCPY(IObuff, ":return ");
22113 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22114 if (STRLEN(s) + 8 >= IOSIZE)
22115 STRCPY(IObuff + IOSIZE - 4, "...");
22116 vim_free(tofree);
22117 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118}
22119
22120/*
22121 * Get next function line.
22122 * Called by do_cmdline() to get the next line.
22123 * Returns allocated string, or NULL for end of function.
22124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125 char_u *
22126get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022127 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022129 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022130{
Bram Moolenaar33570922005-01-25 22:26:29 +000022131 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022132 ufunc_T *fp = fcp->func;
22133 char_u *retval;
22134 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135
22136 /* If breakpoints have been added/deleted need to check for it. */
22137 if (fcp->dbg_tick != debug_tick)
22138 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022139 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022140 sourcing_lnum);
22141 fcp->dbg_tick = debug_tick;
22142 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022143#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022144 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022145 func_line_end(cookie);
22146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147
Bram Moolenaar05159a02005-02-26 23:04:13 +000022148 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022149 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22150 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 retval = NULL;
22152 else
22153 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022154 /* Skip NULL lines (continuation lines). */
22155 while (fcp->linenr < gap->ga_len
22156 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22157 ++fcp->linenr;
22158 if (fcp->linenr >= gap->ga_len)
22159 retval = NULL;
22160 else
22161 {
22162 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22163 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022164#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022165 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022166 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022167#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 }
22170
22171 /* Did we encounter a breakpoint? */
22172 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22173 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022174 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022176 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177 sourcing_lnum);
22178 fcp->dbg_tick = debug_tick;
22179 }
22180
22181 return retval;
22182}
22183
Bram Moolenaar05159a02005-02-26 23:04:13 +000022184#if defined(FEAT_PROFILE) || defined(PROTO)
22185/*
22186 * Called when starting to read a function line.
22187 * "sourcing_lnum" must be correct!
22188 * When skipping lines it may not actually be executed, but we won't find out
22189 * until later and we need to store the time now.
22190 */
22191 void
22192func_line_start(cookie)
22193 void *cookie;
22194{
22195 funccall_T *fcp = (funccall_T *)cookie;
22196 ufunc_T *fp = fcp->func;
22197
22198 if (fp->uf_profiling && sourcing_lnum >= 1
22199 && sourcing_lnum <= fp->uf_lines.ga_len)
22200 {
22201 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022202 /* Skip continuation lines. */
22203 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22204 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022205 fp->uf_tml_execed = FALSE;
22206 profile_start(&fp->uf_tml_start);
22207 profile_zero(&fp->uf_tml_children);
22208 profile_get_wait(&fp->uf_tml_wait);
22209 }
22210}
22211
22212/*
22213 * Called when actually executing a function line.
22214 */
22215 void
22216func_line_exec(cookie)
22217 void *cookie;
22218{
22219 funccall_T *fcp = (funccall_T *)cookie;
22220 ufunc_T *fp = fcp->func;
22221
22222 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22223 fp->uf_tml_execed = TRUE;
22224}
22225
22226/*
22227 * Called when done with a function line.
22228 */
22229 void
22230func_line_end(cookie)
22231 void *cookie;
22232{
22233 funccall_T *fcp = (funccall_T *)cookie;
22234 ufunc_T *fp = fcp->func;
22235
22236 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22237 {
22238 if (fp->uf_tml_execed)
22239 {
22240 ++fp->uf_tml_count[fp->uf_tml_idx];
22241 profile_end(&fp->uf_tml_start);
22242 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022243 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022244 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22245 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022246 }
22247 fp->uf_tml_idx = -1;
22248 }
22249}
22250#endif
22251
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252/*
22253 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022254 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 */
22256 int
22257func_has_ended(cookie)
22258 void *cookie;
22259{
Bram Moolenaar33570922005-01-25 22:26:29 +000022260 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261
22262 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22263 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022264 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265 || fcp->returned);
22266}
22267
22268/*
22269 * return TRUE if cookie indicates a function which "abort"s on errors.
22270 */
22271 int
22272func_has_abort(cookie)
22273 void *cookie;
22274{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022275 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276}
22277
22278#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22279typedef enum
22280{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022281 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22282 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22283 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284} var_flavour_T;
22285
22286static var_flavour_T var_flavour __ARGS((char_u *varname));
22287
22288 static var_flavour_T
22289var_flavour(varname)
22290 char_u *varname;
22291{
22292 char_u *p = varname;
22293
22294 if (ASCII_ISUPPER(*p))
22295 {
22296 while (*(++p))
22297 if (ASCII_ISLOWER(*p))
22298 return VAR_FLAVOUR_SESSION;
22299 return VAR_FLAVOUR_VIMINFO;
22300 }
22301 else
22302 return VAR_FLAVOUR_DEFAULT;
22303}
22304#endif
22305
22306#if defined(FEAT_VIMINFO) || defined(PROTO)
22307/*
22308 * Restore global vars that start with a capital from the viminfo file
22309 */
22310 int
22311read_viminfo_varlist(virp, writing)
22312 vir_T *virp;
22313 int writing;
22314{
22315 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022316 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022317 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318
22319 if (!writing && (find_viminfo_parameter('!') != NULL))
22320 {
22321 tab = vim_strchr(virp->vir_line + 1, '\t');
22322 if (tab != NULL)
22323 {
22324 *tab++ = '\0'; /* isolate the variable name */
22325 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022326 type = VAR_STRING;
22327#ifdef FEAT_FLOAT
22328 else if (*tab == 'F')
22329 type = VAR_FLOAT;
22330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331
22332 tab = vim_strchr(tab, '\t');
22333 if (tab != NULL)
22334 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022335 tv.v_type = type;
22336 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022337 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022339#ifdef FEAT_FLOAT
22340 else if (type == VAR_FLOAT)
22341 (void)string2float(tab + 1, &tv.vval.v_float);
22342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022344 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022345 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022346 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022347 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348 }
22349 }
22350 }
22351
22352 return viminfo_readline(virp);
22353}
22354
22355/*
22356 * Write global vars that start with a capital to the viminfo file
22357 */
22358 void
22359write_viminfo_varlist(fp)
22360 FILE *fp;
22361{
Bram Moolenaar33570922005-01-25 22:26:29 +000022362 hashitem_T *hi;
22363 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022364 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022365 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022366 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022367 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022368 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369
22370 if (find_viminfo_parameter('!') == NULL)
22371 return;
22372
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022373 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022374
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022375 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022376 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022378 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022380 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022381 this_var = HI2DI(hi);
22382 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022383 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022384 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022385 {
22386 case VAR_STRING: s = "STR"; break;
22387 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022388#ifdef FEAT_FLOAT
22389 case VAR_FLOAT: s = "FLO"; break;
22390#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022391 default: continue;
22392 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022393 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022394 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022395 if (p != NULL)
22396 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022397 vim_free(tofree);
22398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 }
22400 }
22401}
22402#endif
22403
22404#if defined(FEAT_SESSION) || defined(PROTO)
22405 int
22406store_session_globals(fd)
22407 FILE *fd;
22408{
Bram Moolenaar33570922005-01-25 22:26:29 +000022409 hashitem_T *hi;
22410 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022411 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412 char_u *p, *t;
22413
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022414 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022415 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022417 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022419 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022420 this_var = HI2DI(hi);
22421 if ((this_var->di_tv.v_type == VAR_NUMBER
22422 || this_var->di_tv.v_type == VAR_STRING)
22423 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022424 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022425 /* Escape special characters with a backslash. Turn a LF and
22426 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022427 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022428 (char_u *)"\\\"\n\r");
22429 if (p == NULL) /* out of memory */
22430 break;
22431 for (t = p; *t != NUL; ++t)
22432 if (*t == '\n')
22433 *t = 'n';
22434 else if (*t == '\r')
22435 *t = 'r';
22436 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022437 this_var->di_key,
22438 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22439 : ' ',
22440 p,
22441 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22442 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022443 || put_eol(fd) == FAIL)
22444 {
22445 vim_free(p);
22446 return FAIL;
22447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 vim_free(p);
22449 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022450#ifdef FEAT_FLOAT
22451 else if (this_var->di_tv.v_type == VAR_FLOAT
22452 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22453 {
22454 float_T f = this_var->di_tv.vval.v_float;
22455 int sign = ' ';
22456
22457 if (f < 0)
22458 {
22459 f = -f;
22460 sign = '-';
22461 }
22462 if ((fprintf(fd, "let %s = %c&%f",
22463 this_var->di_key, sign, f) < 0)
22464 || put_eol(fd) == FAIL)
22465 return FAIL;
22466 }
22467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468 }
22469 }
22470 return OK;
22471}
22472#endif
22473
Bram Moolenaar661b1822005-07-28 22:36:45 +000022474/*
22475 * Display script name where an item was last set.
22476 * Should only be invoked when 'verbose' is non-zero.
22477 */
22478 void
22479last_set_msg(scriptID)
22480 scid_T scriptID;
22481{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022482 char_u *p;
22483
Bram Moolenaar661b1822005-07-28 22:36:45 +000022484 if (scriptID != 0)
22485 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022486 p = home_replace_save(NULL, get_scriptname(scriptID));
22487 if (p != NULL)
22488 {
22489 verbose_enter();
22490 MSG_PUTS(_("\n\tLast set from "));
22491 MSG_PUTS(p);
22492 vim_free(p);
22493 verbose_leave();
22494 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022495 }
22496}
22497
Bram Moolenaard812df62008-11-09 12:46:09 +000022498/*
22499 * List v:oldfiles in a nice way.
22500 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022501 void
22502ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022503 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022504{
22505 list_T *l = vimvars[VV_OLDFILES].vv_list;
22506 listitem_T *li;
22507 int nr = 0;
22508
22509 if (l == NULL)
22510 msg((char_u *)_("No old files"));
22511 else
22512 {
22513 msg_start();
22514 msg_scroll = TRUE;
22515 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22516 {
22517 msg_outnum((long)++nr);
22518 MSG_PUTS(": ");
22519 msg_outtrans(get_tv_string(&li->li_tv));
22520 msg_putchar('\n');
22521 out_flush(); /* output one line at a time */
22522 ui_breakcheck();
22523 }
22524 /* Assume "got_int" was set to truncate the listing. */
22525 got_int = FALSE;
22526
22527#ifdef FEAT_BROWSE_CMD
22528 if (cmdmod.browse)
22529 {
22530 quit_more = FALSE;
22531 nr = prompt_for_number(FALSE);
22532 msg_starthere();
22533 if (nr > 0)
22534 {
22535 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22536 (long)nr);
22537
22538 if (p != NULL)
22539 {
22540 p = expand_env_save(p);
22541 eap->arg = p;
22542 eap->cmdidx = CMD_edit;
22543 cmdmod.browse = FALSE;
22544 do_exedit(eap, NULL);
22545 vim_free(p);
22546 }
22547 }
22548 }
22549#endif
22550 }
22551}
22552
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553#endif /* FEAT_EVAL */
22554
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022556#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557
22558#ifdef WIN3264
22559/*
22560 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22561 */
22562static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22563static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22564static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22565
22566/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022567 * Get the short path (8.3) for the filename in "fnamep".
22568 * Only works for a valid file name.
22569 * When the path gets longer "fnamep" is changed and the allocated buffer
22570 * is put in "bufp".
22571 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22572 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 */
22574 static int
22575get_short_pathname(fnamep, bufp, fnamelen)
22576 char_u **fnamep;
22577 char_u **bufp;
22578 int *fnamelen;
22579{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022580 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 char_u *newbuf;
22582
22583 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 l = GetShortPathName(*fnamep, *fnamep, len);
22585 if (l > len - 1)
22586 {
22587 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022588 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 newbuf = vim_strnsave(*fnamep, l);
22590 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022591 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592
22593 vim_free(*bufp);
22594 *fnamep = *bufp = newbuf;
22595
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022596 /* Really should always succeed, as the buffer is big enough. */
22597 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 }
22599
22600 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022601 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602}
22603
22604/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022605 * Get the short path (8.3) for the filename in "fname". The converted
22606 * path is returned in "bufp".
22607 *
22608 * Some of the directories specified in "fname" may not exist. This function
22609 * will shorten the existing directories at the beginning of the path and then
22610 * append the remaining non-existing path.
22611 *
22612 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022613 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022614 * bufp - Pointer to an allocated buffer for the filename.
22615 * fnamelen - Length of the filename pointed to by fname
22616 *
22617 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 */
22619 static int
22620shortpath_for_invalid_fname(fname, bufp, fnamelen)
22621 char_u **fname;
22622 char_u **bufp;
22623 int *fnamelen;
22624{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022625 char_u *short_fname, *save_fname, *pbuf_unused;
22626 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022628 int old_len, len;
22629 int new_len, sfx_len;
22630 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631
22632 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022633 old_len = *fnamelen;
22634 save_fname = vim_strnsave(*fname, old_len);
22635 pbuf_unused = NULL;
22636 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022638 endp = save_fname + old_len - 1; /* Find the end of the copy */
22639 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022641 /*
22642 * Try shortening the supplied path till it succeeds by removing one
22643 * directory at a time from the tail of the path.
22644 */
22645 len = 0;
22646 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022648 /* go back one path-separator */
22649 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22650 --endp;
22651 if (endp <= save_fname)
22652 break; /* processed the complete path */
22653
22654 /*
22655 * Replace the path separator with a NUL and try to shorten the
22656 * resulting path.
22657 */
22658 ch = *endp;
22659 *endp = 0;
22660 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022661 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022662 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22663 {
22664 retval = FAIL;
22665 goto theend;
22666 }
22667 *endp = ch; /* preserve the string */
22668
22669 if (len > 0)
22670 break; /* successfully shortened the path */
22671
22672 /* failed to shorten the path. Skip the path separator */
22673 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022674 }
22675
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022676 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022678 /*
22679 * Succeeded in shortening the path. Now concatenate the shortened
22680 * path with the remaining path at the tail.
22681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022683 /* Compute the length of the new path. */
22684 sfx_len = (int)(save_endp - endp) + 1;
22685 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022687 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022688 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022689 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022691 /* There is not enough space in the currently allocated string,
22692 * copy it to a buffer big enough. */
22693 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022695 {
22696 retval = FAIL;
22697 goto theend;
22698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 }
22700 else
22701 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022702 /* Transfer short_fname to the main buffer (it's big enough),
22703 * unless get_short_pathname() did its work in-place. */
22704 *fname = *bufp = save_fname;
22705 if (short_fname != save_fname)
22706 vim_strncpy(save_fname, short_fname, len);
22707 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022709
22710 /* concat the not-shortened part of the path */
22711 vim_strncpy(*fname + len, endp, sfx_len);
22712 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022714
22715theend:
22716 vim_free(pbuf_unused);
22717 vim_free(save_fname);
22718
22719 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720}
22721
22722/*
22723 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022724 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725 */
22726 static int
22727shortpath_for_partial(fnamep, bufp, fnamelen)
22728 char_u **fnamep;
22729 char_u **bufp;
22730 int *fnamelen;
22731{
22732 int sepcount, len, tflen;
22733 char_u *p;
22734 char_u *pbuf, *tfname;
22735 int hasTilde;
22736
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022737 /* Count up the path separators from the RHS.. so we know which part
22738 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022740 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741 if (vim_ispathsep(*p))
22742 ++sepcount;
22743
22744 /* Need full path first (use expand_env() to remove a "~/") */
22745 hasTilde = (**fnamep == '~');
22746 if (hasTilde)
22747 pbuf = tfname = expand_env_save(*fnamep);
22748 else
22749 pbuf = tfname = FullName_save(*fnamep, FALSE);
22750
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022751 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022753 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22754 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755
22756 if (len == 0)
22757 {
22758 /* Don't have a valid filename, so shorten the rest of the
22759 * path if we can. This CAN give us invalid 8.3 filenames, but
22760 * there's not a lot of point in guessing what it might be.
22761 */
22762 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022763 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22764 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765 }
22766
22767 /* Count the paths backward to find the beginning of the desired string. */
22768 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022769 {
22770#ifdef FEAT_MBYTE
22771 if (has_mbyte)
22772 p -= mb_head_off(tfname, p);
22773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774 if (vim_ispathsep(*p))
22775 {
22776 if (sepcount == 0 || (hasTilde && sepcount == 1))
22777 break;
22778 else
22779 sepcount --;
22780 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782 if (hasTilde)
22783 {
22784 --p;
22785 if (p >= tfname)
22786 *p = '~';
22787 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022788 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 }
22790 else
22791 ++p;
22792
22793 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22794 vim_free(*bufp);
22795 *fnamelen = (int)STRLEN(p);
22796 *bufp = pbuf;
22797 *fnamep = p;
22798
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022799 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800}
22801#endif /* WIN3264 */
22802
22803/*
22804 * Adjust a filename, according to a string of modifiers.
22805 * *fnamep must be NUL terminated when called. When returning, the length is
22806 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022807 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 * When there is an error, *fnamep is set to NULL.
22809 */
22810 int
22811modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22812 char_u *src; /* string with modifiers */
22813 int *usedlen; /* characters after src that are used */
22814 char_u **fnamep; /* file name so far */
22815 char_u **bufp; /* buffer for allocated file name or NULL */
22816 int *fnamelen; /* length of fnamep */
22817{
22818 int valid = 0;
22819 char_u *tail;
22820 char_u *s, *p, *pbuf;
22821 char_u dirname[MAXPATHL];
22822 int c;
22823 int has_fullname = 0;
22824#ifdef WIN3264
22825 int has_shortname = 0;
22826#endif
22827
22828repeat:
22829 /* ":p" - full path/file_name */
22830 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22831 {
22832 has_fullname = 1;
22833
22834 valid |= VALID_PATH;
22835 *usedlen += 2;
22836
22837 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22838 if ((*fnamep)[0] == '~'
22839#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22840 && ((*fnamep)[1] == '/'
22841# ifdef BACKSLASH_IN_FILENAME
22842 || (*fnamep)[1] == '\\'
22843# endif
22844 || (*fnamep)[1] == NUL)
22845
22846#endif
22847 )
22848 {
22849 *fnamep = expand_env_save(*fnamep);
22850 vim_free(*bufp); /* free any allocated file name */
22851 *bufp = *fnamep;
22852 if (*fnamep == NULL)
22853 return -1;
22854 }
22855
22856 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022857 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858 {
22859 if (vim_ispathsep(*p)
22860 && p[1] == '.'
22861 && (p[2] == NUL
22862 || vim_ispathsep(p[2])
22863 || (p[2] == '.'
22864 && (p[3] == NUL || vim_ispathsep(p[3])))))
22865 break;
22866 }
22867
22868 /* FullName_save() is slow, don't use it when not needed. */
22869 if (*p != NUL || !vim_isAbsName(*fnamep))
22870 {
22871 *fnamep = FullName_save(*fnamep, *p != NUL);
22872 vim_free(*bufp); /* free any allocated file name */
22873 *bufp = *fnamep;
22874 if (*fnamep == NULL)
22875 return -1;
22876 }
22877
22878 /* Append a path separator to a directory. */
22879 if (mch_isdir(*fnamep))
22880 {
22881 /* Make room for one or two extra characters. */
22882 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22883 vim_free(*bufp); /* free any allocated file name */
22884 *bufp = *fnamep;
22885 if (*fnamep == NULL)
22886 return -1;
22887 add_pathsep(*fnamep);
22888 }
22889 }
22890
22891 /* ":." - path relative to the current directory */
22892 /* ":~" - path relative to the home directory */
22893 /* ":8" - shortname path - postponed till after */
22894 while (src[*usedlen] == ':'
22895 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22896 {
22897 *usedlen += 2;
22898 if (c == '8')
22899 {
22900#ifdef WIN3264
22901 has_shortname = 1; /* Postpone this. */
22902#endif
22903 continue;
22904 }
22905 pbuf = NULL;
22906 /* Need full path first (use expand_env() to remove a "~/") */
22907 if (!has_fullname)
22908 {
22909 if (c == '.' && **fnamep == '~')
22910 p = pbuf = expand_env_save(*fnamep);
22911 else
22912 p = pbuf = FullName_save(*fnamep, FALSE);
22913 }
22914 else
22915 p = *fnamep;
22916
22917 has_fullname = 0;
22918
22919 if (p != NULL)
22920 {
22921 if (c == '.')
22922 {
22923 mch_dirname(dirname, MAXPATHL);
22924 s = shorten_fname(p, dirname);
22925 if (s != NULL)
22926 {
22927 *fnamep = s;
22928 if (pbuf != NULL)
22929 {
22930 vim_free(*bufp); /* free any allocated file name */
22931 *bufp = pbuf;
22932 pbuf = NULL;
22933 }
22934 }
22935 }
22936 else
22937 {
22938 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22939 /* Only replace it when it starts with '~' */
22940 if (*dirname == '~')
22941 {
22942 s = vim_strsave(dirname);
22943 if (s != NULL)
22944 {
22945 *fnamep = s;
22946 vim_free(*bufp);
22947 *bufp = s;
22948 }
22949 }
22950 }
22951 vim_free(pbuf);
22952 }
22953 }
22954
22955 tail = gettail(*fnamep);
22956 *fnamelen = (int)STRLEN(*fnamep);
22957
22958 /* ":h" - head, remove "/file_name", can be repeated */
22959 /* Don't remove the first "/" or "c:\" */
22960 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22961 {
22962 valid |= VALID_HEAD;
22963 *usedlen += 2;
22964 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022965 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022966 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967 *fnamelen = (int)(tail - *fnamep);
22968#ifdef VMS
22969 if (*fnamelen > 0)
22970 *fnamelen += 1; /* the path separator is part of the path */
22971#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022972 if (*fnamelen == 0)
22973 {
22974 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22975 p = vim_strsave((char_u *)".");
22976 if (p == NULL)
22977 return -1;
22978 vim_free(*bufp);
22979 *bufp = *fnamep = tail = p;
22980 *fnamelen = 1;
22981 }
22982 else
22983 {
22984 while (tail > s && !after_pathsep(s, tail))
22985 mb_ptr_back(*fnamep, tail);
22986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 }
22988
22989 /* ":8" - shortname */
22990 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22991 {
22992 *usedlen += 2;
22993#ifdef WIN3264
22994 has_shortname = 1;
22995#endif
22996 }
22997
22998#ifdef WIN3264
22999 /* Check shortname after we have done 'heads' and before we do 'tails'
23000 */
23001 if (has_shortname)
23002 {
23003 pbuf = NULL;
23004 /* Copy the string if it is shortened by :h */
23005 if (*fnamelen < (int)STRLEN(*fnamep))
23006 {
23007 p = vim_strnsave(*fnamep, *fnamelen);
23008 if (p == 0)
23009 return -1;
23010 vim_free(*bufp);
23011 *bufp = *fnamep = p;
23012 }
23013
23014 /* Split into two implementations - makes it easier. First is where
23015 * there isn't a full name already, second is where there is.
23016 */
23017 if (!has_fullname && !vim_isAbsName(*fnamep))
23018 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023019 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020 return -1;
23021 }
23022 else
23023 {
23024 int l;
23025
23026 /* Simple case, already have the full-name
23027 * Nearly always shorter, so try first time. */
23028 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023029 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 return -1;
23031
23032 if (l == 0)
23033 {
23034 /* Couldn't find the filename.. search the paths.
23035 */
23036 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023037 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 return -1;
23039 }
23040 *fnamelen = l;
23041 }
23042 }
23043#endif /* WIN3264 */
23044
23045 /* ":t" - tail, just the basename */
23046 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23047 {
23048 *usedlen += 2;
23049 *fnamelen -= (int)(tail - *fnamep);
23050 *fnamep = tail;
23051 }
23052
23053 /* ":e" - extension, can be repeated */
23054 /* ":r" - root, without extension, can be repeated */
23055 while (src[*usedlen] == ':'
23056 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23057 {
23058 /* find a '.' in the tail:
23059 * - for second :e: before the current fname
23060 * - otherwise: The last '.'
23061 */
23062 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23063 s = *fnamep - 2;
23064 else
23065 s = *fnamep + *fnamelen - 1;
23066 for ( ; s > tail; --s)
23067 if (s[0] == '.')
23068 break;
23069 if (src[*usedlen + 1] == 'e') /* :e */
23070 {
23071 if (s > tail)
23072 {
23073 *fnamelen += (int)(*fnamep - (s + 1));
23074 *fnamep = s + 1;
23075#ifdef VMS
23076 /* cut version from the extension */
23077 s = *fnamep + *fnamelen - 1;
23078 for ( ; s > *fnamep; --s)
23079 if (s[0] == ';')
23080 break;
23081 if (s > *fnamep)
23082 *fnamelen = s - *fnamep;
23083#endif
23084 }
23085 else if (*fnamep <= tail)
23086 *fnamelen = 0;
23087 }
23088 else /* :r */
23089 {
23090 if (s > tail) /* remove one extension */
23091 *fnamelen = (int)(s - *fnamep);
23092 }
23093 *usedlen += 2;
23094 }
23095
23096 /* ":s?pat?foo?" - substitute */
23097 /* ":gs?pat?foo?" - global substitute */
23098 if (src[*usedlen] == ':'
23099 && (src[*usedlen + 1] == 's'
23100 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23101 {
23102 char_u *str;
23103 char_u *pat;
23104 char_u *sub;
23105 int sep;
23106 char_u *flags;
23107 int didit = FALSE;
23108
23109 flags = (char_u *)"";
23110 s = src + *usedlen + 2;
23111 if (src[*usedlen + 1] == 'g')
23112 {
23113 flags = (char_u *)"g";
23114 ++s;
23115 }
23116
23117 sep = *s++;
23118 if (sep)
23119 {
23120 /* find end of pattern */
23121 p = vim_strchr(s, sep);
23122 if (p != NULL)
23123 {
23124 pat = vim_strnsave(s, (int)(p - s));
23125 if (pat != NULL)
23126 {
23127 s = p + 1;
23128 /* find end of substitution */
23129 p = vim_strchr(s, sep);
23130 if (p != NULL)
23131 {
23132 sub = vim_strnsave(s, (int)(p - s));
23133 str = vim_strnsave(*fnamep, *fnamelen);
23134 if (sub != NULL && str != NULL)
23135 {
23136 *usedlen = (int)(p + 1 - src);
23137 s = do_string_sub(str, pat, sub, flags);
23138 if (s != NULL)
23139 {
23140 *fnamep = s;
23141 *fnamelen = (int)STRLEN(s);
23142 vim_free(*bufp);
23143 *bufp = s;
23144 didit = TRUE;
23145 }
23146 }
23147 vim_free(sub);
23148 vim_free(str);
23149 }
23150 vim_free(pat);
23151 }
23152 }
23153 /* after using ":s", repeat all the modifiers */
23154 if (didit)
23155 goto repeat;
23156 }
23157 }
23158
23159 return valid;
23160}
23161
23162/*
23163 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23164 * "flags" can be "g" to do a global substitute.
23165 * Returns an allocated string, NULL for error.
23166 */
23167 char_u *
23168do_string_sub(str, pat, sub, flags)
23169 char_u *str;
23170 char_u *pat;
23171 char_u *sub;
23172 char_u *flags;
23173{
23174 int sublen;
23175 regmatch_T regmatch;
23176 int i;
23177 int do_all;
23178 char_u *tail;
23179 garray_T ga;
23180 char_u *ret;
23181 char_u *save_cpo;
23182
23183 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23184 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023185 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186
23187 ga_init2(&ga, 1, 200);
23188
23189 do_all = (flags[0] == 'g');
23190
23191 regmatch.rm_ic = p_ic;
23192 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23193 if (regmatch.regprog != NULL)
23194 {
23195 tail = str;
23196 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23197 {
23198 /*
23199 * Get some space for a temporary buffer to do the substitution
23200 * into. It will contain:
23201 * - The text up to where the match is.
23202 * - The substituted text.
23203 * - The text after the match.
23204 */
23205 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23206 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23207 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23208 {
23209 ga_clear(&ga);
23210 break;
23211 }
23212
23213 /* copy the text up to where the match is */
23214 i = (int)(regmatch.startp[0] - tail);
23215 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23216 /* add the substituted text */
23217 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23218 + ga.ga_len + i, TRUE, TRUE, FALSE);
23219 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220 /* avoid getting stuck on a match with an empty string */
23221 if (tail == regmatch.endp[0])
23222 {
23223 if (*tail == NUL)
23224 break;
23225 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23226 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227 }
23228 else
23229 {
23230 tail = regmatch.endp[0];
23231 if (*tail == NUL)
23232 break;
23233 }
23234 if (!do_all)
23235 break;
23236 }
23237
23238 if (ga.ga_data != NULL)
23239 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23240
23241 vim_free(regmatch.regprog);
23242 }
23243
23244 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23245 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023246 if (p_cpo == empty_option)
23247 p_cpo = save_cpo;
23248 else
23249 /* Darn, evaluating {sub} expression changed the value. */
23250 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023251
23252 return ret;
23253}
23254
23255#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */