blob: 082324dbe2d8a445a256f611f947101ee1c38e6a [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
148static 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));
436static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000437static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
439static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
440static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000441static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *list2string __ARGS((typval_T *tv, int copyID));
444static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000445static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000446static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
447static void set_ref_in_list __ARGS((list_T *l, int copyID));
448static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
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_alloc __ARGS((char_u *key));
452static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
453static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
454static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000455static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static int dict_add __ARGS((dict_T *d, dictitem_T *item));
457static long dict_len __ARGS((dict_T *d));
458static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000459static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000461static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
462static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
465static int string2float __ARGS((char_u *text, float_T *value));
466#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000467static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
468static int find_internal_func __ARGS((char_u *name));
469static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
470static 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));
471static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000472static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000473static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000474
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#ifdef FEAT_FLOAT
476static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
477#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000478static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#ifdef FEAT_FLOAT
484static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
485#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#ifdef FEAT_FLOAT
498static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
499#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000500static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000503static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000505#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000506static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
509#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512#ifdef FEAT_FLOAT
513static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
514#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));
529static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000531static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000537#ifdef FEAT_FLOAT
538static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
540#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000541static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000550static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000552static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000558static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000566static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000567static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000568static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000569static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000572static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000580static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000594static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000600static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000612#ifdef FEAT_FLOAT
613static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
614#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000620static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000621static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000623static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000627#ifdef vim_mkdir
628static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000633static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000634#ifdef FEAT_FLOAT
635static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000638static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000639static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000641static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000642static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#ifdef FEAT_FLOAT
655static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000658static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000660static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000667static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000668static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000669static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000670static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000672static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000674static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000676#ifdef FEAT_FLOAT
677static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
678#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000680static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000681static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000684#ifdef FEAT_FLOAT
685static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
687#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000688static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689#ifdef HAVE_STRFTIME
690static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
691#endif
692static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
693static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
699static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000703static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000705static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000706static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000707static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000708static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000709static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000711static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000712static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000715#ifdef FEAT_FLOAT
716static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
717#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000718static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
727static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000728static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000731static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000732
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000733static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000734static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static int get_env_len __ARGS((char_u **arg));
736static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000737static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000738static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
739#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
740#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
741 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000742static 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 +0000743static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000744static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000745static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
746static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static typval_T *alloc_tv __ARGS((void));
748static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void init_tv __ARGS((typval_T *varp));
750static long get_tv_number __ARGS((typval_T *varp));
751static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000752static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static char_u *get_tv_string __ARGS((typval_T *varp));
754static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000755static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000757static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
759static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
760static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000761static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
762static 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 +0000763static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
764static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000765static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000766static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000768static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
770static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
771static int eval_fname_script __ARGS((char_u *p));
772static int eval_fname_sid __ARGS((char_u *p));
773static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static ufunc_T *find_func __ARGS((char_u *name));
775static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000776static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777#ifdef FEAT_PROFILE
778static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000779static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
780static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
781static int
782# ifdef __BORLANDC__
783 _RTLENTRYF
784# endif
785 prof_total_cmp __ARGS((const void *s1, const void *s2));
786static int
787# ifdef __BORLANDC__
788 _RTLENTRYF
789# endif
790 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000791#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000792static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000793static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000794static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static void func_free __ARGS((ufunc_T *fp));
796static void func_unref __ARGS((char_u *name));
797static void func_ref __ARGS((char_u *name));
798static 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 +0000799static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
800static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000802static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
803static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000804static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000805static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000806static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000807
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000808/* Character used as separated in autoload function/variable names. */
809#define AUTOLOAD_CHAR '#'
810
Bram Moolenaar33570922005-01-25 22:26:29 +0000811/*
812 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000813 */
814 void
815eval_init()
816{
Bram Moolenaar33570922005-01-25 22:26:29 +0000817 int i;
818 struct vimvar *p;
819
820 init_var_dict(&globvardict, &globvars_var);
821 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000822 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000823 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000824
825 for (i = 0; i < VV_LEN; ++i)
826 {
827 p = &vimvars[i];
828 STRCPY(p->vv_di.di_key, p->vv_name);
829 if (p->vv_flags & VV_RO)
830 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
831 else if (p->vv_flags & VV_RO_SBX)
832 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
833 else
834 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000835
836 /* add to v: scope dict, unless the value is not always available */
837 if (p->vv_type != VAR_UNKNOWN)
838 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000840 /* add to compat scope dict */
841 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000842 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000843 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaara7043832005-01-21 11:56:39 +0000844}
845
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000846#if defined(EXITFREE) || defined(PROTO)
847 void
848eval_clear()
849{
850 int i;
851 struct vimvar *p;
852
853 for (i = 0; i < VV_LEN; ++i)
854 {
855 p = &vimvars[i];
856 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000857 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000858 vim_free(p->vv_str);
859 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000860 }
861 else if (p->vv_di.di_tv.v_type == VAR_LIST)
862 {
863 list_unref(p->vv_list);
864 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000865 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000866 }
867 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000868 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000869 hash_clear(&compat_hashtab);
870
871 /* script-local variables */
872 for (i = 1; i <= ga_scripts.ga_len; ++i)
873 vars_clear(&SCRIPT_VARS(i));
874 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000875 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000876
877 /* global variables */
878 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000879
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000880 /* autoloaded script names */
881 ga_clear_strings(&ga_loaded);
882
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000883 /* unreferenced lists and dicts */
884 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000885
886 /* functions */
887 free_all_functions();
888 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000889}
890#endif
891
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 * Return the name of the executed function.
894 */
895 char_u *
896func_name(cookie)
897 void *cookie;
898{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000899 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900}
901
902/*
903 * Return the address holding the next breakpoint line for a funccall cookie.
904 */
905 linenr_T *
906func_breakpoint(cookie)
907 void *cookie;
908{
Bram Moolenaar33570922005-01-25 22:26:29 +0000909 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910}
911
912/*
913 * Return the address holding the debug tick for a funccall cookie.
914 */
915 int *
916func_dbg_tick(cookie)
917 void *cookie;
918{
Bram Moolenaar33570922005-01-25 22:26:29 +0000919 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920}
921
922/*
923 * Return the nesting level for a funccall cookie.
924 */
925 int
926func_level(cookie)
927 void *cookie;
928{
Bram Moolenaar33570922005-01-25 22:26:29 +0000929 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930}
931
932/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000933funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000935/* pointer to list of previously used funccal, still around because some
936 * item in it is still being used. */
937funccall_T *previous_funccal = NULL;
938
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939/*
940 * Return TRUE when a function was ended by a ":return" command.
941 */
942 int
943current_func_returned()
944{
945 return current_funccal->returned;
946}
947
948
949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 * Set an internal variable to a string value. Creates the variable if it does
951 * not already exist.
952 */
953 void
954set_internal_string_var(name, value)
955 char_u *name;
956 char_u *value;
957{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000958 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000959 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 val = vim_strsave(value);
962 if (val != NULL)
963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000964 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000965 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000967 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000968 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
970 }
971}
972
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000973static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000974static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000975static char_u *redir_endp = NULL;
976static char_u *redir_varname = NULL;
977
978/*
979 * Start recording command output to a variable
980 * Returns OK if successfully completed the setup. FAIL otherwise.
981 */
982 int
983var_redir_start(name, append)
984 char_u *name;
985 int append; /* append to an existing variable */
986{
987 int save_emsg;
988 int err;
989 typval_T tv;
990
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000991 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000992 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000993 {
994 EMSG(_(e_invarg));
995 return FAIL;
996 }
997
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000998 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999 redir_varname = vim_strsave(name);
1000 if (redir_varname == NULL)
1001 return FAIL;
1002
1003 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1004 if (redir_lval == NULL)
1005 {
1006 var_redir_stop();
1007 return FAIL;
1008 }
1009
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001010 /* The output is stored in growarray "redir_ga" until redirection ends. */
1011 ga_init2(&redir_ga, (int)sizeof(char), 500);
1012
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001013 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001014 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1015 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1017 {
1018 if (redir_endp != NULL && *redir_endp != NUL)
1019 /* Trailing characters are present after the variable name */
1020 EMSG(_(e_trailing));
1021 else
1022 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001023 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001024 var_redir_stop();
1025 return FAIL;
1026 }
1027
1028 /* check if we can write to the variable: set it to or append an empty
1029 * string */
1030 save_emsg = did_emsg;
1031 did_emsg = FALSE;
1032 tv.v_type = VAR_STRING;
1033 tv.vval.v_string = (char_u *)"";
1034 if (append)
1035 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1036 else
1037 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1038 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001039 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 if (err)
1041 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001042 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 var_redir_stop();
1044 return FAIL;
1045 }
1046 if (redir_lval->ll_newkey != NULL)
1047 {
1048 /* Dictionary item was created, don't do it again. */
1049 vim_free(redir_lval->ll_newkey);
1050 redir_lval->ll_newkey = NULL;
1051 }
1052
1053 return OK;
1054}
1055
1056/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001057 * Append "value[value_len]" to the variable set by var_redir_start().
1058 * The actual appending is postponed until redirection ends, because the value
1059 * appended may in fact be the string we write to, changing it may cause freed
1060 * memory to be used:
1061 * :redir => foo
1062 * :let foo
1063 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 */
1065 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001066var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001068 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001070 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071
1072 if (redir_lval == NULL)
1073 return;
1074
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001075 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001076 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001077 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001078 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001080 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001081 {
1082 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001083 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001084 }
1085 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087}
1088
1089/*
1090 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001091 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 */
1093 void
1094var_redir_stop()
1095{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096 typval_T tv;
1097
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 if (redir_lval != NULL)
1099 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 /* If there was no error: assign the text to the variable. */
1101 if (redir_endp != NULL)
1102 {
1103 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1104 tv.v_type = VAR_STRING;
1105 tv.vval.v_string = redir_ga.ga_data;
1106 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1107 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001109 /* free the collected output */
1110 vim_free(redir_ga.ga_data);
1111 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 clear_lval(redir_lval);
1114 vim_free(redir_lval);
1115 redir_lval = NULL;
1116 }
1117 vim_free(redir_varname);
1118 redir_varname = NULL;
1119}
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121# if defined(FEAT_MBYTE) || defined(PROTO)
1122 int
1123eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1124 char_u *enc_from;
1125 char_u *enc_to;
1126 char_u *fname_from;
1127 char_u *fname_to;
1128{
1129 int err = FALSE;
1130
1131 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1132 set_vim_var_string(VV_CC_TO, enc_to, -1);
1133 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1134 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1135 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1136 err = TRUE;
1137 set_vim_var_string(VV_CC_FROM, NULL, -1);
1138 set_vim_var_string(VV_CC_TO, NULL, -1);
1139 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1140 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1141
1142 if (err)
1143 return FAIL;
1144 return OK;
1145}
1146# endif
1147
1148# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1149 int
1150eval_printexpr(fname, args)
1151 char_u *fname;
1152 char_u *args;
1153{
1154 int err = FALSE;
1155
1156 set_vim_var_string(VV_FNAME_IN, fname, -1);
1157 set_vim_var_string(VV_CMDARG, args, -1);
1158 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1159 err = TRUE;
1160 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1161 set_vim_var_string(VV_CMDARG, NULL, -1);
1162
1163 if (err)
1164 {
1165 mch_remove(fname);
1166 return FAIL;
1167 }
1168 return OK;
1169}
1170# endif
1171
1172# if defined(FEAT_DIFF) || defined(PROTO)
1173 void
1174eval_diff(origfile, newfile, outfile)
1175 char_u *origfile;
1176 char_u *newfile;
1177 char_u *outfile;
1178{
1179 int err = FALSE;
1180
1181 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1182 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1183 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1184 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1185 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1186 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1187 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1188}
1189
1190 void
1191eval_patch(origfile, difffile, outfile)
1192 char_u *origfile;
1193 char_u *difffile;
1194 char_u *outfile;
1195{
1196 int err;
1197
1198 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1199 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1200 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1201 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1202 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1203 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1204 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1205}
1206# endif
1207
1208/*
1209 * Top level evaluation function, returning a boolean.
1210 * Sets "error" to TRUE if there was an error.
1211 * Return TRUE or FALSE.
1212 */
1213 int
1214eval_to_bool(arg, error, nextcmd, skip)
1215 char_u *arg;
1216 int *error;
1217 char_u **nextcmd;
1218 int skip; /* only parse, don't execute */
1219{
Bram Moolenaar33570922005-01-25 22:26:29 +00001220 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 int retval = FALSE;
1222
1223 if (skip)
1224 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001225 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 else
1228 {
1229 *error = FALSE;
1230 if (!skip)
1231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001232 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 }
1235 }
1236 if (skip)
1237 --emsg_skip;
1238
1239 return retval;
1240}
1241
1242/*
1243 * Top level evaluation function, returning a string. If "skip" is TRUE,
1244 * only parsing to "nextcmd" is done, without reporting errors. Return
1245 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1246 */
1247 char_u *
1248eval_to_string_skip(arg, nextcmd, skip)
1249 char_u *arg;
1250 char_u **nextcmd;
1251 int skip; /* only parse, don't execute */
1252{
Bram Moolenaar33570922005-01-25 22:26:29 +00001253 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 char_u *retval;
1255
1256 if (skip)
1257 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001258 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 retval = NULL;
1260 else
1261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001262 retval = vim_strsave(get_tv_string(&tv));
1263 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265 if (skip)
1266 --emsg_skip;
1267
1268 return retval;
1269}
1270
1271/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001272 * Skip over an expression at "*pp".
1273 * Return FAIL for an error, OK otherwise.
1274 */
1275 int
1276skip_expr(pp)
1277 char_u **pp;
1278{
Bram Moolenaar33570922005-01-25 22:26:29 +00001279 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001280
1281 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001283}
1284
1285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001287 * When "convert" is TRUE convert a List into a sequence of lines and convert
1288 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 * Return pointer to allocated memory, or NULL for failure.
1290 */
1291 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001292eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 char_u *arg;
1294 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001295 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001299 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001300#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001301 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 retval = NULL;
1306 else
1307 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001308 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001309 {
1310 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001311 if (tv.vval.v_list != NULL)
1312 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001313 ga_append(&ga, NUL);
1314 retval = (char_u *)ga.ga_data;
1315 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001316#ifdef FEAT_FLOAT
1317 else if (convert && tv.v_type == VAR_FLOAT)
1318 {
1319 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1320 retval = vim_strsave(numbuf);
1321 }
1322#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001323 else
1324 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001325 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
1327
1328 return retval;
1329}
1330
1331/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001332 * Call eval_to_string() without using current local variables and using
1333 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 */
1335 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001336eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *arg;
1338 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001339 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
1341 char_u *retval;
1342 void *save_funccalp;
1343
1344 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001345 if (use_sandbox)
1346 ++sandbox;
1347 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001348 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001349 if (use_sandbox)
1350 --sandbox;
1351 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 restore_funccal(save_funccalp);
1353 return retval;
1354}
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356/*
1357 * Top level evaluation function, returning a number.
1358 * Evaluates "expr" silently.
1359 * Returns -1 for an error.
1360 */
1361 int
1362eval_to_number(expr)
1363 char_u *expr;
1364{
Bram Moolenaar33570922005-01-25 22:26:29 +00001365 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001367 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 ++emsg_off;
1370
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 retval = -1;
1373 else
1374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001375 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
1378 --emsg_off;
1379
1380 return retval;
1381}
1382
Bram Moolenaara40058a2005-07-11 22:42:07 +00001383/*
1384 * Prepare v: variable "idx" to be used.
1385 * Save the current typeval in "save_tv".
1386 * When not used yet add the variable to the v: hashtable.
1387 */
1388 static void
1389prepare_vimvar(idx, save_tv)
1390 int idx;
1391 typval_T *save_tv;
1392{
1393 *save_tv = vimvars[idx].vv_tv;
1394 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1395 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1396}
1397
1398/*
1399 * Restore v: variable "idx" to typeval "save_tv".
1400 * When no longer defined, remove the variable from the v: hashtable.
1401 */
1402 static void
1403restore_vimvar(idx, save_tv)
1404 int idx;
1405 typval_T *save_tv;
1406{
1407 hashitem_T *hi;
1408
Bram Moolenaara40058a2005-07-11 22:42:07 +00001409 vimvars[idx].vv_tv = *save_tv;
1410 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1411 {
1412 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1413 if (HASHITEM_EMPTY(hi))
1414 EMSG2(_(e_intern2), "restore_vimvar()");
1415 else
1416 hash_remove(&vimvarht, hi);
1417 }
1418}
1419
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001420#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001421/*
1422 * Evaluate an expression to a list with suggestions.
1423 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001424 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001425 */
1426 list_T *
1427eval_spell_expr(badword, expr)
1428 char_u *badword;
1429 char_u *expr;
1430{
1431 typval_T save_val;
1432 typval_T rettv;
1433 list_T *list = NULL;
1434 char_u *p = skipwhite(expr);
1435
1436 /* Set "v:val" to the bad word. */
1437 prepare_vimvar(VV_VAL, &save_val);
1438 vimvars[VV_VAL].vv_type = VAR_STRING;
1439 vimvars[VV_VAL].vv_str = badword;
1440 if (p_verbose == 0)
1441 ++emsg_off;
1442
1443 if (eval1(&p, &rettv, TRUE) == OK)
1444 {
1445 if (rettv.v_type != VAR_LIST)
1446 clear_tv(&rettv);
1447 else
1448 list = rettv.vval.v_list;
1449 }
1450
1451 if (p_verbose == 0)
1452 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001453 restore_vimvar(VV_VAL, &save_val);
1454
1455 return list;
1456}
1457
1458/*
1459 * "list" is supposed to contain two items: a word and a number. Return the
1460 * word in "pp" and the number as the return value.
1461 * Return -1 if anything isn't right.
1462 * Used to get the good word and score from the eval_spell_expr() result.
1463 */
1464 int
1465get_spellword(list, pp)
1466 list_T *list;
1467 char_u **pp;
1468{
1469 listitem_T *li;
1470
1471 li = list->lv_first;
1472 if (li == NULL)
1473 return -1;
1474 *pp = get_tv_string(&li->li_tv);
1475
1476 li = li->li_next;
1477 if (li == NULL)
1478 return -1;
1479 return get_tv_number(&li->li_tv);
1480}
1481#endif
1482
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001483/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001484 * Top level evaluation function.
1485 * Returns an allocated typval_T with the result.
1486 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001487 */
1488 typval_T *
1489eval_expr(arg, nextcmd)
1490 char_u *arg;
1491 char_u **nextcmd;
1492{
1493 typval_T *tv;
1494
1495 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001496 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001497 {
1498 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001499 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001500 }
1501
1502 return tv;
1503}
1504
1505
Bram Moolenaar4f688582007-07-24 12:34:30 +00001506#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1507 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001509 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001510 * Uses argv[argc] for the function arguments. Only Number and String
1511 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001512 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001514 static int
1515call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 char_u *func;
1517 int argc;
1518 char_u **argv;
1519 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
Bram Moolenaar33570922005-01-25 22:26:29 +00001522 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 long n;
1524 int len;
1525 int i;
1526 int doesrange;
1527 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001528 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001530 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001532 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
1534 for (i = 0; i < argc; i++)
1535 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001536 /* Pass a NULL or empty argument as an empty string */
1537 if (argv[i] == NULL || *argv[i] == NUL)
1538 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001539 argvars[i].v_type = VAR_STRING;
1540 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001541 continue;
1542 }
1543
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 /* Recognize a number argument, the others must be strings. */
1545 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1546 if (len != 0 && len == (int)STRLEN(argv[i]))
1547 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001548 argvars[i].v_type = VAR_NUMBER;
1549 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 }
1551 else
1552 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001553 argvars[i].v_type = VAR_STRING;
1554 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 }
1556 }
1557
1558 if (safe)
1559 {
1560 save_funccalp = save_funccal();
1561 ++sandbox;
1562 }
1563
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001564 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1565 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (safe)
1569 {
1570 --sandbox;
1571 restore_funccal(save_funccalp);
1572 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001573 vim_free(argvars);
1574
1575 if (ret == FAIL)
1576 clear_tv(rettv);
1577
1578 return ret;
1579}
1580
Bram Moolenaar4f688582007-07-24 12:34:30 +00001581# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001582/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001583 * Call vimL function "func" and return the result as a string.
1584 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001585 * Uses argv[argc] for the function arguments.
1586 */
1587 void *
1588call_func_retstr(func, argc, argv, safe)
1589 char_u *func;
1590 int argc;
1591 char_u **argv;
1592 int safe; /* use the sandbox */
1593{
1594 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001595 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001596
1597 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1598 return NULL;
1599
1600 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001601 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 return retval;
1603}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001604# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001605
Bram Moolenaar4f688582007-07-24 12:34:30 +00001606# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001607/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001608 * Call vimL function "func" and return the result as a number.
1609 * Returns -1 when calling the function fails.
1610 * Uses argv[argc] for the function arguments.
1611 */
1612 long
1613call_func_retnr(func, argc, argv, safe)
1614 char_u *func;
1615 int argc;
1616 char_u **argv;
1617 int safe; /* use the sandbox */
1618{
1619 typval_T rettv;
1620 long retval;
1621
1622 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1623 return -1;
1624
1625 retval = get_tv_number_chk(&rettv, NULL);
1626 clear_tv(&rettv);
1627 return retval;
1628}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001629# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001630
1631/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001632 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001634 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001635 */
1636 void *
1637call_func_retlist(func, argc, argv, safe)
1638 char_u *func;
1639 int argc;
1640 char_u **argv;
1641 int safe; /* use the sandbox */
1642{
1643 typval_T rettv;
1644
1645 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1646 return NULL;
1647
1648 if (rettv.v_type != VAR_LIST)
1649 {
1650 clear_tv(&rettv);
1651 return NULL;
1652 }
1653
1654 return rettv.vval.v_list;
1655}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656#endif
1657
Bram Moolenaar4f688582007-07-24 12:34:30 +00001658
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659/*
1660 * Save the current function call pointer, and set it to NULL.
1661 * Used when executing autocommands and for ":source".
1662 */
1663 void *
1664save_funccal()
1665{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001666 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 current_funccal = NULL;
1669 return (void *)fc;
1670}
1671
1672 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001673restore_funccal(vfc)
1674 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001676 funccall_T *fc = (funccall_T *)vfc;
1677
1678 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679}
1680
Bram Moolenaar05159a02005-02-26 23:04:13 +00001681#if defined(FEAT_PROFILE) || defined(PROTO)
1682/*
1683 * Prepare profiling for entering a child or something else that is not
1684 * counted for the script/function itself.
1685 * Should always be called in pair with prof_child_exit().
1686 */
1687 void
1688prof_child_enter(tm)
1689 proftime_T *tm; /* place to store waittime */
1690{
1691 funccall_T *fc = current_funccal;
1692
1693 if (fc != NULL && fc->func->uf_profiling)
1694 profile_start(&fc->prof_child);
1695 script_prof_save(tm);
1696}
1697
1698/*
1699 * Take care of time spent in a child.
1700 * Should always be called after prof_child_enter().
1701 */
1702 void
1703prof_child_exit(tm)
1704 proftime_T *tm; /* where waittime was stored */
1705{
1706 funccall_T *fc = current_funccal;
1707
1708 if (fc != NULL && fc->func->uf_profiling)
1709 {
1710 profile_end(&fc->prof_child);
1711 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1712 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1713 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1714 }
1715 script_prof_restore(tm);
1716}
1717#endif
1718
1719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#ifdef FEAT_FOLDING
1721/*
1722 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1723 * it in "*cp". Doesn't give error messages.
1724 */
1725 int
1726eval_foldexpr(arg, cp)
1727 char_u *arg;
1728 int *cp;
1729{
Bram Moolenaar33570922005-01-25 22:26:29 +00001730 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 int retval;
1732 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001733 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1734 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
1736 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001737 if (use_sandbox)
1738 ++sandbox;
1739 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001741 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 retval = 0;
1743 else
1744 {
1745 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746 if (tv.v_type == VAR_NUMBER)
1747 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001748 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 retval = 0;
1750 else
1751 {
1752 /* If the result is a string, check if there is a non-digit before
1753 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001754 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (!VIM_ISDIGIT(*s) && *s != '-')
1756 *cp = *s++;
1757 retval = atol((char *)s);
1758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001762 if (use_sandbox)
1763 --sandbox;
1764 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
1766 return retval;
1767}
1768#endif
1769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001771 * ":let" list all variable values
1772 * ":let var1 var2" list variable values
1773 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001774 * ":let var += expr" assignment command.
1775 * ":let var -= expr" assignment command.
1776 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 */
1779 void
1780ex_let(eap)
1781 exarg_T *eap;
1782{
1783 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001785 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 int var_count = 0;
1788 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001789 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001790 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001791 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
Bram Moolenaardb552d602006-03-23 22:59:57 +00001793 argend = skip_var_list(arg, &var_count, &semicolon);
1794 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001796 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1797 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001798 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001799 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001801 /*
1802 * ":let" without "=": list variables
1803 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 if (*arg == '[')
1805 EMSG(_(e_invarg));
1806 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001808 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001810 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001812 list_glob_vars(&first);
1813 list_buf_vars(&first);
1814 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001815#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001816 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001817#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001818 list_script_vars(&first);
1819 list_func_vars(&first);
1820 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 eap->nextcmd = check_nextcmd(arg);
1823 }
1824 else
1825 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 op[0] = '=';
1827 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001828 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001829 {
1830 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1831 op[0] = expr[-1]; /* +=, -= or .= */
1832 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001833 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 if (eap->skip)
1836 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if (eap->skip)
1839 {
1840 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 --emsg_skip;
1843 }
1844 else if (i != FAIL)
1845 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850 }
1851}
1852
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853/*
1854 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1855 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001856 * When "nextchars" is not NULL it points to a string with characters that
1857 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1858 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 * Returns OK or FAIL;
1860 */
1861 static int
1862ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1863 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001864 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 int copy; /* copy values from "tv", don't move */
1866 int semicolon; /* from skip_var_list() */
1867 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001868 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869{
1870 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001871 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001873 listitem_T *item;
1874 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875
1876 if (*arg != '[')
1877 {
1878 /*
1879 * ":let var = expr" or ":for var in list"
1880 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882 return FAIL;
1883 return OK;
1884 }
1885
1886 /*
1887 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1888 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001889 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 {
1891 EMSG(_(e_listreq));
1892 return FAIL;
1893 }
1894
1895 i = list_len(l);
1896 if (semicolon == 0 && var_count < i)
1897 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001898 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 return FAIL;
1900 }
1901 if (var_count - semicolon > i)
1902 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001903 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 return FAIL;
1905 }
1906
1907 item = l->lv_first;
1908 while (*arg != ']')
1909 {
1910 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001911 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 item = item->li_next;
1913 if (arg == NULL)
1914 return FAIL;
1915
1916 arg = skipwhite(arg);
1917 if (*arg == ';')
1918 {
1919 /* Put the rest of the list (may be empty) in the var after ';'.
1920 * Create a new list for this. */
1921 l = list_alloc();
1922 if (l == NULL)
1923 return FAIL;
1924 while (item != NULL)
1925 {
1926 list_append_tv(l, &item->li_tv);
1927 item = item->li_next;
1928 }
1929
1930 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001931 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 ltv.vval.v_list = l;
1933 l->lv_refcount = 1;
1934
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1936 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 clear_tv(&ltv);
1938 if (arg == NULL)
1939 return FAIL;
1940 break;
1941 }
1942 else if (*arg != ',' && *arg != ']')
1943 {
1944 EMSG2(_(e_intern2), "ex_let_vars()");
1945 return FAIL;
1946 }
1947 }
1948
1949 return OK;
1950}
1951
1952/*
1953 * Skip over assignable variable "var" or list of variables "[var, var]".
1954 * Used for ":let varvar = expr" and ":for varvar in expr".
1955 * For "[var, var]" increment "*var_count" for each variable.
1956 * for "[var, var; var]" set "semicolon".
1957 * Return NULL for an error.
1958 */
1959 static char_u *
1960skip_var_list(arg, var_count, semicolon)
1961 char_u *arg;
1962 int *var_count;
1963 int *semicolon;
1964{
1965 char_u *p, *s;
1966
1967 if (*arg == '[')
1968 {
1969 /* "[var, var]": find the matching ']'. */
1970 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001971 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 {
1973 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1974 s = skip_var_one(p);
1975 if (s == p)
1976 {
1977 EMSG2(_(e_invarg2), p);
1978 return NULL;
1979 }
1980 ++*var_count;
1981
1982 p = skipwhite(s);
1983 if (*p == ']')
1984 break;
1985 else if (*p == ';')
1986 {
1987 if (*semicolon == 1)
1988 {
1989 EMSG(_("Double ; in list of variables"));
1990 return NULL;
1991 }
1992 *semicolon = 1;
1993 }
1994 else if (*p != ',')
1995 {
1996 EMSG2(_(e_invarg2), p);
1997 return NULL;
1998 }
1999 }
2000 return p + 1;
2001 }
2002 else
2003 return skip_var_one(arg);
2004}
2005
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002006/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002007 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002008 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002009 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 static char_u *
2011skip_var_one(arg)
2012 char_u *arg;
2013{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002014 if (*arg == '@' && arg[1] != NUL)
2015 return arg + 2;
2016 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2017 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018}
2019
Bram Moolenaara7043832005-01-21 11:56:39 +00002020/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002021 * List variables for hashtab "ht" with prefix "prefix".
2022 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002023 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002025list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002026 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002027 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002028 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002029 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002030{
Bram Moolenaar33570922005-01-25 22:26:29 +00002031 hashitem_T *hi;
2032 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002033 int todo;
2034
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002035 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002036 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2037 {
2038 if (!HASHITEM_EMPTY(hi))
2039 {
2040 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002041 di = HI2DI(hi);
2042 if (empty || di->di_tv.v_type != VAR_STRING
2043 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002044 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002045 }
2046 }
2047}
2048
2049/*
2050 * List global variables.
2051 */
2052 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002053list_glob_vars(first)
2054 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002055{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002056 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002057}
2058
2059/*
2060 * List buffer variables.
2061 */
2062 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002063list_buf_vars(first)
2064 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002065{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066 char_u numbuf[NUMBUFLEN];
2067
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002068 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2069 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002070
2071 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002072 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2073 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002074}
2075
2076/*
2077 * List window variables.
2078 */
2079 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002080list_win_vars(first)
2081 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002082{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002083 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2084 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002085}
2086
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002087#ifdef FEAT_WINDOWS
2088/*
2089 * List tab page variables.
2090 */
2091 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002092list_tab_vars(first)
2093 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002094{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2096 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002097}
2098#endif
2099
Bram Moolenaara7043832005-01-21 11:56:39 +00002100/*
2101 * List Vim variables.
2102 */
2103 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104list_vim_vars(first)
2105 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002108}
2109
2110/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002111 * List script-local variables, if there is a script.
2112 */
2113 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114list_script_vars(first)
2115 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002116{
2117 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2119 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002120}
2121
2122/*
2123 * List function variables, if there is a function.
2124 */
2125 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126list_func_vars(first)
2127 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002128{
2129 if (current_funccal != NULL)
2130 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002131 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002132}
2133
2134/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002135 * List variables in "arg".
2136 */
2137 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139 exarg_T *eap;
2140 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002142{
2143 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002144 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146 char_u *name_start;
2147 char_u *arg_subsc;
2148 char_u *tofree;
2149 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150
2151 while (!ends_excmd(*arg) && !got_int)
2152 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002153 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002155 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002156 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2157 {
2158 emsg_severe = TRUE;
2159 EMSG(_(e_trailing));
2160 break;
2161 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002163 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002165 /* get_name_len() takes care of expanding curly braces */
2166 name_start = name = arg;
2167 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2168 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002169 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002170 /* This is mainly to keep test 49 working: when expanding
2171 * curly braces fails overrule the exception error message. */
2172 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002174 emsg_severe = TRUE;
2175 EMSG2(_(e_invarg2), arg);
2176 break;
2177 }
2178 error = TRUE;
2179 }
2180 else
2181 {
2182 if (tofree != NULL)
2183 name = tofree;
2184 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 else
2187 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002188 /* handle d.key, l[idx], f(expr) */
2189 arg_subsc = arg;
2190 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002191 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002193 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002194 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002195 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002197 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 case 'g': list_glob_vars(first); break;
2199 case 'b': list_buf_vars(first); break;
2200 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204 case 'v': list_vim_vars(first); break;
2205 case 's': list_script_vars(first); break;
2206 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 default:
2208 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002209 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002210 }
2211 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 {
2213 char_u numbuf[NUMBUFLEN];
2214 char_u *tf;
2215 int c;
2216 char_u *s;
2217
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002218 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 c = *arg;
2220 *arg = NUL;
2221 list_one_var_a((char_u *)"",
2222 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 tv.v_type,
2224 s == NULL ? (char_u *)"" : s,
2225 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 *arg = c;
2227 vim_free(tf);
2228 }
2229 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002230 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 }
2232 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233
2234 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236
2237 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
2239
2240 return arg;
2241}
2242
2243/*
2244 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2245 * Returns a pointer to the char just after the var name.
2246 * Returns NULL if there is an error.
2247 */
2248 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002249ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002251 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002253 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002254 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255{
2256 int c1;
2257 char_u *name;
2258 char_u *p;
2259 char_u *arg_end = NULL;
2260 int len;
2261 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002262 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263
2264 /*
2265 * ":let $VAR = expr": Set environment variable.
2266 */
2267 if (*arg == '$')
2268 {
2269 /* Find the end of the name. */
2270 ++arg;
2271 name = arg;
2272 len = get_env_len(&arg);
2273 if (len == 0)
2274 EMSG2(_(e_invarg2), name - 1);
2275 else
2276 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002277 if (op != NULL && (*op == '+' || *op == '-'))
2278 EMSG2(_(e_letwrong), op);
2279 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002280 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 EMSG(_(e_letunexp));
2282 else
2283 {
2284 c1 = name[len];
2285 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002286 p = get_tv_string_chk(tv);
2287 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002288 {
2289 int mustfree = FALSE;
2290 char_u *s = vim_getenv(name, &mustfree);
2291
2292 if (s != NULL)
2293 {
2294 p = tofree = concat_str(s, p);
2295 if (mustfree)
2296 vim_free(s);
2297 }
2298 }
2299 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002300 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002302 if (STRICMP(name, "HOME") == 0)
2303 init_homedir();
2304 else if (didset_vim && STRICMP(name, "VIM") == 0)
2305 didset_vim = FALSE;
2306 else if (didset_vimruntime
2307 && STRICMP(name, "VIMRUNTIME") == 0)
2308 didset_vimruntime = FALSE;
2309 arg_end = arg;
2310 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002312 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 }
2314 }
2315 }
2316
2317 /*
2318 * ":let &option = expr": Set option value.
2319 * ":let &l:option = expr": Set local option value.
2320 * ":let &g:option = expr": Set global option value.
2321 */
2322 else if (*arg == '&')
2323 {
2324 /* Find the end of the name. */
2325 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002326 if (p == NULL || (endchars != NULL
2327 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 EMSG(_(e_letunexp));
2329 else
2330 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 long n;
2332 int opt_type;
2333 long numval;
2334 char_u *stringval = NULL;
2335 char_u *s;
2336
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 c1 = *p;
2338 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002339
2340 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002341 s = get_tv_string_chk(tv); /* != NULL if number or string */
2342 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 {
2344 opt_type = get_option_value(arg, &numval,
2345 &stringval, opt_flags);
2346 if ((opt_type == 1 && *op == '.')
2347 || (opt_type == 0 && *op != '.'))
2348 EMSG2(_(e_letwrong), op);
2349 else
2350 {
2351 if (opt_type == 1) /* number */
2352 {
2353 if (*op == '+')
2354 n = numval + n;
2355 else
2356 n = numval - n;
2357 }
2358 else if (opt_type == 0 && stringval != NULL) /* string */
2359 {
2360 s = concat_str(stringval, s);
2361 vim_free(stringval);
2362 stringval = s;
2363 }
2364 }
2365 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002366 if (s != NULL)
2367 {
2368 set_option_value(arg, n, s, opt_flags);
2369 arg_end = p;
2370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
2374 }
2375
2376 /*
2377 * ":let @r = expr": Set register contents.
2378 */
2379 else if (*arg == '@')
2380 {
2381 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 if (op != NULL && (*op == '+' || *op == '-'))
2383 EMSG2(_(e_letwrong), op);
2384 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002385 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 EMSG(_(e_letunexp));
2387 else
2388 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002389 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 char_u *s;
2391
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 p = get_tv_string_chk(tv);
2393 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002395 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 if (s != NULL)
2397 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002398 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399 vim_free(s);
2400 }
2401 }
2402 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002405 arg_end = arg + 1;
2406 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002407 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002408 }
2409 }
2410
2411 /*
2412 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002415 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002417 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002419 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2423 EMSG(_(e_letunexp));
2424 else
2425 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002427 arg_end = p;
2428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 }
2432
2433 else
2434 EMSG2(_(e_invarg2), arg);
2435
2436 return arg_end;
2437}
2438
2439/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2441 */
2442 static int
2443check_changedtick(arg)
2444 char_u *arg;
2445{
2446 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2447 {
2448 EMSG2(_(e_readonlyvar), arg);
2449 return TRUE;
2450 }
2451 return FALSE;
2452}
2453
2454/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 * Get an lval: variable, Dict item or List item that can be assigned a value
2456 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2457 * "name.key", "name.key[expr]" etc.
2458 * Indexing only works if "name" is an existing List or Dictionary.
2459 * "name" points to the start of the name.
2460 * If "rettv" is not NULL it points to the value to be assigned.
2461 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2462 * wrong; must end in space or cmd separator.
2463 *
2464 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002465 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 */
2468 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002469get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002471 typval_T *rettv;
2472 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 int unlet;
2474 int skip;
2475 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002476 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 char_u *expr_start, *expr_end;
2480 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002481 dictitem_T *v;
2482 typval_T var1;
2483 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002485 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002488 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002491 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492
2493 if (skip)
2494 {
2495 /* When skipping just find the end of the name. */
2496 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002497 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 }
2499
2500 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002501 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 if (expr_start != NULL)
2503 {
2504 /* Don't expand the name when we already know there is an error. */
2505 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2506 && *p != '[' && *p != '.')
2507 {
2508 EMSG(_(e_trailing));
2509 return NULL;
2510 }
2511
2512 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2513 if (lp->ll_exp_name == NULL)
2514 {
2515 /* Report an invalid expression in braces, unless the
2516 * expression evaluation has been cancelled due to an
2517 * aborting error, an interrupt, or an exception. */
2518 if (!aborting() && !quiet)
2519 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002520 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 EMSG2(_(e_invarg2), name);
2522 return NULL;
2523 }
2524 }
2525 lp->ll_name = lp->ll_exp_name;
2526 }
2527 else
2528 lp->ll_name = name;
2529
2530 /* Without [idx] or .key we are done. */
2531 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2532 return p;
2533
2534 cc = *p;
2535 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002536 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 if (v == NULL && !quiet)
2538 EMSG2(_(e_undefvar), lp->ll_name);
2539 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 if (v == NULL)
2541 return NULL;
2542
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 /*
2544 * Loop until no more [idx] or .key is following.
2545 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002546 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2550 && !(lp->ll_tv->v_type == VAR_DICT
2551 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002552 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 if (!quiet)
2554 EMSG(_("E689: Can only index a List or Dictionary"));
2555 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 if (!quiet)
2560 EMSG(_("E708: [:] must come last"));
2561 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002562 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002563
Bram Moolenaar8c711452005-01-14 21:53:12 +00002564 len = -1;
2565 if (*p == '.')
2566 {
2567 key = p + 1;
2568 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2569 ;
2570 if (len == 0)
2571 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 if (!quiet)
2573 EMSG(_(e_emptykey));
2574 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002575 }
2576 p = key + len;
2577 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002578 else
2579 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002581 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 if (*p == ':')
2583 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002584 else
2585 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586 empty1 = FALSE;
2587 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002589 if (get_tv_string_chk(&var1) == NULL)
2590 {
2591 /* not a number or string */
2592 clear_tv(&var1);
2593 return NULL;
2594 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 }
2596
2597 /* Optionally get the second index [ :expr]. */
2598 if (*p == ':')
2599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002603 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002604 if (!empty1)
2605 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002607 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 if (rettv != NULL && (rettv->v_type != VAR_LIST
2609 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (!quiet)
2612 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 if (!empty1)
2614 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 }
2617 p = skipwhite(p + 1);
2618 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 else
2621 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2624 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002625 if (!empty1)
2626 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002629 if (get_tv_string_chk(&var2) == NULL)
2630 {
2631 /* not a number or string */
2632 if (!empty1)
2633 clear_tv(&var1);
2634 clear_tv(&var2);
2635 return NULL;
2636 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002637 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002639 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002642
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (!quiet)
2646 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 if (!empty1)
2648 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 }
2653
2654 /* Skip to past ']'. */
2655 ++p;
2656 }
2657
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 {
2660 if (len == -1)
2661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002663 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 if (*key == NUL)
2665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
2667 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
2671 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002672 lp->ll_list = NULL;
2673 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002674 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002677 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002681 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 if (len == -1)
2683 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
2686 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (len == -1)
2691 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 p = NULL;
2694 break;
2695 }
2696 if (len == -1)
2697 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
2700 else
2701 {
2702 /*
2703 * Get the number and item for the only or first index of the List.
2704 */
2705 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 else
2708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002709 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 clear_tv(&var1);
2711 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002712 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 lp->ll_list = lp->ll_tv->vval.v_list;
2714 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2715 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002717 if (lp->ll_n1 < 0)
2718 {
2719 lp->ll_n1 = 0;
2720 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2721 }
2722 }
2723 if (lp->ll_li == NULL)
2724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 }
2729
2730 /*
2731 * May need to find the item or absolute index for the second
2732 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 * When no index given: "lp->ll_empty2" is TRUE.
2734 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002738 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 }
2747
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2749 if (lp->ll_n1 < 0)
2750 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2751 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753 }
2754
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002757 }
2758
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 return p;
2760}
2761
2762/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002763 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 */
2765 static void
2766clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002767 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768{
2769 vim_free(lp->ll_exp_name);
2770 vim_free(lp->ll_newkey);
2771}
2772
2773/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002774 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002776 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 */
2778 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002780 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002782 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002784 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785{
2786 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002787 listitem_T *ri;
2788 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789
2790 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 cc = *endp;
2795 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002796 if (op != NULL && *op != '=')
2797 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002798 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002799
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002800 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002801 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002802 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 {
2804 if (tv_op(&tv, rettv, op) == OK)
2805 set_var(lp->ll_name, &tv, FALSE);
2806 clear_tv(&tv);
2807 }
2808 }
2809 else
2810 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002812 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002814 else if (tv_check_lock(lp->ll_newkey == NULL
2815 ? lp->ll_tv->v_lock
2816 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2817 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 else if (lp->ll_range)
2819 {
2820 /*
2821 * Assign the List values to the list items.
2822 */
2823 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002824 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002825 if (op != NULL && *op != '=')
2826 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2827 else
2828 {
2829 clear_tv(&lp->ll_li->li_tv);
2830 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2831 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 ri = ri->li_next;
2833 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2834 break;
2835 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002836 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002838 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002839 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 ri = NULL;
2841 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002842 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002843 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 lp->ll_li = lp->ll_li->li_next;
2845 ++lp->ll_n1;
2846 }
2847 if (ri != NULL)
2848 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002849 else if (lp->ll_empty2
2850 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 : lp->ll_n1 != lp->ll_n2)
2852 EMSG(_("E711: List value has not enough items"));
2853 }
2854 else
2855 {
2856 /*
2857 * Assign to a List or Dictionary item.
2858 */
2859 if (lp->ll_newkey != NULL)
2860 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002861 if (op != NULL && *op != '=')
2862 {
2863 EMSG2(_(e_letwrong), op);
2864 return;
2865 }
2866
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002868 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (di == NULL)
2870 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002871 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2872 {
2873 vim_free(di);
2874 return;
2875 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002877 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002878 else if (op != NULL && *op != '=')
2879 {
2880 tv_op(lp->ll_tv, rettv, op);
2881 return;
2882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002883 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 /*
2887 * Assign the value to the variable or list item.
2888 */
2889 if (copy)
2890 copy_tv(rettv, lp->ll_tv);
2891 else
2892 {
2893 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002894 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002896 }
2897 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002898}
2899
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002900/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002901 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2902 * Returns OK or FAIL.
2903 */
2904 static int
2905tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002906 typval_T *tv1;
2907 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 char_u *op;
2909{
2910 long n;
2911 char_u numbuf[NUMBUFLEN];
2912 char_u *s;
2913
2914 /* Can't do anything with a Funcref or a Dict on the right. */
2915 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2916 {
2917 switch (tv1->v_type)
2918 {
2919 case VAR_DICT:
2920 case VAR_FUNC:
2921 break;
2922
2923 case VAR_LIST:
2924 if (*op != '+' || tv2->v_type != VAR_LIST)
2925 break;
2926 /* List += List */
2927 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2928 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2929 return OK;
2930
2931 case VAR_NUMBER:
2932 case VAR_STRING:
2933 if (tv2->v_type == VAR_LIST)
2934 break;
2935 if (*op == '+' || *op == '-')
2936 {
2937 /* nr += nr or nr -= nr*/
2938 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002939#ifdef FEAT_FLOAT
2940 if (tv2->v_type == VAR_FLOAT)
2941 {
2942 float_T f = n;
2943
2944 if (*op == '+')
2945 f += tv2->vval.v_float;
2946 else
2947 f -= tv2->vval.v_float;
2948 clear_tv(tv1);
2949 tv1->v_type = VAR_FLOAT;
2950 tv1->vval.v_float = f;
2951 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002953#endif
2954 {
2955 if (*op == '+')
2956 n += get_tv_number(tv2);
2957 else
2958 n -= get_tv_number(tv2);
2959 clear_tv(tv1);
2960 tv1->v_type = VAR_NUMBER;
2961 tv1->vval.v_number = n;
2962 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 }
2964 else
2965 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002966 if (tv2->v_type == VAR_FLOAT)
2967 break;
2968
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002969 /* str .= str */
2970 s = get_tv_string(tv1);
2971 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2972 clear_tv(tv1);
2973 tv1->v_type = VAR_STRING;
2974 tv1->vval.v_string = s;
2975 }
2976 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002977
2978#ifdef FEAT_FLOAT
2979 case VAR_FLOAT:
2980 {
2981 float_T f;
2982
2983 if (*op == '.' || (tv2->v_type != VAR_FLOAT
2984 && tv2->v_type != VAR_NUMBER
2985 && tv2->v_type != VAR_STRING))
2986 break;
2987 if (tv2->v_type == VAR_FLOAT)
2988 f = tv2->vval.v_float;
2989 else
2990 f = get_tv_number(tv2);
2991 if (*op == '+')
2992 tv1->vval.v_float += f;
2993 else
2994 tv1->vval.v_float -= f;
2995 }
2996 return OK;
2997#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002998 }
2999 }
3000
3001 EMSG2(_(e_letwrong), op);
3002 return FAIL;
3003}
3004
3005/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003006 * Add a watcher to a list.
3007 */
3008 static void
3009list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003010 list_T *l;
3011 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003012{
3013 lw->lw_next = l->lv_watch;
3014 l->lv_watch = lw;
3015}
3016
3017/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003018 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003019 * No warning when it isn't found...
3020 */
3021 static void
3022list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003023 list_T *l;
3024 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003025{
Bram Moolenaar33570922005-01-25 22:26:29 +00003026 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003027
3028 lwp = &l->lv_watch;
3029 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3030 {
3031 if (lw == lwrem)
3032 {
3033 *lwp = lw->lw_next;
3034 break;
3035 }
3036 lwp = &lw->lw_next;
3037 }
3038}
3039
3040/*
3041 * Just before removing an item from a list: advance watchers to the next
3042 * item.
3043 */
3044 static void
3045list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003046 list_T *l;
3047 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003048{
Bram Moolenaar33570922005-01-25 22:26:29 +00003049 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003050
3051 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3052 if (lw->lw_item == item)
3053 lw->lw_item = item->li_next;
3054}
3055
3056/*
3057 * Evaluate the expression used in a ":for var in expr" command.
3058 * "arg" points to "var".
3059 * Set "*errp" to TRUE for an error, FALSE otherwise;
3060 * Return a pointer that holds the info. Null when there is an error.
3061 */
3062 void *
3063eval_for_line(arg, errp, nextcmdp, skip)
3064 char_u *arg;
3065 int *errp;
3066 char_u **nextcmdp;
3067 int skip;
3068{
Bram Moolenaar33570922005-01-25 22:26:29 +00003069 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003070 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003071 typval_T tv;
3072 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003073
3074 *errp = TRUE; /* default: there is an error */
3075
Bram Moolenaar33570922005-01-25 22:26:29 +00003076 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003077 if (fi == NULL)
3078 return NULL;
3079
3080 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3081 if (expr == NULL)
3082 return fi;
3083
3084 expr = skipwhite(expr);
3085 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3086 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003087 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003088 return fi;
3089 }
3090
3091 if (skip)
3092 ++emsg_skip;
3093 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3094 {
3095 *errp = FALSE;
3096 if (!skip)
3097 {
3098 l = tv.vval.v_list;
3099 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003100 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003101 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003102 clear_tv(&tv);
3103 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003104 else
3105 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003106 /* No need to increment the refcount, it's already set for the
3107 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108 fi->fi_list = l;
3109 list_add_watch(l, &fi->fi_lw);
3110 fi->fi_lw.lw_item = l->lv_first;
3111 }
3112 }
3113 }
3114 if (skip)
3115 --emsg_skip;
3116
3117 return fi;
3118}
3119
3120/*
3121 * Use the first item in a ":for" list. Advance to the next.
3122 * Assign the values to the variable (list). "arg" points to the first one.
3123 * Return TRUE when a valid item was found, FALSE when at end of list or
3124 * something wrong.
3125 */
3126 int
3127next_for_item(fi_void, arg)
3128 void *fi_void;
3129 char_u *arg;
3130{
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003133 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134
3135 item = fi->fi_lw.lw_item;
3136 if (item == NULL)
3137 result = FALSE;
3138 else
3139 {
3140 fi->fi_lw.lw_item = item->li_next;
3141 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3142 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3143 }
3144 return result;
3145}
3146
3147/*
3148 * Free the structure used to store info used by ":for".
3149 */
3150 void
3151free_for_info(fi_void)
3152 void *fi_void;
3153{
Bram Moolenaar33570922005-01-25 22:26:29 +00003154 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003156 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003157 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003158 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003159 list_unref(fi->fi_list);
3160 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003161 vim_free(fi);
3162}
3163
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3165
3166 void
3167set_context_for_expression(xp, arg, cmdidx)
3168 expand_T *xp;
3169 char_u *arg;
3170 cmdidx_T cmdidx;
3171{
3172 int got_eq = FALSE;
3173 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176 if (cmdidx == CMD_let)
3177 {
3178 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003179 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180 {
3181 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003182 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183 {
3184 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003185 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003186 if (vim_iswhite(*p))
3187 break;
3188 }
3189 return;
3190 }
3191 }
3192 else
3193 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3194 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 while ((xp->xp_pattern = vim_strpbrk(arg,
3196 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3197 {
3198 c = *xp->xp_pattern;
3199 if (c == '&')
3200 {
3201 c = xp->xp_pattern[1];
3202 if (c == '&')
3203 {
3204 ++xp->xp_pattern;
3205 xp->xp_context = cmdidx != CMD_let || got_eq
3206 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3207 }
3208 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003211 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3212 xp->xp_pattern += 2;
3213
3214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
3216 else if (c == '$')
3217 {
3218 /* environment variable */
3219 xp->xp_context = EXPAND_ENV_VARS;
3220 }
3221 else if (c == '=')
3222 {
3223 got_eq = TRUE;
3224 xp->xp_context = EXPAND_EXPRESSION;
3225 }
3226 else if (c == '<'
3227 && xp->xp_context == EXPAND_FUNCTIONS
3228 && vim_strchr(xp->xp_pattern, '(') == NULL)
3229 {
3230 /* Function name can start with "<SNR>" */
3231 break;
3232 }
3233 else if (cmdidx != CMD_let || got_eq)
3234 {
3235 if (c == '"') /* string */
3236 {
3237 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3238 if (c == '\\' && xp->xp_pattern[1] != NUL)
3239 ++xp->xp_pattern;
3240 xp->xp_context = EXPAND_NOTHING;
3241 }
3242 else if (c == '\'') /* literal string */
3243 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003244 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3246 /* skip */ ;
3247 xp->xp_context = EXPAND_NOTHING;
3248 }
3249 else if (c == '|')
3250 {
3251 if (xp->xp_pattern[1] == '|')
3252 {
3253 ++xp->xp_pattern;
3254 xp->xp_context = EXPAND_EXPRESSION;
3255 }
3256 else
3257 xp->xp_context = EXPAND_COMMANDS;
3258 }
3259 else
3260 xp->xp_context = EXPAND_EXPRESSION;
3261 }
3262 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 /* Doesn't look like something valid, expand as an expression
3264 * anyway. */
3265 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 arg = xp->xp_pattern;
3267 if (*arg != NUL)
3268 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3269 /* skip */ ;
3270 }
3271 xp->xp_pattern = arg;
3272}
3273
3274#endif /* FEAT_CMDL_COMPL */
3275
3276/*
3277 * ":1,25call func(arg1, arg2)" function call.
3278 */
3279 void
3280ex_call(eap)
3281 exarg_T *eap;
3282{
3283 char_u *arg = eap->arg;
3284 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003286 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003288 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 linenr_T lnum;
3290 int doesrange;
3291 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003292 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003294 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003295 if (fudi.fd_newkey != NULL)
3296 {
3297 /* Still need to give an error message for missing key. */
3298 EMSG2(_(e_dictkey), fudi.fd_newkey);
3299 vim_free(fudi.fd_newkey);
3300 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003301 if (tofree == NULL)
3302 return;
3303
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003304 /* Increase refcount on dictionary, it could get deleted when evaluating
3305 * the arguments. */
3306 if (fudi.fd_dict != NULL)
3307 ++fudi.fd_dict->dv_refcount;
3308
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003309 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003310 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003311 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
Bram Moolenaar532c7802005-01-27 14:44:31 +00003313 /* Skip white space to allow ":call func ()". Not good, but required for
3314 * backward compatibility. */
3315 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003316 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
3318 if (*startarg != '(')
3319 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003320 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 goto end;
3322 }
3323
3324 /*
3325 * When skipping, evaluate the function once, to find the end of the
3326 * arguments.
3327 * When the function takes a range, this is discovered after the first
3328 * call, and the loop is broken.
3329 */
3330 if (eap->skip)
3331 {
3332 ++emsg_skip;
3333 lnum = eap->line2; /* do it once, also with an invalid range */
3334 }
3335 else
3336 lnum = eap->line1;
3337 for ( ; lnum <= eap->line2; ++lnum)
3338 {
3339 if (!eap->skip && eap->addr_count > 0)
3340 {
3341 curwin->w_cursor.lnum = lnum;
3342 curwin->w_cursor.col = 0;
3343 }
3344 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003345 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003346 eap->line1, eap->line2, &doesrange,
3347 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
3349 failed = TRUE;
3350 break;
3351 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003352
3353 /* Handle a function returning a Funcref, Dictionary or List. */
3354 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3355 {
3356 failed = TRUE;
3357 break;
3358 }
3359
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003360 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 if (doesrange || eap->skip)
3362 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003365 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003366 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003367 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (aborting())
3369 break;
3370 }
3371 if (eap->skip)
3372 --emsg_skip;
3373
3374 if (!failed)
3375 {
3376 /* Check for trailing illegal characters and a following command. */
3377 if (!ends_excmd(*arg))
3378 {
3379 emsg_severe = TRUE;
3380 EMSG(_(e_trailing));
3381 }
3382 else
3383 eap->nextcmd = check_nextcmd(arg);
3384 }
3385
3386end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003387 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003388 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389}
3390
3391/*
3392 * ":unlet[!] var1 ... " command.
3393 */
3394 void
3395ex_unlet(eap)
3396 exarg_T *eap;
3397{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003398 ex_unletlock(eap, eap->arg, 0);
3399}
3400
3401/*
3402 * ":lockvar" and ":unlockvar" commands
3403 */
3404 void
3405ex_lockvar(eap)
3406 exarg_T *eap;
3407{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003409 int deep = 2;
3410
3411 if (eap->forceit)
3412 deep = -1;
3413 else if (vim_isdigit(*arg))
3414 {
3415 deep = getdigits(&arg);
3416 arg = skipwhite(arg);
3417 }
3418
3419 ex_unletlock(eap, arg, deep);
3420}
3421
3422/*
3423 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3424 */
3425 static void
3426ex_unletlock(eap, argstart, deep)
3427 exarg_T *eap;
3428 char_u *argstart;
3429 int deep;
3430{
3431 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003434 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
3436 do
3437 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003438 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003439 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3440 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003441 if (lv.ll_name == NULL)
3442 error = TRUE; /* error but continue parsing */
3443 if (name_end == NULL || (!vim_iswhite(*name_end)
3444 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003446 if (name_end != NULL)
3447 {
3448 emsg_severe = TRUE;
3449 EMSG(_(e_trailing));
3450 }
3451 if (!(eap->skip || error))
3452 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 break;
3454 }
3455
3456 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003457 {
3458 if (eap->cmdidx == CMD_unlet)
3459 {
3460 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3461 error = TRUE;
3462 }
3463 else
3464 {
3465 if (do_lock_var(&lv, name_end, deep,
3466 eap->cmdidx == CMD_lockvar) == FAIL)
3467 error = TRUE;
3468 }
3469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003471 if (!eap->skip)
3472 clear_lval(&lv);
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 arg = skipwhite(name_end);
3475 } while (!ends_excmd(*arg));
3476
3477 eap->nextcmd = check_nextcmd(arg);
3478}
3479
Bram Moolenaar8c711452005-01-14 21:53:12 +00003480 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003481do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003482 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003483 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003484 int forceit;
3485{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003486 int ret = OK;
3487 int cc;
3488
3489 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003490 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003491 cc = *name_end;
3492 *name_end = NUL;
3493
3494 /* Normal name or expanded name. */
3495 if (check_changedtick(lp->ll_name))
3496 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003497 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003498 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003499 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003500 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003501 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3502 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003503 else if (lp->ll_range)
3504 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003505 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003506
3507 /* Delete a range of List items. */
3508 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3509 {
3510 li = lp->ll_li->li_next;
3511 listitem_remove(lp->ll_list, lp->ll_li);
3512 lp->ll_li = li;
3513 ++lp->ll_n1;
3514 }
3515 }
3516 else
3517 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003518 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003519 /* unlet a List item. */
3520 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003521 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003522 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003523 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003524 }
3525
3526 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003527}
3528
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529/*
3530 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003531 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 */
3533 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003536 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
Bram Moolenaar33570922005-01-25 22:26:29 +00003538 hashtab_T *ht;
3539 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003540 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003541 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
Bram Moolenaar33570922005-01-25 22:26:29 +00003543 ht = find_var_ht(name, &varname);
3544 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003546 hi = hash_find(ht, varname);
3547 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003548 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003549 di = HI2DI(hi);
3550 if (var_check_fixed(di->di_flags, name)
3551 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003552 return FAIL;
3553 delete_var(ht, hi);
3554 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003557 if (forceit)
3558 return OK;
3559 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 return FAIL;
3561}
3562
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003563/*
3564 * Lock or unlock variable indicated by "lp".
3565 * "deep" is the levels to go (-1 for unlimited);
3566 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3567 */
3568 static int
3569do_lock_var(lp, name_end, deep, lock)
3570 lval_T *lp;
3571 char_u *name_end;
3572 int deep;
3573 int lock;
3574{
3575 int ret = OK;
3576 int cc;
3577 dictitem_T *di;
3578
3579 if (deep == 0) /* nothing to do */
3580 return OK;
3581
3582 if (lp->ll_tv == NULL)
3583 {
3584 cc = *name_end;
3585 *name_end = NUL;
3586
3587 /* Normal name or expanded name. */
3588 if (check_changedtick(lp->ll_name))
3589 ret = FAIL;
3590 else
3591 {
3592 di = find_var(lp->ll_name, NULL);
3593 if (di == NULL)
3594 ret = FAIL;
3595 else
3596 {
3597 if (lock)
3598 di->di_flags |= DI_FLAGS_LOCK;
3599 else
3600 di->di_flags &= ~DI_FLAGS_LOCK;
3601 item_lock(&di->di_tv, deep, lock);
3602 }
3603 }
3604 *name_end = cc;
3605 }
3606 else if (lp->ll_range)
3607 {
3608 listitem_T *li = lp->ll_li;
3609
3610 /* (un)lock a range of List items. */
3611 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3612 {
3613 item_lock(&li->li_tv, deep, lock);
3614 li = li->li_next;
3615 ++lp->ll_n1;
3616 }
3617 }
3618 else if (lp->ll_list != NULL)
3619 /* (un)lock a List item. */
3620 item_lock(&lp->ll_li->li_tv, deep, lock);
3621 else
3622 /* un(lock) a Dictionary item. */
3623 item_lock(&lp->ll_di->di_tv, deep, lock);
3624
3625 return ret;
3626}
3627
3628/*
3629 * Lock or unlock an item. "deep" is nr of levels to go.
3630 */
3631 static void
3632item_lock(tv, deep, lock)
3633 typval_T *tv;
3634 int deep;
3635 int lock;
3636{
3637 static int recurse = 0;
3638 list_T *l;
3639 listitem_T *li;
3640 dict_T *d;
3641 hashitem_T *hi;
3642 int todo;
3643
3644 if (recurse >= DICT_MAXNEST)
3645 {
3646 EMSG(_("E743: variable nested too deep for (un)lock"));
3647 return;
3648 }
3649 if (deep == 0)
3650 return;
3651 ++recurse;
3652
3653 /* lock/unlock the item itself */
3654 if (lock)
3655 tv->v_lock |= VAR_LOCKED;
3656 else
3657 tv->v_lock &= ~VAR_LOCKED;
3658
3659 switch (tv->v_type)
3660 {
3661 case VAR_LIST:
3662 if ((l = tv->vval.v_list) != NULL)
3663 {
3664 if (lock)
3665 l->lv_lock |= VAR_LOCKED;
3666 else
3667 l->lv_lock &= ~VAR_LOCKED;
3668 if (deep < 0 || deep > 1)
3669 /* recursive: lock/unlock the items the List contains */
3670 for (li = l->lv_first; li != NULL; li = li->li_next)
3671 item_lock(&li->li_tv, deep - 1, lock);
3672 }
3673 break;
3674 case VAR_DICT:
3675 if ((d = tv->vval.v_dict) != NULL)
3676 {
3677 if (lock)
3678 d->dv_lock |= VAR_LOCKED;
3679 else
3680 d->dv_lock &= ~VAR_LOCKED;
3681 if (deep < 0 || deep > 1)
3682 {
3683 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003684 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3686 {
3687 if (!HASHITEM_EMPTY(hi))
3688 {
3689 --todo;
3690 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3691 }
3692 }
3693 }
3694 }
3695 }
3696 --recurse;
3697}
3698
Bram Moolenaara40058a2005-07-11 22:42:07 +00003699/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003700 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3701 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003702 */
3703 static int
3704tv_islocked(tv)
3705 typval_T *tv;
3706{
3707 return (tv->v_lock & VAR_LOCKED)
3708 || (tv->v_type == VAR_LIST
3709 && tv->vval.v_list != NULL
3710 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3711 || (tv->v_type == VAR_DICT
3712 && tv->vval.v_dict != NULL
3713 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3714}
3715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3717/*
3718 * Delete all "menutrans_" variables.
3719 */
3720 void
3721del_menutrans_vars()
3722{
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003724 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
Bram Moolenaar33570922005-01-25 22:26:29 +00003726 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003727 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003728 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003729 {
3730 if (!HASHITEM_EMPTY(hi))
3731 {
3732 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3734 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003735 }
3736 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003737 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738}
3739#endif
3740
3741#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3742
3743/*
3744 * Local string buffer for the next two functions to store a variable name
3745 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3746 * get_user_var_name().
3747 */
3748
3749static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3750
3751static char_u *varnamebuf = NULL;
3752static int varnamebuflen = 0;
3753
3754/*
3755 * Function to concatenate a prefix and a variable name.
3756 */
3757 static char_u *
3758cat_prefix_varname(prefix, name)
3759 int prefix;
3760 char_u *name;
3761{
3762 int len;
3763
3764 len = (int)STRLEN(name) + 3;
3765 if (len > varnamebuflen)
3766 {
3767 vim_free(varnamebuf);
3768 len += 10; /* some additional space */
3769 varnamebuf = alloc(len);
3770 if (varnamebuf == NULL)
3771 {
3772 varnamebuflen = 0;
3773 return NULL;
3774 }
3775 varnamebuflen = len;
3776 }
3777 *varnamebuf = prefix;
3778 varnamebuf[1] = ':';
3779 STRCPY(varnamebuf + 2, name);
3780 return varnamebuf;
3781}
3782
3783/*
3784 * Function given to ExpandGeneric() to obtain the list of user defined
3785 * (global/buffer/window/built-in) variable names.
3786 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 char_u *
3788get_user_var_name(xp, idx)
3789 expand_T *xp;
3790 int idx;
3791{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003792 static long_u gdone;
3793 static long_u bdone;
3794 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003795#ifdef FEAT_WINDOWS
3796 static long_u tdone;
3797#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003798 static int vidx;
3799 static hashitem_T *hi;
3800 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801
3802 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003803 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003804 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003805#ifdef FEAT_WINDOWS
3806 tdone = 0;
3807#endif
3808 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003809
3810 /* Global variables */
3811 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003813 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003814 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003815 else
3816 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003817 while (HASHITEM_EMPTY(hi))
3818 ++hi;
3819 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3820 return cat_prefix_varname('g', hi->hi_key);
3821 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003823
3824 /* b: variables */
3825 ht = &curbuf->b_vars.dv_hashtab;
3826 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003828 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003829 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003830 else
3831 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003832 while (HASHITEM_EMPTY(hi))
3833 ++hi;
3834 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003836 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003838 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 return (char_u *)"b:changedtick";
3840 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003841
3842 /* w: variables */
3843 ht = &curwin->w_vars.dv_hashtab;
3844 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003846 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003847 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003848 else
3849 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 while (HASHITEM_EMPTY(hi))
3851 ++hi;
3852 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003854
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003855#ifdef FEAT_WINDOWS
3856 /* t: variables */
3857 ht = &curtab->tp_vars.dv_hashtab;
3858 if (tdone < ht->ht_used)
3859 {
3860 if (tdone++ == 0)
3861 hi = ht->ht_array;
3862 else
3863 ++hi;
3864 while (HASHITEM_EMPTY(hi))
3865 ++hi;
3866 return cat_prefix_varname('t', hi->hi_key);
3867 }
3868#endif
3869
Bram Moolenaar33570922005-01-25 22:26:29 +00003870 /* v: variables */
3871 if (vidx < VV_LEN)
3872 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873
3874 vim_free(varnamebuf);
3875 varnamebuf = NULL;
3876 varnamebuflen = 0;
3877 return NULL;
3878}
3879
3880#endif /* FEAT_CMDL_COMPL */
3881
3882/*
3883 * types for expressions.
3884 */
3885typedef enum
3886{
3887 TYPE_UNKNOWN = 0
3888 , TYPE_EQUAL /* == */
3889 , TYPE_NEQUAL /* != */
3890 , TYPE_GREATER /* > */
3891 , TYPE_GEQUAL /* >= */
3892 , TYPE_SMALLER /* < */
3893 , TYPE_SEQUAL /* <= */
3894 , TYPE_MATCH /* =~ */
3895 , TYPE_NOMATCH /* !~ */
3896} exptype_T;
3897
3898/*
3899 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003900 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3902 */
3903
3904/*
3905 * Handle zero level expression.
3906 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003907 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003908 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 * Return OK or FAIL.
3910 */
3911 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003912eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 char_u **nextcmd;
3916 int evaluate;
3917{
3918 int ret;
3919 char_u *p;
3920
3921 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003922 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 if (ret == FAIL || !ends_excmd(*p))
3924 {
3925 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003926 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 /*
3928 * Report the invalid expression unless the expression evaluation has
3929 * been cancelled due to an aborting error, an interrupt, or an
3930 * exception.
3931 */
3932 if (!aborting())
3933 EMSG2(_(e_invexpr2), arg);
3934 ret = FAIL;
3935 }
3936 if (nextcmd != NULL)
3937 *nextcmd = check_nextcmd(p);
3938
3939 return ret;
3940}
3941
3942/*
3943 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003944 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 *
3946 * "arg" must point to the first non-white of the expression.
3947 * "arg" is advanced to the next non-white after the recognized expression.
3948 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003949 * Note: "rettv.v_lock" is not set.
3950 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 * Return OK or FAIL.
3952 */
3953 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 int evaluate;
3958{
3959 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961
3962 /*
3963 * Get the first variable.
3964 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003965 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return FAIL;
3967
3968 if ((*arg)[0] == '?')
3969 {
3970 result = FALSE;
3971 if (evaluate)
3972 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003973 int error = FALSE;
3974
3975 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003977 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003978 if (error)
3979 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981
3982 /*
3983 * Get the second variable.
3984 */
3985 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003986 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 return FAIL;
3988
3989 /*
3990 * Check for the ":".
3991 */
3992 if ((*arg)[0] != ':')
3993 {
3994 EMSG(_("E109: Missing ':' after '?'"));
3995 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003996 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 return FAIL;
3998 }
3999
4000 /*
4001 * Get the third variable.
4002 */
4003 *arg = skipwhite(*arg + 1);
4004 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4005 {
4006 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 return FAIL;
4009 }
4010 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004011 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013
4014 return OK;
4015}
4016
4017/*
4018 * Handle first level expression:
4019 * expr2 || expr2 || expr2 logical OR
4020 *
4021 * "arg" must point to the first non-white of the expression.
4022 * "arg" is advanced to the next non-white after the recognized expression.
4023 *
4024 * Return OK or FAIL.
4025 */
4026 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 int evaluate;
4031{
Bram Moolenaar33570922005-01-25 22:26:29 +00004032 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 long result;
4034 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004035 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036
4037 /*
4038 * Get the first variable.
4039 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004040 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 return FAIL;
4042
4043 /*
4044 * Repeat until there is no following "||".
4045 */
4046 first = TRUE;
4047 result = FALSE;
4048 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4049 {
4050 if (evaluate && first)
4051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004052 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004054 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004055 if (error)
4056 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 first = FALSE;
4058 }
4059
4060 /*
4061 * Get the second variable.
4062 */
4063 *arg = skipwhite(*arg + 2);
4064 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4065 return FAIL;
4066
4067 /*
4068 * Compute the result.
4069 */
4070 if (evaluate && !result)
4071 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004072 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004075 if (error)
4076 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
4078 if (evaluate)
4079 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080 rettv->v_type = VAR_NUMBER;
4081 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
4083 }
4084
4085 return OK;
4086}
4087
4088/*
4089 * Handle second level expression:
4090 * expr3 && expr3 && expr3 logical AND
4091 *
4092 * "arg" must point to the first non-white of the expression.
4093 * "arg" is advanced to the next non-white after the recognized expression.
4094 *
4095 * Return OK or FAIL.
4096 */
4097 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 int evaluate;
4102{
Bram Moolenaar33570922005-01-25 22:26:29 +00004103 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 long result;
4105 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004106 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
4108 /*
4109 * Get the first variable.
4110 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 return FAIL;
4113
4114 /*
4115 * Repeat until there is no following "&&".
4116 */
4117 first = TRUE;
4118 result = TRUE;
4119 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4120 {
4121 if (evaluate && first)
4122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004123 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004126 if (error)
4127 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 first = FALSE;
4129 }
4130
4131 /*
4132 * Get the second variable.
4133 */
4134 *arg = skipwhite(*arg + 2);
4135 if (eval4(arg, &var2, evaluate && result) == FAIL)
4136 return FAIL;
4137
4138 /*
4139 * Compute the result.
4140 */
4141 if (evaluate && result)
4142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004143 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004146 if (error)
4147 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149 if (evaluate)
4150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 rettv->v_type = VAR_NUMBER;
4152 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
4154 }
4155
4156 return OK;
4157}
4158
4159/*
4160 * Handle third level expression:
4161 * var1 == var2
4162 * var1 =~ var2
4163 * var1 != var2
4164 * var1 !~ var2
4165 * var1 > var2
4166 * var1 >= var2
4167 * var1 < var2
4168 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004169 * var1 is var2
4170 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 *
4172 * "arg" must point to the first non-white of the expression.
4173 * "arg" is advanced to the next non-white after the recognized expression.
4174 *
4175 * Return OK or FAIL.
4176 */
4177 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 int evaluate;
4182{
Bram Moolenaar33570922005-01-25 22:26:29 +00004183 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 char_u *p;
4185 int i;
4186 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004187 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 int len = 2;
4189 long n1, n2;
4190 char_u *s1, *s2;
4191 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4192 regmatch_T regmatch;
4193 int ic;
4194 char_u *save_cpo;
4195
4196 /*
4197 * Get the first variable.
4198 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 return FAIL;
4201
4202 p = *arg;
4203 switch (p[0])
4204 {
4205 case '=': if (p[1] == '=')
4206 type = TYPE_EQUAL;
4207 else if (p[1] == '~')
4208 type = TYPE_MATCH;
4209 break;
4210 case '!': if (p[1] == '=')
4211 type = TYPE_NEQUAL;
4212 else if (p[1] == '~')
4213 type = TYPE_NOMATCH;
4214 break;
4215 case '>': if (p[1] != '=')
4216 {
4217 type = TYPE_GREATER;
4218 len = 1;
4219 }
4220 else
4221 type = TYPE_GEQUAL;
4222 break;
4223 case '<': if (p[1] != '=')
4224 {
4225 type = TYPE_SMALLER;
4226 len = 1;
4227 }
4228 else
4229 type = TYPE_SEQUAL;
4230 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004231 case 'i': if (p[1] == 's')
4232 {
4233 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4234 len = 5;
4235 if (!vim_isIDc(p[len]))
4236 {
4237 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4238 type_is = TRUE;
4239 }
4240 }
4241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243
4244 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004245 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 */
4247 if (type != TYPE_UNKNOWN)
4248 {
4249 /* extra question mark appended: ignore case */
4250 if (p[len] == '?')
4251 {
4252 ic = TRUE;
4253 ++len;
4254 }
4255 /* extra '#' appended: match case */
4256 else if (p[len] == '#')
4257 {
4258 ic = FALSE;
4259 ++len;
4260 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004261 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 else
4263 ic = p_ic;
4264
4265 /*
4266 * Get the second variable.
4267 */
4268 *arg = skipwhite(p + len);
4269 if (eval5(arg, &var2, evaluate) == FAIL)
4270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 return FAIL;
4273 }
4274
4275 if (evaluate)
4276 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004277 if (type_is && rettv->v_type != var2.v_type)
4278 {
4279 /* For "is" a different type always means FALSE, for "notis"
4280 * it means TRUE. */
4281 n1 = (type == TYPE_NEQUAL);
4282 }
4283 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4284 {
4285 if (type_is)
4286 {
4287 n1 = (rettv->v_type == var2.v_type
4288 && rettv->vval.v_list == var2.vval.v_list);
4289 if (type == TYPE_NEQUAL)
4290 n1 = !n1;
4291 }
4292 else if (rettv->v_type != var2.v_type
4293 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4294 {
4295 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004296 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004297 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004298 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004299 clear_tv(rettv);
4300 clear_tv(&var2);
4301 return FAIL;
4302 }
4303 else
4304 {
4305 /* Compare two Lists for being equal or unequal. */
4306 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4307 if (type == TYPE_NEQUAL)
4308 n1 = !n1;
4309 }
4310 }
4311
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004312 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4313 {
4314 if (type_is)
4315 {
4316 n1 = (rettv->v_type == var2.v_type
4317 && rettv->vval.v_dict == var2.vval.v_dict);
4318 if (type == TYPE_NEQUAL)
4319 n1 = !n1;
4320 }
4321 else if (rettv->v_type != var2.v_type
4322 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4323 {
4324 if (rettv->v_type != var2.v_type)
4325 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4326 else
4327 EMSG(_("E736: Invalid operation for Dictionary"));
4328 clear_tv(rettv);
4329 clear_tv(&var2);
4330 return FAIL;
4331 }
4332 else
4333 {
4334 /* Compare two Dictionaries for being equal or unequal. */
4335 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4336 if (type == TYPE_NEQUAL)
4337 n1 = !n1;
4338 }
4339 }
4340
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004341 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4342 {
4343 if (rettv->v_type != var2.v_type
4344 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4345 {
4346 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004347 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004348 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004349 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004350 clear_tv(rettv);
4351 clear_tv(&var2);
4352 return FAIL;
4353 }
4354 else
4355 {
4356 /* Compare two Funcrefs for being equal or unequal. */
4357 if (rettv->vval.v_string == NULL
4358 || var2.vval.v_string == NULL)
4359 n1 = FALSE;
4360 else
4361 n1 = STRCMP(rettv->vval.v_string,
4362 var2.vval.v_string) == 0;
4363 if (type == TYPE_NEQUAL)
4364 n1 = !n1;
4365 }
4366 }
4367
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004368#ifdef FEAT_FLOAT
4369 /*
4370 * If one of the two variables is a float, compare as a float.
4371 * When using "=~" or "!~", always compare as string.
4372 */
4373 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4374 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4375 {
4376 float_T f1, f2;
4377
4378 if (rettv->v_type == VAR_FLOAT)
4379 f1 = rettv->vval.v_float;
4380 else
4381 f1 = get_tv_number(rettv);
4382 if (var2.v_type == VAR_FLOAT)
4383 f2 = var2.vval.v_float;
4384 else
4385 f2 = get_tv_number(&var2);
4386 n1 = FALSE;
4387 switch (type)
4388 {
4389 case TYPE_EQUAL: n1 = (f1 == f2); break;
4390 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4391 case TYPE_GREATER: n1 = (f1 > f2); break;
4392 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4393 case TYPE_SMALLER: n1 = (f1 < f2); break;
4394 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4395 case TYPE_UNKNOWN:
4396 case TYPE_MATCH:
4397 case TYPE_NOMATCH: break; /* avoid gcc warning */
4398 }
4399 }
4400#endif
4401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 /*
4403 * If one of the two variables is a number, compare as a number.
4404 * When using "=~" or "!~", always compare as string.
4405 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004406 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 n1 = get_tv_number(rettv);
4410 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 switch (type)
4412 {
4413 case TYPE_EQUAL: n1 = (n1 == n2); break;
4414 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4415 case TYPE_GREATER: n1 = (n1 > n2); break;
4416 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4417 case TYPE_SMALLER: n1 = (n1 < n2); break;
4418 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4419 case TYPE_UNKNOWN:
4420 case TYPE_MATCH:
4421 case TYPE_NOMATCH: break; /* avoid gcc warning */
4422 }
4423 }
4424 else
4425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004426 s1 = get_tv_string_buf(rettv, buf1);
4427 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4429 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4430 else
4431 i = 0;
4432 n1 = FALSE;
4433 switch (type)
4434 {
4435 case TYPE_EQUAL: n1 = (i == 0); break;
4436 case TYPE_NEQUAL: n1 = (i != 0); break;
4437 case TYPE_GREATER: n1 = (i > 0); break;
4438 case TYPE_GEQUAL: n1 = (i >= 0); break;
4439 case TYPE_SMALLER: n1 = (i < 0); break;
4440 case TYPE_SEQUAL: n1 = (i <= 0); break;
4441
4442 case TYPE_MATCH:
4443 case TYPE_NOMATCH:
4444 /* avoid 'l' flag in 'cpoptions' */
4445 save_cpo = p_cpo;
4446 p_cpo = (char_u *)"";
4447 regmatch.regprog = vim_regcomp(s2,
4448 RE_MAGIC + RE_STRING);
4449 regmatch.rm_ic = ic;
4450 if (regmatch.regprog != NULL)
4451 {
4452 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4453 vim_free(regmatch.regprog);
4454 if (type == TYPE_NOMATCH)
4455 n1 = !n1;
4456 }
4457 p_cpo = save_cpo;
4458 break;
4459
4460 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4461 }
4462 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004463 clear_tv(rettv);
4464 clear_tv(&var2);
4465 rettv->v_type = VAR_NUMBER;
4466 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468 }
4469
4470 return OK;
4471}
4472
4473/*
4474 * Handle fourth level expression:
4475 * + number addition
4476 * - number subtraction
4477 * . string concatenation
4478 *
4479 * "arg" must point to the first non-white of the expression.
4480 * "arg" is advanced to the next non-white after the recognized expression.
4481 *
4482 * Return OK or FAIL.
4483 */
4484 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004485eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 int evaluate;
4489{
Bram Moolenaar33570922005-01-25 22:26:29 +00004490 typval_T var2;
4491 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 int op;
4493 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004494#ifdef FEAT_FLOAT
4495 float_T f1 = 0, f2 = 0;
4496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 char_u *s1, *s2;
4498 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4499 char_u *p;
4500
4501 /*
4502 * Get the first variable.
4503 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004504 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 return FAIL;
4506
4507 /*
4508 * Repeat computing, until no '+', '-' or '.' is following.
4509 */
4510 for (;;)
4511 {
4512 op = **arg;
4513 if (op != '+' && op != '-' && op != '.')
4514 break;
4515
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004516 if ((op != '+' || rettv->v_type != VAR_LIST)
4517#ifdef FEAT_FLOAT
4518 && (op == '.' || rettv->v_type != VAR_FLOAT)
4519#endif
4520 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004521 {
4522 /* For "list + ...", an illegal use of the first operand as
4523 * a number cannot be determined before evaluating the 2nd
4524 * operand: if this is also a list, all is ok.
4525 * For "something . ...", "something - ..." or "non-list + ...",
4526 * we know that the first operand needs to be a string or number
4527 * without evaluating the 2nd operand. So check before to avoid
4528 * side effects after an error. */
4529 if (evaluate && get_tv_string_chk(rettv) == NULL)
4530 {
4531 clear_tv(rettv);
4532 return FAIL;
4533 }
4534 }
4535
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 /*
4537 * Get the second variable.
4538 */
4539 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004540 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004542 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 return FAIL;
4544 }
4545
4546 if (evaluate)
4547 {
4548 /*
4549 * Compute the result.
4550 */
4551 if (op == '.')
4552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004553 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4554 s2 = get_tv_string_buf_chk(&var2, buf2);
4555 if (s2 == NULL) /* type error ? */
4556 {
4557 clear_tv(rettv);
4558 clear_tv(&var2);
4559 return FAIL;
4560 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004561 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004562 clear_tv(rettv);
4563 rettv->v_type = VAR_STRING;
4564 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004566 else if (op == '+' && rettv->v_type == VAR_LIST
4567 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004568 {
4569 /* concatenate Lists */
4570 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4571 &var3) == FAIL)
4572 {
4573 clear_tv(rettv);
4574 clear_tv(&var2);
4575 return FAIL;
4576 }
4577 clear_tv(rettv);
4578 *rettv = var3;
4579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 else
4581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004582 int error = FALSE;
4583
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004584#ifdef FEAT_FLOAT
4585 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004586 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004587 f1 = rettv->vval.v_float;
4588 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004589 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004590 else
4591#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004592 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004593 n1 = get_tv_number_chk(rettv, &error);
4594 if (error)
4595 {
4596 /* This can only happen for "list + non-list". For
4597 * "non-list + ..." or "something - ...", we returned
4598 * before evaluating the 2nd operand. */
4599 clear_tv(rettv);
4600 return FAIL;
4601 }
4602#ifdef FEAT_FLOAT
4603 if (var2.v_type == VAR_FLOAT)
4604 f1 = n1;
4605#endif
4606 }
4607#ifdef FEAT_FLOAT
4608 if (var2.v_type == VAR_FLOAT)
4609 {
4610 f2 = var2.vval.v_float;
4611 n2 = 0;
4612 }
4613 else
4614#endif
4615 {
4616 n2 = get_tv_number_chk(&var2, &error);
4617 if (error)
4618 {
4619 clear_tv(rettv);
4620 clear_tv(&var2);
4621 return FAIL;
4622 }
4623#ifdef FEAT_FLOAT
4624 if (rettv->v_type == VAR_FLOAT)
4625 f2 = n2;
4626#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004629
4630#ifdef FEAT_FLOAT
4631 /* If there is a float on either side the result is a float. */
4632 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4633 {
4634 if (op == '+')
4635 f1 = f1 + f2;
4636 else
4637 f1 = f1 - f2;
4638 rettv->v_type = VAR_FLOAT;
4639 rettv->vval.v_float = f1;
4640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004642#endif
4643 {
4644 if (op == '+')
4645 n1 = n1 + n2;
4646 else
4647 n1 = n1 - n2;
4648 rettv->v_type = VAR_NUMBER;
4649 rettv->vval.v_number = n1;
4650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 }
4654 }
4655 return OK;
4656}
4657
4658/*
4659 * Handle fifth level expression:
4660 * * number multiplication
4661 * / number division
4662 * % number modulo
4663 *
4664 * "arg" must point to the first non-white of the expression.
4665 * "arg" is advanced to the next non-white after the recognized expression.
4666 *
4667 * Return OK or FAIL.
4668 */
4669 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004670eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004674 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
Bram Moolenaar33570922005-01-25 22:26:29 +00004676 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 int op;
4678 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004679#ifdef FEAT_FLOAT
4680 int use_float = FALSE;
4681 float_T f1 = 0, f2;
4682#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004683 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684
4685 /*
4686 * Get the first variable.
4687 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004688 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 return FAIL;
4690
4691 /*
4692 * Repeat computing, until no '*', '/' or '%' is following.
4693 */
4694 for (;;)
4695 {
4696 op = **arg;
4697 if (op != '*' && op != '/' && op != '%')
4698 break;
4699
4700 if (evaluate)
4701 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004702#ifdef FEAT_FLOAT
4703 if (rettv->v_type == VAR_FLOAT)
4704 {
4705 f1 = rettv->vval.v_float;
4706 use_float = TRUE;
4707 n1 = 0;
4708 }
4709 else
4710#endif
4711 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004712 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004713 if (error)
4714 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 }
4716 else
4717 n1 = 0;
4718
4719 /*
4720 * Get the second variable.
4721 */
4722 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004723 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 return FAIL;
4725
4726 if (evaluate)
4727 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728#ifdef FEAT_FLOAT
4729 if (var2.v_type == VAR_FLOAT)
4730 {
4731 if (!use_float)
4732 {
4733 f1 = n1;
4734 use_float = TRUE;
4735 }
4736 f2 = var2.vval.v_float;
4737 n2 = 0;
4738 }
4739 else
4740#endif
4741 {
4742 n2 = get_tv_number_chk(&var2, &error);
4743 clear_tv(&var2);
4744 if (error)
4745 return FAIL;
4746#ifdef FEAT_FLOAT
4747 if (use_float)
4748 f2 = n2;
4749#endif
4750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
4752 /*
4753 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004754 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004756#ifdef FEAT_FLOAT
4757 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004759 if (op == '*')
4760 f1 = f1 * f2;
4761 else if (op == '/')
4762 {
4763 /* We rely on the floating point library to handle divide
4764 * by zero to result in "inf" and not a crash. */
4765 f1 = f1 / f2;
4766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004769 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004770 return FAIL;
4771 }
4772 rettv->v_type = VAR_FLOAT;
4773 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 }
4775 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004778 if (op == '*')
4779 n1 = n1 * n2;
4780 else if (op == '/')
4781 {
4782 if (n2 == 0) /* give an error message? */
4783 {
4784 if (n1 == 0)
4785 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4786 else if (n1 < 0)
4787 n1 = -0x7fffffffL;
4788 else
4789 n1 = 0x7fffffffL;
4790 }
4791 else
4792 n1 = n1 / n2;
4793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 {
4796 if (n2 == 0) /* give an error message? */
4797 n1 = 0;
4798 else
4799 n1 = n1 % n2;
4800 }
4801 rettv->v_type = VAR_NUMBER;
4802 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805 }
4806
4807 return OK;
4808}
4809
4810/*
4811 * Handle sixth level expression:
4812 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004813 * "string" string constant
4814 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 * &option-name option value
4816 * @r register contents
4817 * identifier variable value
4818 * function() function call
4819 * $VAR environment variable
4820 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004821 * [expr, expr] List
4822 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 *
4824 * Also handle:
4825 * ! in front logical NOT
4826 * - in front unary minus
4827 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004828 * trailing [] subscript in String or List
4829 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 *
4831 * "arg" must point to the first non-white of the expression.
4832 * "arg" is advanced to the next non-white after the recognized expression.
4833 *
4834 * Return OK or FAIL.
4835 */
4836 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004837eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004841 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 long n;
4844 int len;
4845 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 char_u *start_leader, *end_leader;
4847 int ret = OK;
4848 char_u *alias;
4849
4850 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004852 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
4856 /*
4857 * Skip '!' and '-' characters. They are handled later.
4858 */
4859 start_leader = *arg;
4860 while (**arg == '!' || **arg == '-' || **arg == '+')
4861 *arg = skipwhite(*arg + 1);
4862 end_leader = *arg;
4863
4864 switch (**arg)
4865 {
4866 /*
4867 * Number constant.
4868 */
4869 case '0':
4870 case '1':
4871 case '2':
4872 case '3':
4873 case '4':
4874 case '5':
4875 case '6':
4876 case '7':
4877 case '8':
4878 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879 {
4880#ifdef FEAT_FLOAT
4881 char_u *p = skipdigits(*arg + 1);
4882 int get_float = FALSE;
4883
4884 /* We accept a float when the format matches
4885 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004886 * strict to avoid backwards compatibility problems.
4887 * Don't look for a float after the "." operator, so that
4888 * ":let vers = 1.2.3" doesn't fail. */
4889 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891 get_float = TRUE;
4892 p = skipdigits(p + 2);
4893 if (*p == 'e' || *p == 'E')
4894 {
4895 ++p;
4896 if (*p == '-' || *p == '+')
4897 ++p;
4898 if (!vim_isdigit(*p))
4899 get_float = FALSE;
4900 else
4901 p = skipdigits(p + 1);
4902 }
4903 if (ASCII_ISALPHA(*p) || *p == '.')
4904 get_float = FALSE;
4905 }
4906 if (get_float)
4907 {
4908 float_T f;
4909
4910 *arg += string2float(*arg, &f);
4911 if (evaluate)
4912 {
4913 rettv->v_type = VAR_FLOAT;
4914 rettv->vval.v_float = f;
4915 }
4916 }
4917 else
4918#endif
4919 {
4920 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4921 *arg += len;
4922 if (evaluate)
4923 {
4924 rettv->v_type = VAR_NUMBER;
4925 rettv->vval.v_number = n;
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
4931 /*
4932 * String constant: "string".
4933 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004934 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 break;
4936
4937 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004938 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004940 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004941 break;
4942
4943 /*
4944 * List: [expr, expr]
4945 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004946 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 break;
4948
4949 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004950 * Dictionary: {key: val, key: val}
4951 */
4952 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4953 break;
4954
4955 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004956 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004958 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 break;
4960
4961 /*
4962 * Environment variable: $VAR.
4963 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 break;
4966
4967 /*
4968 * Register contents: @r.
4969 */
4970 case '@': ++*arg;
4971 if (evaluate)
4972 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004973 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004974 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976 if (**arg != NUL)
4977 ++*arg;
4978 break;
4979
4980 /*
4981 * nested expression: (expression).
4982 */
4983 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004984 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (**arg == ')')
4986 ++*arg;
4987 else if (ret == OK)
4988 {
4989 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004990 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 ret = FAIL;
4992 }
4993 break;
4994
Bram Moolenaar8c711452005-01-14 21:53:12 +00004995 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 break;
4997 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998
4999 if (ret == NOTDONE)
5000 {
5001 /*
5002 * Must be a variable or function name.
5003 * Can also be a curly-braces kind of name: {expr}.
5004 */
5005 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005006 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007 if (alias != NULL)
5008 s = alias;
5009
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005010 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 ret = FAIL;
5012 else
5013 {
5014 if (**arg == '(') /* recursive! */
5015 {
5016 /* If "s" is the name of a variable of type VAR_FUNC
5017 * use its contents. */
5018 s = deref_func_name(s, &len);
5019
5020 /* Invoke the function. */
5021 ret = get_func_tv(s, len, rettv, arg,
5022 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005023 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005024 /* Stop the expression evaluation when immediately
5025 * aborting on error, or when an interrupt occurred or
5026 * an exception was thrown but not caught. */
5027 if (aborting())
5028 {
5029 if (ret == OK)
5030 clear_tv(rettv);
5031 ret = FAIL;
5032 }
5033 }
5034 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005035 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005036 else
5037 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 }
5039
5040 if (alias != NULL)
5041 vim_free(alias);
5042 }
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 *arg = skipwhite(*arg);
5045
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005046 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5047 * expr(expr). */
5048 if (ret == OK)
5049 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050
5051 /*
5052 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5053 */
5054 if (ret == OK && evaluate && end_leader > start_leader)
5055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005056 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005057 int val = 0;
5058#ifdef FEAT_FLOAT
5059 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005060
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005061 if (rettv->v_type == VAR_FLOAT)
5062 f = rettv->vval.v_float;
5063 else
5064#endif
5065 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005066 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005068 clear_tv(rettv);
5069 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005071 else
5072 {
5073 while (end_leader > start_leader)
5074 {
5075 --end_leader;
5076 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005077 {
5078#ifdef FEAT_FLOAT
5079 if (rettv->v_type == VAR_FLOAT)
5080 f = !f;
5081 else
5082#endif
5083 val = !val;
5084 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005085 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005086 {
5087#ifdef FEAT_FLOAT
5088 if (rettv->v_type == VAR_FLOAT)
5089 f = -f;
5090 else
5091#endif
5092 val = -val;
5093 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005094 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005095#ifdef FEAT_FLOAT
5096 if (rettv->v_type == VAR_FLOAT)
5097 {
5098 clear_tv(rettv);
5099 rettv->vval.v_float = f;
5100 }
5101 else
5102#endif
5103 {
5104 clear_tv(rettv);
5105 rettv->v_type = VAR_NUMBER;
5106 rettv->vval.v_number = val;
5107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
5110
5111 return ret;
5112}
5113
5114/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005115 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5116 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005117 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5118 */
5119 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005120eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005121 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005122 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005123 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005124 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005125{
5126 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005127 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005128 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129 long len = -1;
5130 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005131 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005133
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005134 if (rettv->v_type == VAR_FUNC
5135#ifdef FEAT_FLOAT
5136 || rettv->v_type == VAR_FLOAT
5137#endif
5138 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005140 if (verbose)
5141 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142 return FAIL;
5143 }
5144
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005146 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 /*
5148 * dict.name
5149 */
5150 key = *arg + 1;
5151 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5152 ;
5153 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005156 }
5157 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 /*
5160 * something[idx]
5161 *
5162 * Get the (first) variable from inside the [].
5163 */
5164 *arg = skipwhite(*arg + 1);
5165 if (**arg == ':')
5166 empty1 = TRUE;
5167 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5168 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005169 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5170 {
5171 /* not a number or string */
5172 clear_tv(&var1);
5173 return FAIL;
5174 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175
5176 /*
5177 * Get the second variable from inside the [:].
5178 */
5179 if (**arg == ':')
5180 {
5181 range = TRUE;
5182 *arg = skipwhite(*arg + 1);
5183 if (**arg == ']')
5184 empty2 = TRUE;
5185 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005187 if (!empty1)
5188 clear_tv(&var1);
5189 return FAIL;
5190 }
5191 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5192 {
5193 /* not a number or string */
5194 if (!empty1)
5195 clear_tv(&var1);
5196 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 return FAIL;
5198 }
5199 }
5200
5201 /* Check for the ']'. */
5202 if (**arg != ']')
5203 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005204 if (verbose)
5205 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 clear_tv(&var1);
5207 if (range)
5208 clear_tv(&var2);
5209 return FAIL;
5210 }
5211 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005212 }
5213
5214 if (evaluate)
5215 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 n1 = 0;
5217 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005218 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005219 n1 = get_tv_number(&var1);
5220 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005221 }
5222 if (range)
5223 {
5224 if (empty2)
5225 n2 = -1;
5226 else
5227 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005228 n2 = get_tv_number(&var2);
5229 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230 }
5231 }
5232
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005233 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005234 {
5235 case VAR_NUMBER:
5236 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005237 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238 len = (long)STRLEN(s);
5239 if (range)
5240 {
5241 /* The resulting variable is a substring. If the indexes
5242 * are out of range the result is empty. */
5243 if (n1 < 0)
5244 {
5245 n1 = len + n1;
5246 if (n1 < 0)
5247 n1 = 0;
5248 }
5249 if (n2 < 0)
5250 n2 = len + n2;
5251 else if (n2 >= len)
5252 n2 = len;
5253 if (n1 >= len || n2 < 0 || n1 > n2)
5254 s = NULL;
5255 else
5256 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5257 }
5258 else
5259 {
5260 /* The resulting variable is a string of a single
5261 * character. If the index is too big or negative the
5262 * result is empty. */
5263 if (n1 >= len || n1 < 0)
5264 s = NULL;
5265 else
5266 s = vim_strnsave(s + n1, 1);
5267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005268 clear_tv(rettv);
5269 rettv->v_type = VAR_STRING;
5270 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 break;
5272
5273 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 if (n1 < 0)
5276 n1 = len + n1;
5277 if (!empty1 && (n1 < 0 || n1 >= len))
5278 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005279 /* For a range we allow invalid values and return an empty
5280 * list. A list index out of range is an error. */
5281 if (!range)
5282 {
5283 if (verbose)
5284 EMSGN(_(e_listidx), n1);
5285 return FAIL;
5286 }
5287 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 }
5289 if (range)
5290 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005291 list_T *l;
5292 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293
5294 if (n2 < 0)
5295 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005296 else if (n2 >= len)
5297 n2 = len - 1;
5298 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005299 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 l = list_alloc();
5301 if (l == NULL)
5302 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005303 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 n1 <= n2; ++n1)
5305 {
5306 if (list_append_tv(l, &item->li_tv) == FAIL)
5307 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005308 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 return FAIL;
5310 }
5311 item = item->li_next;
5312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005313 clear_tv(rettv);
5314 rettv->v_type = VAR_LIST;
5315 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005316 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 }
5318 else
5319 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005320 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005321 clear_tv(rettv);
5322 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005323 }
5324 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325
5326 case VAR_DICT:
5327 if (range)
5328 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005329 if (verbose)
5330 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331 if (len == -1)
5332 clear_tv(&var1);
5333 return FAIL;
5334 }
5335 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005336 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005337
5338 if (len == -1)
5339 {
5340 key = get_tv_string(&var1);
5341 if (*key == NUL)
5342 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005343 if (verbose)
5344 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005345 clear_tv(&var1);
5346 return FAIL;
5347 }
5348 }
5349
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005350 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005352 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005353 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005354 if (len == -1)
5355 clear_tv(&var1);
5356 if (item == NULL)
5357 return FAIL;
5358
5359 copy_tv(&item->di_tv, &var1);
5360 clear_tv(rettv);
5361 *rettv = var1;
5362 }
5363 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 }
5365 }
5366
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 return OK;
5368}
5369
5370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 * Get an option value.
5372 * "arg" points to the '&' or '+' before the option name.
5373 * "arg" is advanced to character after the option name.
5374 * Return OK or FAIL.
5375 */
5376 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005377get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005379 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 int evaluate;
5381{
5382 char_u *option_end;
5383 long numval;
5384 char_u *stringval;
5385 int opt_type;
5386 int c;
5387 int working = (**arg == '+'); /* has("+option") */
5388 int ret = OK;
5389 int opt_flags;
5390
5391 /*
5392 * Isolate the option name and find its value.
5393 */
5394 option_end = find_option_end(arg, &opt_flags);
5395 if (option_end == NULL)
5396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005397 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 EMSG2(_("E112: Option name missing: %s"), *arg);
5399 return FAIL;
5400 }
5401
5402 if (!evaluate)
5403 {
5404 *arg = option_end;
5405 return OK;
5406 }
5407
5408 c = *option_end;
5409 *option_end = NUL;
5410 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005411 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
5413 if (opt_type == -3) /* invalid name */
5414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 EMSG2(_("E113: Unknown option: %s"), *arg);
5417 ret = FAIL;
5418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005419 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 {
5421 if (opt_type == -2) /* hidden string option */
5422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005423 rettv->v_type = VAR_STRING;
5424 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 }
5426 else if (opt_type == -1) /* hidden number option */
5427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005428 rettv->v_type = VAR_NUMBER;
5429 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431 else if (opt_type == 1) /* number option */
5432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433 rettv->v_type = VAR_NUMBER;
5434 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 }
5436 else /* string option */
5437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438 rettv->v_type = VAR_STRING;
5439 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
5441 }
5442 else if (working && (opt_type == -2 || opt_type == -1))
5443 ret = FAIL;
5444
5445 *option_end = c; /* put back for error messages */
5446 *arg = option_end;
5447
5448 return ret;
5449}
5450
5451/*
5452 * Allocate a variable for a string constant.
5453 * Return OK or FAIL.
5454 */
5455 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 int evaluate;
5460{
5461 char_u *p;
5462 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 int extra = 0;
5464
5465 /*
5466 * Find the end of the string, skipping backslashed characters.
5467 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005468 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {
5470 if (*p == '\\' && p[1] != NUL)
5471 {
5472 ++p;
5473 /* A "\<x>" form occupies at least 4 characters, and produces up
5474 * to 6 characters: reserve space for 2 extra */
5475 if (*p == '<')
5476 extra += 2;
5477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 }
5479
5480 if (*p != '"')
5481 {
5482 EMSG2(_("E114: Missing quote: %s"), *arg);
5483 return FAIL;
5484 }
5485
5486 /* If only parsing, set *arg and return here */
5487 if (!evaluate)
5488 {
5489 *arg = p + 1;
5490 return OK;
5491 }
5492
5493 /*
5494 * Copy the string into allocated memory, handling backslashed
5495 * characters.
5496 */
5497 name = alloc((unsigned)(p - *arg + extra));
5498 if (name == NULL)
5499 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005500 rettv->v_type = VAR_STRING;
5501 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502
Bram Moolenaar8c711452005-01-14 21:53:12 +00005503 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
5505 if (*p == '\\')
5506 {
5507 switch (*++p)
5508 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005509 case 'b': *name++ = BS; ++p; break;
5510 case 'e': *name++ = ESC; ++p; break;
5511 case 'f': *name++ = FF; ++p; break;
5512 case 'n': *name++ = NL; ++p; break;
5513 case 'r': *name++ = CAR; ++p; break;
5514 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515
5516 case 'X': /* hex: "\x1", "\x12" */
5517 case 'x':
5518 case 'u': /* Unicode: "\u0023" */
5519 case 'U':
5520 if (vim_isxdigit(p[1]))
5521 {
5522 int n, nr;
5523 int c = toupper(*p);
5524
5525 if (c == 'X')
5526 n = 2;
5527 else
5528 n = 4;
5529 nr = 0;
5530 while (--n >= 0 && vim_isxdigit(p[1]))
5531 {
5532 ++p;
5533 nr = (nr << 4) + hex2nr(*p);
5534 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005535 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536#ifdef FEAT_MBYTE
5537 /* For "\u" store the number according to
5538 * 'encoding'. */
5539 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005540 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 else
5542#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005543 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 break;
5546
5547 /* octal: "\1", "\12", "\123" */
5548 case '0':
5549 case '1':
5550 case '2':
5551 case '3':
5552 case '4':
5553 case '5':
5554 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005555 case '7': *name = *p++ - '0';
5556 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005558 *name = (*name << 3) + *p++ - '0';
5559 if (*p >= '0' && *p <= '7')
5560 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 break;
5564
5565 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005566 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 if (extra != 0)
5568 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005569 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 break;
5571 }
5572 /* FALLTHROUGH */
5573
Bram Moolenaar8c711452005-01-14 21:53:12 +00005574 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 break;
5576 }
5577 }
5578 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005579 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005582 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 *arg = p + 1;
5584
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 return OK;
5586}
5587
5588/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 * Return OK or FAIL.
5591 */
5592 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 int evaluate;
5597{
5598 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005599 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005600 int reduce = 0;
5601
5602 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005604 */
5605 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5606 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005608 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005610 break;
5611 ++reduce;
5612 ++p;
5613 }
5614 }
5615
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005617 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005618 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005619 return FAIL;
5620 }
5621
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005623 if (!evaluate)
5624 {
5625 *arg = p + 1;
5626 return OK;
5627 }
5628
5629 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005631 */
5632 str = alloc((unsigned)((p - *arg) - reduce));
5633 if (str == NULL)
5634 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 rettv->v_type = VAR_STRING;
5636 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005637
Bram Moolenaar8c711452005-01-14 21:53:12 +00005638 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005639 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005642 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005643 break;
5644 ++p;
5645 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005649 *arg = p + 1;
5650
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005651 return OK;
5652}
5653
5654/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005655 * Allocate a variable for a List and fill it from "*arg".
5656 * Return OK or FAIL.
5657 */
5658 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005660 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005661 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005662 int evaluate;
5663{
Bram Moolenaar33570922005-01-25 22:26:29 +00005664 list_T *l = NULL;
5665 typval_T tv;
5666 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005667
5668 if (evaluate)
5669 {
5670 l = list_alloc();
5671 if (l == NULL)
5672 return FAIL;
5673 }
5674
5675 *arg = skipwhite(*arg + 1);
5676 while (**arg != ']' && **arg != NUL)
5677 {
5678 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5679 goto failret;
5680 if (evaluate)
5681 {
5682 item = listitem_alloc();
5683 if (item != NULL)
5684 {
5685 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005686 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687 list_append(l, item);
5688 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005689 else
5690 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005691 }
5692
5693 if (**arg == ']')
5694 break;
5695 if (**arg != ',')
5696 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698 goto failret;
5699 }
5700 *arg = skipwhite(*arg + 1);
5701 }
5702
5703 if (**arg != ']')
5704 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005706failret:
5707 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005708 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709 return FAIL;
5710 }
5711
5712 *arg = skipwhite(*arg + 1);
5713 if (evaluate)
5714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005715 rettv->v_type = VAR_LIST;
5716 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 ++l->lv_refcount;
5718 }
5719
5720 return OK;
5721}
5722
5723/*
5724 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005725 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005727 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728list_alloc()
5729{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005730 list_T *l;
5731
5732 l = (list_T *)alloc_clear(sizeof(list_T));
5733 if (l != NULL)
5734 {
5735 /* Prepend the list to the list of lists for garbage collection. */
5736 if (first_list != NULL)
5737 first_list->lv_used_prev = l;
5738 l->lv_used_prev = NULL;
5739 l->lv_used_next = first_list;
5740 first_list = l;
5741 }
5742 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743}
5744
5745/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005746 * Allocate an empty list for a return value.
5747 * Returns OK or FAIL.
5748 */
5749 static int
5750rettv_list_alloc(rettv)
5751 typval_T *rettv;
5752{
5753 list_T *l = list_alloc();
5754
5755 if (l == NULL)
5756 return FAIL;
5757
5758 rettv->vval.v_list = l;
5759 rettv->v_type = VAR_LIST;
5760 ++l->lv_refcount;
5761 return OK;
5762}
5763
5764/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005765 * Unreference a list: decrement the reference count and free it when it
5766 * becomes zero.
5767 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005768 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005770 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005772 if (l != NULL && --l->lv_refcount <= 0)
5773 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005774}
5775
5776/*
5777 * Free a list, including all items it points to.
5778 * Ignores the reference count.
5779 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005780 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005781list_free(l, recurse)
5782 list_T *l;
5783 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005784{
Bram Moolenaar33570922005-01-25 22:26:29 +00005785 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005786
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005787 /* Remove the list from the list of lists for garbage collection. */
5788 if (l->lv_used_prev == NULL)
5789 first_list = l->lv_used_next;
5790 else
5791 l->lv_used_prev->lv_used_next = l->lv_used_next;
5792 if (l->lv_used_next != NULL)
5793 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5794
Bram Moolenaard9fba312005-06-26 22:34:35 +00005795 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005797 /* Remove the item before deleting it. */
5798 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005799 if (recurse || (item->li_tv.v_type != VAR_LIST
5800 && item->li_tv.v_type != VAR_DICT))
5801 clear_tv(&item->li_tv);
5802 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 }
5804 vim_free(l);
5805}
5806
5807/*
5808 * Allocate a list item.
5809 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005810 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811listitem_alloc()
5812{
Bram Moolenaar33570922005-01-25 22:26:29 +00005813 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814}
5815
5816/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005817 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 */
5819 static void
5820listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005821 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005823 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 vim_free(item);
5825}
5826
5827/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005828 * Remove a list item from a List and free it. Also clears the value.
5829 */
5830 static void
5831listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005832 list_T *l;
5833 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005834{
5835 list_remove(l, item, item);
5836 listitem_free(item);
5837}
5838
5839/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840 * Get the number of items in a list.
5841 */
5842 static long
5843list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005844 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005845{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846 if (l == NULL)
5847 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005848 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005849}
5850
5851/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005852 * Return TRUE when two lists have exactly the same values.
5853 */
5854 static int
5855list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 list_T *l1;
5857 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005858 int ic; /* ignore case for strings */
5859{
Bram Moolenaar33570922005-01-25 22:26:29 +00005860 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005861
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005862 if (l1 == NULL || l2 == NULL)
5863 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005864 if (l1 == l2)
5865 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005866 if (list_len(l1) != list_len(l2))
5867 return FALSE;
5868
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005869 for (item1 = l1->lv_first, item2 = l2->lv_first;
5870 item1 != NULL && item2 != NULL;
5871 item1 = item1->li_next, item2 = item2->li_next)
5872 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5873 return FALSE;
5874 return item1 == NULL && item2 == NULL;
5875}
5876
Bram Moolenaar9e70cf12009-05-26 20:59:55 +00005877#if defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005878/*
5879 * Return the dictitem that an entry in a hashtable points to.
5880 */
5881 dictitem_T *
5882dict_lookup(hi)
5883 hashitem_T *hi;
5884{
5885 return HI2DI(hi);
5886}
5887#endif
5888
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005889/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005890 * Return TRUE when two dictionaries have exactly the same key/values.
5891 */
5892 static int
5893dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005894 dict_T *d1;
5895 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005896 int ic; /* ignore case for strings */
5897{
Bram Moolenaar33570922005-01-25 22:26:29 +00005898 hashitem_T *hi;
5899 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005900 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005901
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005902 if (d1 == NULL || d2 == NULL)
5903 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005904 if (d1 == d2)
5905 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005906 if (dict_len(d1) != dict_len(d2))
5907 return FALSE;
5908
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005909 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005910 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005911 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005912 if (!HASHITEM_EMPTY(hi))
5913 {
5914 item2 = dict_find(d2, hi->hi_key, -1);
5915 if (item2 == NULL)
5916 return FALSE;
5917 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5918 return FALSE;
5919 --todo;
5920 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005921 }
5922 return TRUE;
5923}
5924
5925/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005926 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005927 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005928 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005929 */
5930 static int
5931tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 typval_T *tv1;
5933 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005934 int ic; /* ignore case */
5935{
5936 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005937 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005938 static int recursive = 0; /* cach recursive loops */
5939 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005940
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005941 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005942 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005943 /* Catch lists and dicts that have an endless loop by limiting
5944 * recursiveness to 1000. We guess they are equal then. */
5945 if (recursive >= 1000)
5946 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005947
5948 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005949 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005950 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005951 ++recursive;
5952 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5953 --recursive;
5954 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005955
5956 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005957 ++recursive;
5958 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5959 --recursive;
5960 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005961
5962 case VAR_FUNC:
5963 return (tv1->vval.v_string != NULL
5964 && tv2->vval.v_string != NULL
5965 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5966
5967 case VAR_NUMBER:
5968 return tv1->vval.v_number == tv2->vval.v_number;
5969
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005970#ifdef FEAT_FLOAT
5971 case VAR_FLOAT:
5972 return tv1->vval.v_float == tv2->vval.v_float;
5973#endif
5974
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005975 case VAR_STRING:
5976 s1 = get_tv_string_buf(tv1, buf1);
5977 s2 = get_tv_string_buf(tv2, buf2);
5978 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005979 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005980
5981 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005982 return TRUE;
5983}
5984
5985/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986 * Locate item with index "n" in list "l" and return it.
5987 * A negative index is counted from the end; -1 is the last item.
5988 * Returns NULL when "n" is out of range.
5989 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005990 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005992 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993 long n;
5994{
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 long idx;
5997
5998 if (l == NULL)
5999 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006000
6001 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006003 n = l->lv_len + n;
6004
6005 /* Check for index out of range. */
6006 if (n < 0 || n >= l->lv_len)
6007 return NULL;
6008
6009 /* When there is a cached index may start search from there. */
6010 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006012 if (n < l->lv_idx / 2)
6013 {
6014 /* closest to the start of the list */
6015 item = l->lv_first;
6016 idx = 0;
6017 }
6018 else if (n > (l->lv_idx + l->lv_len) / 2)
6019 {
6020 /* closest to the end of the list */
6021 item = l->lv_last;
6022 idx = l->lv_len - 1;
6023 }
6024 else
6025 {
6026 /* closest to the cached index */
6027 item = l->lv_idx_item;
6028 idx = l->lv_idx;
6029 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 }
6031 else
6032 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006033 if (n < l->lv_len / 2)
6034 {
6035 /* closest to the start of the list */
6036 item = l->lv_first;
6037 idx = 0;
6038 }
6039 else
6040 {
6041 /* closest to the end of the list */
6042 item = l->lv_last;
6043 idx = l->lv_len - 1;
6044 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006046
6047 while (n > idx)
6048 {
6049 /* search forward */
6050 item = item->li_next;
6051 ++idx;
6052 }
6053 while (n < idx)
6054 {
6055 /* search backward */
6056 item = item->li_prev;
6057 --idx;
6058 }
6059
6060 /* cache the used index */
6061 l->lv_idx = idx;
6062 l->lv_idx_item = item;
6063
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 return item;
6065}
6066
6067/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006068 * Get list item "l[idx]" as a number.
6069 */
6070 static long
6071list_find_nr(l, idx, errorp)
6072 list_T *l;
6073 long idx;
6074 int *errorp; /* set to TRUE when something wrong */
6075{
6076 listitem_T *li;
6077
6078 li = list_find(l, idx);
6079 if (li == NULL)
6080 {
6081 if (errorp != NULL)
6082 *errorp = TRUE;
6083 return -1L;
6084 }
6085 return get_tv_number_chk(&li->li_tv, errorp);
6086}
6087
6088/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006089 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6090 */
6091 char_u *
6092list_find_str(l, idx)
6093 list_T *l;
6094 long idx;
6095{
6096 listitem_T *li;
6097
6098 li = list_find(l, idx - 1);
6099 if (li == NULL)
6100 {
6101 EMSGN(_(e_listidx), idx);
6102 return NULL;
6103 }
6104 return get_tv_string(&li->li_tv);
6105}
6106
6107/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006108 * Locate "item" list "l" and return its index.
6109 * Returns -1 when "item" is not in the list.
6110 */
6111 static long
6112list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006113 list_T *l;
6114 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006115{
6116 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006117 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006118
6119 if (l == NULL)
6120 return -1;
6121 idx = 0;
6122 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6123 ++idx;
6124 if (li == NULL)
6125 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006126 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006127}
6128
6129/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006130 * Append item "item" to the end of list "l".
6131 */
6132 static void
6133list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 list_T *l;
6135 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006136{
6137 if (l->lv_last == NULL)
6138 {
6139 /* empty list */
6140 l->lv_first = item;
6141 l->lv_last = item;
6142 item->li_prev = NULL;
6143 }
6144 else
6145 {
6146 l->lv_last->li_next = item;
6147 item->li_prev = l->lv_last;
6148 l->lv_last = item;
6149 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006150 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151 item->li_next = NULL;
6152}
6153
6154/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006156 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157 */
6158 static int
6159list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006160 list_T *l;
6161 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006163 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164
Bram Moolenaar05159a02005-02-26 23:04:13 +00006165 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006166 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006167 copy_tv(tv, &li->li_tv);
6168 list_append(l, li);
6169 return OK;
6170}
6171
6172/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006173 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006174 * Return FAIL when out of memory.
6175 */
6176 int
6177list_append_dict(list, dict)
6178 list_T *list;
6179 dict_T *dict;
6180{
6181 listitem_T *li = listitem_alloc();
6182
6183 if (li == NULL)
6184 return FAIL;
6185 li->li_tv.v_type = VAR_DICT;
6186 li->li_tv.v_lock = 0;
6187 li->li_tv.vval.v_dict = dict;
6188 list_append(list, li);
6189 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006190 return OK;
6191}
6192
6193/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006194 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006195 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006196 * Returns FAIL when out of memory.
6197 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006198 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006199list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006200 list_T *l;
6201 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006202 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006203{
6204 listitem_T *li = listitem_alloc();
6205
6206 if (li == NULL)
6207 return FAIL;
6208 list_append(l, li);
6209 li->li_tv.v_type = VAR_STRING;
6210 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006211 if (str == NULL)
6212 li->li_tv.vval.v_string = NULL;
6213 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006214 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006215 return FAIL;
6216 return OK;
6217}
6218
6219/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006220 * Append "n" to list "l".
6221 * Returns FAIL when out of memory.
6222 */
6223 static int
6224list_append_number(l, n)
6225 list_T *l;
6226 varnumber_T n;
6227{
6228 listitem_T *li;
6229
6230 li = listitem_alloc();
6231 if (li == NULL)
6232 return FAIL;
6233 li->li_tv.v_type = VAR_NUMBER;
6234 li->li_tv.v_lock = 0;
6235 li->li_tv.vval.v_number = n;
6236 list_append(l, li);
6237 return OK;
6238}
6239
6240/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242 * If "item" is NULL append at the end.
6243 * Return FAIL when out of memory.
6244 */
6245 static int
6246list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006247 list_T *l;
6248 typval_T *tv;
6249 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006250{
Bram Moolenaar33570922005-01-25 22:26:29 +00006251 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006252
6253 if (ni == NULL)
6254 return FAIL;
6255 copy_tv(tv, &ni->li_tv);
6256 if (item == NULL)
6257 /* Append new item at end of list. */
6258 list_append(l, ni);
6259 else
6260 {
6261 /* Insert new item before existing item. */
6262 ni->li_prev = item->li_prev;
6263 ni->li_next = item;
6264 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006265 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006266 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267 ++l->lv_idx;
6268 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006269 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006271 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006272 l->lv_idx_item = NULL;
6273 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006274 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006275 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006276 }
6277 return OK;
6278}
6279
6280/*
6281 * Extend "l1" with "l2".
6282 * If "bef" is NULL append at the end, otherwise insert before this item.
6283 * Returns FAIL when out of memory.
6284 */
6285 static int
6286list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 list_T *l1;
6288 list_T *l2;
6289 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006290{
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006292 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006293
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006294 /* We also quit the loop when we have inserted the original item count of
6295 * the list, avoid a hang when we extend a list with itself. */
6296 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006297 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6298 return FAIL;
6299 return OK;
6300}
6301
6302/*
6303 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6304 * Return FAIL when out of memory.
6305 */
6306 static int
6307list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006308 list_T *l1;
6309 list_T *l2;
6310 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006311{
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006313
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006314 if (l1 == NULL || l2 == NULL)
6315 return FAIL;
6316
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006317 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006318 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006319 if (l == NULL)
6320 return FAIL;
6321 tv->v_type = VAR_LIST;
6322 tv->vval.v_list = l;
6323
6324 /* append all items from the second list */
6325 return list_extend(l, l2, NULL);
6326}
6327
6328/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006329 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006331 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006332 * Returns NULL when out of memory.
6333 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006334 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006335list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006336 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006337 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006338 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339{
Bram Moolenaar33570922005-01-25 22:26:29 +00006340 list_T *copy;
6341 listitem_T *item;
6342 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006343
6344 if (orig == NULL)
6345 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346
6347 copy = list_alloc();
6348 if (copy != NULL)
6349 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006350 if (copyID != 0)
6351 {
6352 /* Do this before adding the items, because one of the items may
6353 * refer back to this list. */
6354 orig->lv_copyID = copyID;
6355 orig->lv_copylist = copy;
6356 }
6357 for (item = orig->lv_first; item != NULL && !got_int;
6358 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006359 {
6360 ni = listitem_alloc();
6361 if (ni == NULL)
6362 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006363 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006364 {
6365 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6366 {
6367 vim_free(ni);
6368 break;
6369 }
6370 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006371 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006372 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006373 list_append(copy, ni);
6374 }
6375 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006376 if (item != NULL)
6377 {
6378 list_unref(copy);
6379 copy = NULL;
6380 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381 }
6382
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006383 return copy;
6384}
6385
6386/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006387 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006388 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006389 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006390 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006391list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006392 list_T *l;
6393 listitem_T *item;
6394 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395{
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006398 /* notify watchers */
6399 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006401 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402 list_fix_watch(l, ip);
6403 if (ip == item2)
6404 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406
6407 if (item2->li_next == NULL)
6408 l->lv_last = item->li_prev;
6409 else
6410 item2->li_next->li_prev = item->li_prev;
6411 if (item->li_prev == NULL)
6412 l->lv_first = item2->li_next;
6413 else
6414 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006415 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416}
6417
6418/*
6419 * Return an allocated string with the string representation of a list.
6420 * May return NULL.
6421 */
6422 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006423list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006424 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006425 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426{
6427 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006428
6429 if (tv->vval.v_list == NULL)
6430 return NULL;
6431 ga_init2(&ga, (int)sizeof(char), 80);
6432 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006433 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006434 {
6435 vim_free(ga.ga_data);
6436 return NULL;
6437 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438 ga_append(&ga, ']');
6439 ga_append(&ga, NUL);
6440 return (char_u *)ga.ga_data;
6441}
6442
6443/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006444 * Join list "l" into a string in "*gap", using separator "sep".
6445 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006446 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006447 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006448 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006449list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006450 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006452 char_u *sep;
6453 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006454 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006455{
6456 int first = TRUE;
6457 char_u *tofree;
6458 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006460 char_u *s;
6461
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006462 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006463 {
6464 if (first)
6465 first = FALSE;
6466 else
6467 ga_concat(gap, sep);
6468
6469 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006470 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006471 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006472 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006473 if (s != NULL)
6474 ga_concat(gap, s);
6475 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006476 if (s == NULL)
6477 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006478 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006479 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006480 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006481}
6482
6483/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006484 * Garbage collection for lists and dictionaries.
6485 *
6486 * We use reference counts to be able to free most items right away when they
6487 * are no longer used. But for composite items it's possible that it becomes
6488 * unused while the reference count is > 0: When there is a recursive
6489 * reference. Example:
6490 * :let l = [1, 2, 3]
6491 * :let d = {9: l}
6492 * :let l[1] = d
6493 *
6494 * Since this is quite unusual we handle this with garbage collection: every
6495 * once in a while find out which lists and dicts are not referenced from any
6496 * variable.
6497 *
6498 * Here is a good reference text about garbage collection (refers to Python
6499 * but it applies to all reference-counting mechanisms):
6500 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006501 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006502
6503/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006504 * Do garbage collection for lists and dicts.
6505 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006506 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006507 int
6508garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006509{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006510 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006511 buf_T *buf;
6512 win_T *wp;
6513 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006514 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006515 int did_free;
6516 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006517#ifdef FEAT_WINDOWS
6518 tabpage_T *tp;
6519#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006520
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006521 /* Only do this once. */
6522 want_garbage_collect = FALSE;
6523 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006524 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006525
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006526 /* We advance by two because we add one for items referenced through
6527 * previous_funccal. */
6528 current_copyID += COPYID_INC;
6529 copyID = current_copyID;
6530
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006531 /*
6532 * 1. Go through all accessible variables and mark all lists and dicts
6533 * with copyID.
6534 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006535
6536 /* Don't free variables in the previous_funccal list unless they are only
6537 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006538 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006539 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6540 {
6541 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6542 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6543 }
6544
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006545 /* script-local variables */
6546 for (i = 1; i <= ga_scripts.ga_len; ++i)
6547 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6548
6549 /* buffer-local variables */
6550 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6551 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6552
6553 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006554 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006555 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6556
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006557#ifdef FEAT_WINDOWS
6558 /* tabpage-local variables */
6559 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6560 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6561#endif
6562
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006563 /* global variables */
6564 set_ref_in_ht(&globvarht, copyID);
6565
6566 /* function-local variables */
6567 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6568 {
6569 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6570 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6571 }
6572
Bram Moolenaard812df62008-11-09 12:46:09 +00006573 /* v: vars */
6574 set_ref_in_ht(&vimvarht, copyID);
6575
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006576 /*
6577 * 2. Free lists and dictionaries that are not referenced.
6578 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006579 did_free = free_unref_items(copyID);
6580
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006581 /*
6582 * 3. Check if any funccal can be freed now.
6583 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006584 for (pfc = &previous_funccal; *pfc != NULL; )
6585 {
6586 if (can_free_funccal(*pfc, copyID))
6587 {
6588 fc = *pfc;
6589 *pfc = fc->caller;
6590 free_funccal(fc, TRUE);
6591 did_free = TRUE;
6592 did_free_funccal = TRUE;
6593 }
6594 else
6595 pfc = &(*pfc)->caller;
6596 }
6597 if (did_free_funccal)
6598 /* When a funccal was freed some more items might be garbage
6599 * collected, so run again. */
6600 (void)garbage_collect();
6601
6602 return did_free;
6603}
6604
6605/*
6606 * Free lists and dictionaries that are no longer referenced.
6607 */
6608 static int
6609free_unref_items(copyID)
6610 int copyID;
6611{
6612 dict_T *dd;
6613 list_T *ll;
6614 int did_free = FALSE;
6615
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006616 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006617 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006618 */
6619 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006620 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006621 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006622 /* Free the Dictionary and ordinary items it contains, but don't
6623 * recurse into Lists and Dictionaries, they will be in the list
6624 * of dicts or list of lists. */
6625 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006626 did_free = TRUE;
6627
6628 /* restart, next dict may also have been freed */
6629 dd = first_dict;
6630 }
6631 else
6632 dd = dd->dv_used_next;
6633
6634 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006635 * Go through the list of lists and free items without the copyID.
6636 * But don't free a list that has a watcher (used in a for loop), these
6637 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006638 */
6639 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006640 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6641 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006642 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006643 /* Free the List and ordinary items it contains, but don't recurse
6644 * into Lists and Dictionaries, they will be in the list of dicts
6645 * or list of lists. */
6646 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006647 did_free = TRUE;
6648
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006649 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006650 ll = first_list;
6651 }
6652 else
6653 ll = ll->lv_used_next;
6654
6655 return did_free;
6656}
6657
6658/*
6659 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6660 */
6661 static void
6662set_ref_in_ht(ht, copyID)
6663 hashtab_T *ht;
6664 int copyID;
6665{
6666 int todo;
6667 hashitem_T *hi;
6668
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006669 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006670 for (hi = ht->ht_array; todo > 0; ++hi)
6671 if (!HASHITEM_EMPTY(hi))
6672 {
6673 --todo;
6674 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6675 }
6676}
6677
6678/*
6679 * Mark all lists and dicts referenced through list "l" with "copyID".
6680 */
6681 static void
6682set_ref_in_list(l, copyID)
6683 list_T *l;
6684 int copyID;
6685{
6686 listitem_T *li;
6687
6688 for (li = l->lv_first; li != NULL; li = li->li_next)
6689 set_ref_in_item(&li->li_tv, copyID);
6690}
6691
6692/*
6693 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6694 */
6695 static void
6696set_ref_in_item(tv, copyID)
6697 typval_T *tv;
6698 int copyID;
6699{
6700 dict_T *dd;
6701 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006702
6703 switch (tv->v_type)
6704 {
6705 case VAR_DICT:
6706 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006707 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006708 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006709 /* Didn't see this dict yet. */
6710 dd->dv_copyID = copyID;
6711 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006712 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006713 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006714
6715 case VAR_LIST:
6716 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006717 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006718 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006719 /* Didn't see this list yet. */
6720 ll->lv_copyID = copyID;
6721 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006722 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006723 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006724 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006725 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006726}
6727
6728/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006729 * Allocate an empty header for a dictionary.
6730 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006731 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006732dict_alloc()
6733{
Bram Moolenaar33570922005-01-25 22:26:29 +00006734 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006735
Bram Moolenaar33570922005-01-25 22:26:29 +00006736 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006737 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006738 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006739 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006740 if (first_dict != NULL)
6741 first_dict->dv_used_prev = d;
6742 d->dv_used_next = first_dict;
6743 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006744 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006745
Bram Moolenaar33570922005-01-25 22:26:29 +00006746 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006747 d->dv_lock = 0;
6748 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006749 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006750 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006751 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006752}
6753
6754/*
6755 * Unreference a Dictionary: decrement the reference count and free it when it
6756 * becomes zero.
6757 */
6758 static void
6759dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006760 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006761{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006762 if (d != NULL && --d->dv_refcount <= 0)
6763 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006764}
6765
6766/*
6767 * Free a Dictionary, including all items it contains.
6768 * Ignores the reference count.
6769 */
6770 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006771dict_free(d, recurse)
6772 dict_T *d;
6773 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006775 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006776 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006777 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006778
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006779 /* Remove the dict from the list of dicts for garbage collection. */
6780 if (d->dv_used_prev == NULL)
6781 first_dict = d->dv_used_next;
6782 else
6783 d->dv_used_prev->dv_used_next = d->dv_used_next;
6784 if (d->dv_used_next != NULL)
6785 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6786
6787 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006788 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006789 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006790 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006791 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006792 if (!HASHITEM_EMPTY(hi))
6793 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006794 /* Remove the item before deleting it, just in case there is
6795 * something recursive causing trouble. */
6796 di = HI2DI(hi);
6797 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006798 if (recurse || (di->di_tv.v_type != VAR_LIST
6799 && di->di_tv.v_type != VAR_DICT))
6800 clear_tv(&di->di_tv);
6801 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006802 --todo;
6803 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006804 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006805 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006806 vim_free(d);
6807}
6808
6809/*
6810 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006811 * The "key" is copied to the new item.
6812 * Note that the value of the item "di_tv" still needs to be initialized!
6813 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006814 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006815 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006816dictitem_alloc(key)
6817 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006818{
Bram Moolenaar33570922005-01-25 22:26:29 +00006819 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006820
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006821 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006822 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006823 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006824 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006825 di->di_flags = 0;
6826 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006827 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006828}
6829
6830/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006831 * Make a copy of a Dictionary item.
6832 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006833 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006834dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006835 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006836{
Bram Moolenaar33570922005-01-25 22:26:29 +00006837 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006838
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006839 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6840 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006841 if (di != NULL)
6842 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006843 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006844 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006845 copy_tv(&org->di_tv, &di->di_tv);
6846 }
6847 return di;
6848}
6849
6850/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006851 * Remove item "item" from Dictionary "dict" and free it.
6852 */
6853 static void
6854dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006855 dict_T *dict;
6856 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006857{
Bram Moolenaar33570922005-01-25 22:26:29 +00006858 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006859
Bram Moolenaar33570922005-01-25 22:26:29 +00006860 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006861 if (HASHITEM_EMPTY(hi))
6862 EMSG2(_(e_intern2), "dictitem_remove()");
6863 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006864 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006865 dictitem_free(item);
6866}
6867
6868/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006869 * Free a dict item. Also clears the value.
6870 */
6871 static void
6872dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006873 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006874{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006875 clear_tv(&item->di_tv);
6876 vim_free(item);
6877}
6878
6879/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006880 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6881 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006882 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006883 * Returns NULL when out of memory.
6884 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006885 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006886dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006887 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006888 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006889 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006890{
Bram Moolenaar33570922005-01-25 22:26:29 +00006891 dict_T *copy;
6892 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006893 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006894 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006895
6896 if (orig == NULL)
6897 return NULL;
6898
6899 copy = dict_alloc();
6900 if (copy != NULL)
6901 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006902 if (copyID != 0)
6903 {
6904 orig->dv_copyID = copyID;
6905 orig->dv_copydict = copy;
6906 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006907 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006908 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006909 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006910 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006911 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006912 --todo;
6913
6914 di = dictitem_alloc(hi->hi_key);
6915 if (di == NULL)
6916 break;
6917 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006918 {
6919 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6920 copyID) == FAIL)
6921 {
6922 vim_free(di);
6923 break;
6924 }
6925 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006926 else
6927 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6928 if (dict_add(copy, di) == FAIL)
6929 {
6930 dictitem_free(di);
6931 break;
6932 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006933 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006934 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006935
Bram Moolenaare9a41262005-01-15 22:18:47 +00006936 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006937 if (todo > 0)
6938 {
6939 dict_unref(copy);
6940 copy = NULL;
6941 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006942 }
6943
6944 return copy;
6945}
6946
6947/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006948 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006949 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006950 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006951 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006952dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006953 dict_T *d;
6954 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006955{
Bram Moolenaar33570922005-01-25 22:26:29 +00006956 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006957}
6958
Bram Moolenaar8c711452005-01-14 21:53:12 +00006959/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006960 * Add a number or string entry to dictionary "d".
6961 * When "str" is NULL use number "nr", otherwise use "str".
6962 * Returns FAIL when out of memory and when key already exists.
6963 */
6964 int
6965dict_add_nr_str(d, key, nr, str)
6966 dict_T *d;
6967 char *key;
6968 long nr;
6969 char_u *str;
6970{
6971 dictitem_T *item;
6972
6973 item = dictitem_alloc((char_u *)key);
6974 if (item == NULL)
6975 return FAIL;
6976 item->di_tv.v_lock = 0;
6977 if (str == NULL)
6978 {
6979 item->di_tv.v_type = VAR_NUMBER;
6980 item->di_tv.vval.v_number = nr;
6981 }
6982 else
6983 {
6984 item->di_tv.v_type = VAR_STRING;
6985 item->di_tv.vval.v_string = vim_strsave(str);
6986 }
6987 if (dict_add(d, item) == FAIL)
6988 {
6989 dictitem_free(item);
6990 return FAIL;
6991 }
6992 return OK;
6993}
6994
6995/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006996 * Get the number of items in a Dictionary.
6997 */
6998 static long
6999dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007000 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007001{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007002 if (d == NULL)
7003 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007004 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007005}
7006
7007/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007008 * Find item "key[len]" in Dictionary "d".
7009 * If "len" is negative use strlen(key).
7010 * Returns NULL when not found.
7011 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007012 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007013dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007014 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015 char_u *key;
7016 int len;
7017{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007018#define AKEYLEN 200
7019 char_u buf[AKEYLEN];
7020 char_u *akey;
7021 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007022 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007023
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007024 if (len < 0)
7025 akey = key;
7026 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007027 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007028 tofree = akey = vim_strnsave(key, len);
7029 if (akey == NULL)
7030 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007031 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007032 else
7033 {
7034 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007035 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007036 akey = buf;
7037 }
7038
Bram Moolenaar33570922005-01-25 22:26:29 +00007039 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007040 vim_free(tofree);
7041 if (HASHITEM_EMPTY(hi))
7042 return NULL;
7043 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007044}
7045
7046/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007047 * Get a string item from a dictionary.
7048 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007049 * Returns NULL if the entry doesn't exist or out of memory.
7050 */
7051 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007052get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007053 dict_T *d;
7054 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007055 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007056{
7057 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007058 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007059
7060 di = dict_find(d, key, -1);
7061 if (di == NULL)
7062 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007063 s = get_tv_string(&di->di_tv);
7064 if (save && s != NULL)
7065 s = vim_strsave(s);
7066 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007067}
7068
7069/*
7070 * Get a number item from a dictionary.
7071 * Returns 0 if the entry doesn't exist or out of memory.
7072 */
7073 long
7074get_dict_number(d, key)
7075 dict_T *d;
7076 char_u *key;
7077{
7078 dictitem_T *di;
7079
7080 di = dict_find(d, key, -1);
7081 if (di == NULL)
7082 return 0;
7083 return get_tv_number(&di->di_tv);
7084}
7085
7086/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087 * Return an allocated string with the string representation of a Dictionary.
7088 * May return NULL.
7089 */
7090 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007091dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007092 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007093 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007094{
7095 garray_T ga;
7096 int first = TRUE;
7097 char_u *tofree;
7098 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007102 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007104 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007105 return NULL;
7106 ga_init2(&ga, (int)sizeof(char), 80);
7107 ga_append(&ga, '{');
7108
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007109 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007110 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007111 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007113 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007114 --todo;
7115
7116 if (first)
7117 first = FALSE;
7118 else
7119 ga_concat(&ga, (char_u *)", ");
7120
7121 tofree = string_quote(hi->hi_key, FALSE);
7122 if (tofree != NULL)
7123 {
7124 ga_concat(&ga, tofree);
7125 vim_free(tofree);
7126 }
7127 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007128 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129 if (s != NULL)
7130 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007131 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007132 if (s == NULL)
7133 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007134 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007135 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007136 if (todo > 0)
7137 {
7138 vim_free(ga.ga_data);
7139 return NULL;
7140 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141
7142 ga_append(&ga, '}');
7143 ga_append(&ga, NUL);
7144 return (char_u *)ga.ga_data;
7145}
7146
7147/*
7148 * Allocate a variable for a Dictionary and fill it from "*arg".
7149 * Return OK or FAIL. Returns NOTDONE for {expr}.
7150 */
7151 static int
7152get_dict_tv(arg, rettv, evaluate)
7153 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007155 int evaluate;
7156{
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 dict_T *d = NULL;
7158 typval_T tvkey;
7159 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007160 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007161 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007162 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007164
7165 /*
7166 * First check if it's not a curly-braces thing: {expr}.
7167 * Must do this without evaluating, otherwise a function may be called
7168 * twice. Unfortunately this means we need to call eval1() twice for the
7169 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007172 if (*start != '}')
7173 {
7174 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7175 return FAIL;
7176 if (*start == '}')
7177 return NOTDONE;
7178 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007179
7180 if (evaluate)
7181 {
7182 d = dict_alloc();
7183 if (d == NULL)
7184 return FAIL;
7185 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007186 tvkey.v_type = VAR_UNKNOWN;
7187 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188
7189 *arg = skipwhite(*arg + 1);
7190 while (**arg != '}' && **arg != NUL)
7191 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007192 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193 goto failret;
7194 if (**arg != ':')
7195 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007196 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007197 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198 goto failret;
7199 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007200 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007202 key = get_tv_string_buf_chk(&tvkey, buf);
7203 if (key == NULL || *key == NUL)
7204 {
7205 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7206 if (key != NULL)
7207 EMSG(_(e_emptykey));
7208 clear_tv(&tvkey);
7209 goto failret;
7210 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212
7213 *arg = skipwhite(*arg + 1);
7214 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7215 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007216 if (evaluate)
7217 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218 goto failret;
7219 }
7220 if (evaluate)
7221 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007222 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223 if (item != NULL)
7224 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007225 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007226 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227 clear_tv(&tv);
7228 goto failret;
7229 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007230 item = dictitem_alloc(key);
7231 clear_tv(&tvkey);
7232 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007234 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007235 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007236 if (dict_add(d, item) == FAIL)
7237 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238 }
7239 }
7240
7241 if (**arg == '}')
7242 break;
7243 if (**arg != ',')
7244 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007245 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 goto failret;
7247 }
7248 *arg = skipwhite(*arg + 1);
7249 }
7250
7251 if (**arg != '}')
7252 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007253 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254failret:
7255 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007256 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257 return FAIL;
7258 }
7259
7260 *arg = skipwhite(*arg + 1);
7261 if (evaluate)
7262 {
7263 rettv->v_type = VAR_DICT;
7264 rettv->vval.v_dict = d;
7265 ++d->dv_refcount;
7266 }
7267
7268 return OK;
7269}
7270
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007272 * Return a string with the string representation of a variable.
7273 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007274 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007275 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007276 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007277 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007278 */
7279 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007280echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007282 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007283 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007284 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007285{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007286 static int recurse = 0;
7287 char_u *r = NULL;
7288
Bram Moolenaar33570922005-01-25 22:26:29 +00007289 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007291 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292 *tofree = NULL;
7293 return NULL;
7294 }
7295 ++recurse;
7296
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007297 switch (tv->v_type)
7298 {
7299 case VAR_FUNC:
7300 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301 r = tv->vval.v_string;
7302 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007303
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007304 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007305 if (tv->vval.v_list == NULL)
7306 {
7307 *tofree = NULL;
7308 r = NULL;
7309 }
7310 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7311 {
7312 *tofree = NULL;
7313 r = (char_u *)"[...]";
7314 }
7315 else
7316 {
7317 tv->vval.v_list->lv_copyID = copyID;
7318 *tofree = list2string(tv, copyID);
7319 r = *tofree;
7320 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007321 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007322
Bram Moolenaar8c711452005-01-14 21:53:12 +00007323 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007324 if (tv->vval.v_dict == NULL)
7325 {
7326 *tofree = NULL;
7327 r = NULL;
7328 }
7329 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7330 {
7331 *tofree = NULL;
7332 r = (char_u *)"{...}";
7333 }
7334 else
7335 {
7336 tv->vval.v_dict->dv_copyID = copyID;
7337 *tofree = dict2string(tv, copyID);
7338 r = *tofree;
7339 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007341
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007342 case VAR_STRING:
7343 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007344 *tofree = NULL;
7345 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007346 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007347
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007348#ifdef FEAT_FLOAT
7349 case VAR_FLOAT:
7350 *tofree = NULL;
7351 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7352 r = numbuf;
7353 break;
7354#endif
7355
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007356 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007357 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007359 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360
7361 --recurse;
7362 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007363}
7364
7365/*
7366 * Return a string with the string representation of a variable.
7367 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7368 * "numbuf" is used for a number.
7369 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007370 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007371 */
7372 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007373tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007375 char_u **tofree;
7376 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007377 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007378{
7379 switch (tv->v_type)
7380 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007381 case VAR_FUNC:
7382 *tofree = string_quote(tv->vval.v_string, TRUE);
7383 return *tofree;
7384 case VAR_STRING:
7385 *tofree = string_quote(tv->vval.v_string, FALSE);
7386 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007387#ifdef FEAT_FLOAT
7388 case VAR_FLOAT:
7389 *tofree = NULL;
7390 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7391 return numbuf;
7392#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007394 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007395 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007397 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007398 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007399 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007400 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007401}
7402
7403/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007404 * Return string "str" in ' quotes, doubling ' characters.
7405 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007407 */
7408 static char_u *
7409string_quote(str, function)
7410 char_u *str;
7411 int function;
7412{
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007414 char_u *p, *r, *s;
7415
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 len = (function ? 13 : 3);
7417 if (str != NULL)
7418 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007419 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007420 for (p = str; *p != NUL; mb_ptr_adv(p))
7421 if (*p == '\'')
7422 ++len;
7423 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007424 s = r = alloc(len);
7425 if (r != NULL)
7426 {
7427 if (function)
7428 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007430 r += 10;
7431 }
7432 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007434 if (str != NULL)
7435 for (p = str; *p != NUL; )
7436 {
7437 if (*p == '\'')
7438 *r++ = '\'';
7439 MB_COPY_CHAR(p, r);
7440 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007441 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007442 if (function)
7443 *r++ = ')';
7444 *r++ = NUL;
7445 }
7446 return s;
7447}
7448
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007449#ifdef FEAT_FLOAT
7450/*
7451 * Convert the string "text" to a floating point number.
7452 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7453 * this always uses a decimal point.
7454 * Returns the length of the text that was consumed.
7455 */
7456 static int
7457string2float(text, value)
7458 char_u *text;
7459 float_T *value; /* result stored here */
7460{
7461 char *s = (char *)text;
7462 float_T f;
7463
7464 f = strtod(s, &s);
7465 *value = f;
7466 return (int)((char_u *)s - text);
7467}
7468#endif
7469
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 * Get the value of an environment variable.
7472 * "arg" is pointing to the '$'. It is advanced to after the name.
7473 * If the environment variable was not set, silently assume it is empty.
7474 * Always return OK.
7475 */
7476 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007477get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 int evaluate;
7481{
7482 char_u *string = NULL;
7483 int len;
7484 int cc;
7485 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007486 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
7488 ++*arg;
7489 name = *arg;
7490 len = get_env_len(arg);
7491 if (evaluate)
7492 {
7493 if (len != 0)
7494 {
7495 cc = name[len];
7496 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007497 /* first try vim_getenv(), fast for normal environment vars */
7498 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007500 {
7501 if (!mustfree)
7502 string = vim_strsave(string);
7503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 else
7505 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007506 if (mustfree)
7507 vim_free(string);
7508
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 /* next try expanding things like $VIM and ${HOME} */
7510 string = expand_env_save(name - 1);
7511 if (string != NULL && *string == '$')
7512 {
7513 vim_free(string);
7514 string = NULL;
7515 }
7516 }
7517 name[len] = cc;
7518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007519 rettv->v_type = VAR_STRING;
7520 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
7522
7523 return OK;
7524}
7525
7526/*
7527 * Array with names and number of arguments of all internal functions
7528 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7529 */
7530static struct fst
7531{
7532 char *f_name; /* function name */
7533 char f_min_argc; /* minimal number of arguments */
7534 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007535 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007536 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537} functions[] =
7538{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007539#ifdef FEAT_FLOAT
7540 {"abs", 1, 1, f_abs},
7541#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007542 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 {"append", 2, 2, f_append},
7544 {"argc", 0, 0, f_argc},
7545 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007546 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007547#ifdef FEAT_FLOAT
7548 {"atan", 1, 1, f_atan},
7549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007551 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 {"bufexists", 1, 1, f_bufexists},
7553 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7554 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7555 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7556 {"buflisted", 1, 1, f_buflisted},
7557 {"bufloaded", 1, 1, f_bufloaded},
7558 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007559 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {"bufwinnr", 1, 1, f_bufwinnr},
7561 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007562 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007563 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007564#ifdef FEAT_FLOAT
7565 {"ceil", 1, 1, f_ceil},
7566#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007567 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {"char2nr", 1, 1, f_char2nr},
7569 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007570 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007572#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007573 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007574 {"complete_add", 1, 1, f_complete_add},
7575 {"complete_check", 0, 0, f_complete_check},
7576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007578 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007579#ifdef FEAT_FLOAT
7580 {"cos", 1, 1, f_cos},
7581#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007582 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007584 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007585 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 {"delete", 1, 1, f_delete},
7587 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007588 {"diff_filler", 1, 1, f_diff_filler},
7589 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007590 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007592 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {"eventhandler", 0, 0, f_eventhandler},
7594 {"executable", 1, 1, f_executable},
7595 {"exists", 1, 1, f_exists},
7596 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007597 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007598 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7600 {"filereadable", 1, 1, f_filereadable},
7601 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007602 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007603 {"finddir", 1, 3, f_finddir},
7604 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007605#ifdef FEAT_FLOAT
7606 {"float2nr", 1, 1, f_float2nr},
7607 {"floor", 1, 1, f_floor},
7608#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007609 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 {"fnamemodify", 2, 2, f_fnamemodify},
7611 {"foldclosed", 1, 1, f_foldclosed},
7612 {"foldclosedend", 1, 1, f_foldclosedend},
7613 {"foldlevel", 1, 1, f_foldlevel},
7614 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007615 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007617 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007618 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007619 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007620 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 {"getbufvar", 2, 2, f_getbufvar},
7622 {"getchar", 0, 1, f_getchar},
7623 {"getcharmod", 0, 0, f_getcharmod},
7624 {"getcmdline", 0, 0, f_getcmdline},
7625 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007626 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007628 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007629 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 {"getfsize", 1, 1, f_getfsize},
7631 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007632 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007633 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007634 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007635 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007636 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007637 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007638 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007639 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007641 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 {"getwinposx", 0, 0, f_getwinposx},
7643 {"getwinposy", 0, 0, f_getwinposy},
7644 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007645 {"glob", 1, 2, f_glob},
7646 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007649 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007650 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7652 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7653 {"histadd", 2, 2, f_histadd},
7654 {"histdel", 1, 2, f_histdel},
7655 {"histget", 1, 2, f_histget},
7656 {"histnr", 1, 1, f_histnr},
7657 {"hlID", 1, 1, f_hlID},
7658 {"hlexists", 1, 1, f_hlexists},
7659 {"hostname", 0, 0, f_hostname},
7660 {"iconv", 3, 3, f_iconv},
7661 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007662 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007663 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007665 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {"inputrestore", 0, 0, f_inputrestore},
7667 {"inputsave", 0, 0, f_inputsave},
7668 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007669 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007671 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007673 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007676 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {"libcall", 3, 3, f_libcall},
7678 {"libcallnr", 3, 3, f_libcallnr},
7679 {"line", 1, 1, f_line},
7680 {"line2byte", 1, 1, f_line2byte},
7681 {"lispindent", 1, 1, f_lispindent},
7682 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007683#ifdef FEAT_FLOAT
7684 {"log10", 1, 1, f_log10},
7685#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007687 {"maparg", 1, 3, f_maparg},
7688 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007689 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007690 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007691 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007692 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007693 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007694 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007695 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007696 {"max", 1, 1, f_max},
7697 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007698#ifdef vim_mkdir
7699 {"mkdir", 1, 3, f_mkdir},
7700#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007701 {"mode", 0, 1, f_mode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {"nextnonblank", 1, 1, f_nextnonblank},
7703 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007704 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007705#ifdef FEAT_FLOAT
7706 {"pow", 2, 2, f_pow},
7707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007709 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007710 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007712 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007713 {"reltime", 0, 2, f_reltime},
7714 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 {"remote_expr", 2, 3, f_remote_expr},
7716 {"remote_foreground", 1, 1, f_remote_foreground},
7717 {"remote_peek", 1, 2, f_remote_peek},
7718 {"remote_read", 1, 1, f_remote_read},
7719 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007720 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007722 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007724 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007725#ifdef FEAT_FLOAT
7726 {"round", 1, 1, f_round},
7727#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007728 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007729 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007730 {"searchpair", 3, 7, f_searchpair},
7731 {"searchpairpos", 3, 7, f_searchpairpos},
7732 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 {"server2client", 2, 2, f_server2client},
7734 {"serverlist", 0, 0, f_serverlist},
7735 {"setbufvar", 3, 3, f_setbufvar},
7736 {"setcmdpos", 1, 1, f_setcmdpos},
7737 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007738 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007739 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007740 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007741 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 {"setreg", 2, 3, f_setreg},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007743 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007745 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007747#ifdef FEAT_FLOAT
7748 {"sin", 1, 1, f_sin},
7749#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007750 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007751 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007752 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007753 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007754 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007755#ifdef FEAT_FLOAT
7756 {"sqrt", 1, 1, f_sqrt},
7757 {"str2float", 1, 1, f_str2float},
7758#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007759 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760#ifdef HAVE_STRFTIME
7761 {"strftime", 1, 2, f_strftime},
7762#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007764 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 {"strlen", 1, 1, f_strlen},
7766 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007767 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {"strtrans", 1, 1, f_strtrans},
7769 {"submatch", 1, 1, f_submatch},
7770 {"substitute", 4, 4, f_substitute},
7771 {"synID", 3, 3, f_synID},
7772 {"synIDattr", 2, 3, f_synIDattr},
7773 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007774 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007775 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007776 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007777 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007778 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007779 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007780 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007782 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 {"tolower", 1, 1, f_tolower},
7784 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007785 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007786#ifdef FEAT_FLOAT
7787 {"trunc", 1, 1, f_trunc},
7788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007790 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 {"virtcol", 1, 1, f_virtcol},
7792 {"visualmode", 0, 1, f_visualmode},
7793 {"winbufnr", 1, 1, f_winbufnr},
7794 {"wincol", 0, 0, f_wincol},
7795 {"winheight", 1, 1, f_winheight},
7796 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007797 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007799 {"winrestview", 1, 1, f_winrestview},
7800 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007802 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803};
7804
7805#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7806
7807/*
7808 * Function given to ExpandGeneric() to obtain the list of internal
7809 * or user defined function names.
7810 */
7811 char_u *
7812get_function_name(xp, idx)
7813 expand_T *xp;
7814 int idx;
7815{
7816 static int intidx = -1;
7817 char_u *name;
7818
7819 if (idx == 0)
7820 intidx = -1;
7821 if (intidx < 0)
7822 {
7823 name = get_user_func_name(xp, idx);
7824 if (name != NULL)
7825 return name;
7826 }
7827 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7828 {
7829 STRCPY(IObuff, functions[intidx].f_name);
7830 STRCAT(IObuff, "(");
7831 if (functions[intidx].f_max_argc == 0)
7832 STRCAT(IObuff, ")");
7833 return IObuff;
7834 }
7835
7836 return NULL;
7837}
7838
7839/*
7840 * Function given to ExpandGeneric() to obtain the list of internal or
7841 * user defined variable or function names.
7842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 char_u *
7844get_expr_name(xp, idx)
7845 expand_T *xp;
7846 int idx;
7847{
7848 static int intidx = -1;
7849 char_u *name;
7850
7851 if (idx == 0)
7852 intidx = -1;
7853 if (intidx < 0)
7854 {
7855 name = get_function_name(xp, idx);
7856 if (name != NULL)
7857 return name;
7858 }
7859 return get_user_var_name(xp, ++intidx);
7860}
7861
7862#endif /* FEAT_CMDL_COMPL */
7863
7864/*
7865 * Find internal function in table above.
7866 * Return index, or -1 if not found
7867 */
7868 static int
7869find_internal_func(name)
7870 char_u *name; /* name of the function */
7871{
7872 int first = 0;
7873 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7874 int cmp;
7875 int x;
7876
7877 /*
7878 * Find the function name in the table. Binary search.
7879 */
7880 while (first <= last)
7881 {
7882 x = first + ((unsigned)(last - first) >> 1);
7883 cmp = STRCMP(name, functions[x].f_name);
7884 if (cmp < 0)
7885 last = x - 1;
7886 else if (cmp > 0)
7887 first = x + 1;
7888 else
7889 return x;
7890 }
7891 return -1;
7892}
7893
7894/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007895 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7896 * name it contains, otherwise return "name".
7897 */
7898 static char_u *
7899deref_func_name(name, lenp)
7900 char_u *name;
7901 int *lenp;
7902{
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007904 int cc;
7905
7906 cc = name[*lenp];
7907 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007908 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007909 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007910 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007911 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007912 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913 {
7914 *lenp = 0;
7915 return (char_u *)""; /* just in case */
7916 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007917 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919 }
7920
7921 return name;
7922}
7923
7924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 * Allocate a variable for the result of a function.
7926 * Return OK or FAIL.
7927 */
7928 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007929get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7930 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 char_u *name; /* name of the function */
7932 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 char_u **arg; /* argument, pointing to the '(' */
7935 linenr_T firstline; /* first line of range */
7936 linenr_T lastline; /* last line of range */
7937 int *doesrange; /* return: function handled range */
7938 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
7941 char_u *argp;
7942 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007943 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 int argcount = 0; /* number of arguments found */
7945
7946 /*
7947 * Get the arguments.
7948 */
7949 argp = *arg;
7950 while (argcount < MAX_FUNC_ARGS)
7951 {
7952 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7953 if (*argp == ')' || *argp == ',' || *argp == NUL)
7954 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7956 {
7957 ret = FAIL;
7958 break;
7959 }
7960 ++argcount;
7961 if (*argp != ',')
7962 break;
7963 }
7964 if (*argp == ')')
7965 ++argp;
7966 else
7967 ret = FAIL;
7968
7969 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007970 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007971 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007973 {
7974 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007975 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007976 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00007977 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979
7980 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007981 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982
7983 *arg = skipwhite(argp);
7984 return ret;
7985}
7986
7987
7988/*
7989 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00007990 * Return OK when the function can't be called, FAIL otherwise.
7991 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 */
7993 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007994call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007995 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 char_u *name; /* name of the function */
7997 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007998 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008000 typval_T *argvars; /* vars for arguments, must have "argcount"
8001 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 linenr_T firstline; /* first line of range */
8003 linenr_T lastline; /* last line of range */
8004 int *doesrange; /* return: function handled range */
8005 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008006 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007{
8008 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009#define ERROR_UNKNOWN 0
8010#define ERROR_TOOMANY 1
8011#define ERROR_TOOFEW 2
8012#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008013#define ERROR_DICT 4
8014#define ERROR_NONE 5
8015#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 int error = ERROR_NONE;
8017 int i;
8018 int llen;
8019 ufunc_T *fp;
8020 int cc;
8021#define FLEN_FIXED 40
8022 char_u fname_buf[FLEN_FIXED + 1];
8023 char_u *fname;
8024
8025 /*
8026 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8027 * Change <SNR>123_name() to K_SNR 123_name().
8028 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8029 */
8030 cc = name[len];
8031 name[len] = NUL;
8032 llen = eval_fname_script(name);
8033 if (llen > 0)
8034 {
8035 fname_buf[0] = K_SPECIAL;
8036 fname_buf[1] = KS_EXTRA;
8037 fname_buf[2] = (int)KE_SNR;
8038 i = 3;
8039 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8040 {
8041 if (current_SID <= 0)
8042 error = ERROR_SCRIPT;
8043 else
8044 {
8045 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8046 i = (int)STRLEN(fname_buf);
8047 }
8048 }
8049 if (i + STRLEN(name + llen) < FLEN_FIXED)
8050 {
8051 STRCPY(fname_buf + i, name + llen);
8052 fname = fname_buf;
8053 }
8054 else
8055 {
8056 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8057 if (fname == NULL)
8058 error = ERROR_OTHER;
8059 else
8060 {
8061 mch_memmove(fname, fname_buf, (size_t)i);
8062 STRCPY(fname + i, name + llen);
8063 }
8064 }
8065 }
8066 else
8067 fname = name;
8068
8069 *doesrange = FALSE;
8070
8071
8072 /* execute the function if no errors detected and executing */
8073 if (evaluate && error == ERROR_NONE)
8074 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008075 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8076 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 error = ERROR_UNKNOWN;
8078
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008079 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {
8081 /*
8082 * User defined function.
8083 */
8084 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008085
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008087 /* Trigger FuncUndefined event, may load the function. */
8088 if (fp == NULL
8089 && apply_autocmds(EVENT_FUNCUNDEFINED,
8090 fname, fname, TRUE, NULL)
8091 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008093 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 fp = find_func(fname);
8095 }
8096#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008097 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008098 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008099 {
8100 /* loaded a package, search for the function again */
8101 fp = find_func(fname);
8102 }
8103
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 if (fp != NULL)
8105 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008106 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008108 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008110 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008112 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008113 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 else
8115 {
8116 /*
8117 * Call the user function.
8118 * Save and restore search patterns, script variables and
8119 * redo buffer.
8120 */
8121 save_search_patterns();
8122 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008123 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008124 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008125 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008126 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8127 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8128 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008129 /* Function was unreferenced while being used, free it
8130 * now. */
8131 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 restoreRedobuff();
8133 restore_search_patterns();
8134 error = ERROR_NONE;
8135 }
8136 }
8137 }
8138 else
8139 {
8140 /*
8141 * Find the function name in the table, call its implementation.
8142 */
8143 i = find_internal_func(fname);
8144 if (i >= 0)
8145 {
8146 if (argcount < functions[i].f_min_argc)
8147 error = ERROR_TOOFEW;
8148 else if (argcount > functions[i].f_max_argc)
8149 error = ERROR_TOOMANY;
8150 else
8151 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008152 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008153 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 error = ERROR_NONE;
8155 }
8156 }
8157 }
8158 /*
8159 * The function call (or "FuncUndefined" autocommand sequence) might
8160 * have been aborted by an error, an interrupt, or an explicitly thrown
8161 * exception that has not been caught so far. This situation can be
8162 * tested for by calling aborting(). For an error in an internal
8163 * function or for the "E132" error in call_user_func(), however, the
8164 * throw point at which the "force_abort" flag (temporarily reset by
8165 * emsg()) is normally updated has not been reached yet. We need to
8166 * update that flag first to make aborting() reliable.
8167 */
8168 update_force_abort();
8169 }
8170 if (error == ERROR_NONE)
8171 ret = OK;
8172
8173 /*
8174 * Report an error unless the argument evaluation or function call has been
8175 * cancelled due to an aborting error, an interrupt, or an exception.
8176 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008177 if (!aborting())
8178 {
8179 switch (error)
8180 {
8181 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008182 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008183 break;
8184 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008185 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008186 break;
8187 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008188 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008189 name);
8190 break;
8191 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008192 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008193 name);
8194 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008195 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008196 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008197 name);
8198 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008199 }
8200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201
8202 name[len] = cc;
8203 if (fname != name && fname != fname_buf)
8204 vim_free(fname);
8205
8206 return ret;
8207}
8208
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008209/*
8210 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008211 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008212 */
8213 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008214emsg_funcname(ermsg, name)
8215 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008216 char_u *name;
8217{
8218 char_u *p;
8219
8220 if (*name == K_SPECIAL)
8221 p = concat_str((char_u *)"<SNR>", name + 3);
8222 else
8223 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008224 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008225 if (p != name)
8226 vim_free(p);
8227}
8228
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008229/*
8230 * Return TRUE for a non-zero Number and a non-empty String.
8231 */
8232 static int
8233non_zero_arg(argvars)
8234 typval_T *argvars;
8235{
8236 return ((argvars[0].v_type == VAR_NUMBER
8237 && argvars[0].vval.v_number != 0)
8238 || (argvars[0].v_type == VAR_STRING
8239 && argvars[0].vval.v_string != NULL
8240 && *argvars[0].vval.v_string != NUL));
8241}
8242
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243/*********************************************
8244 * Implementation of the built-in functions
8245 */
8246
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008247#ifdef FEAT_FLOAT
8248/*
8249 * "abs(expr)" function
8250 */
8251 static void
8252f_abs(argvars, rettv)
8253 typval_T *argvars;
8254 typval_T *rettv;
8255{
8256 if (argvars[0].v_type == VAR_FLOAT)
8257 {
8258 rettv->v_type = VAR_FLOAT;
8259 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8260 }
8261 else
8262 {
8263 varnumber_T n;
8264 int error = FALSE;
8265
8266 n = get_tv_number_chk(&argvars[0], &error);
8267 if (error)
8268 rettv->vval.v_number = -1;
8269 else if (n > 0)
8270 rettv->vval.v_number = n;
8271 else
8272 rettv->vval.v_number = -n;
8273 }
8274}
8275#endif
8276
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008278 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 */
8280 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008281f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008282 typval_T *argvars;
8283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284{
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008287 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008288 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008290 if ((l = argvars[0].vval.v_list) != NULL
8291 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8292 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008293 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008294 }
8295 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008296 EMSG(_(e_listreq));
8297}
8298
8299/*
8300 * "append(lnum, string/list)" function
8301 */
8302 static void
8303f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008304 typval_T *argvars;
8305 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008306{
8307 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008308 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008309 list_T *l = NULL;
8310 listitem_T *li = NULL;
8311 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008312 long added = 0;
8313
Bram Moolenaar0d660222005-01-07 21:51:51 +00008314 lnum = get_tv_lnum(argvars);
8315 if (lnum >= 0
8316 && lnum <= curbuf->b_ml.ml_line_count
8317 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008318 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008319 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008320 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008321 l = argvars[1].vval.v_list;
8322 if (l == NULL)
8323 return;
8324 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008325 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008326 for (;;)
8327 {
8328 if (l == NULL)
8329 tv = &argvars[1]; /* append a string */
8330 else if (li == NULL)
8331 break; /* end of list */
8332 else
8333 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008334 line = get_tv_string_chk(tv);
8335 if (line == NULL) /* type error */
8336 {
8337 rettv->vval.v_number = 1; /* Failed */
8338 break;
8339 }
8340 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008341 ++added;
8342 if (l == NULL)
8343 break;
8344 li = li->li_next;
8345 }
8346
8347 appended_lines_mark(lnum, added);
8348 if (curwin->w_cursor.lnum > lnum)
8349 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008351 else
8352 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353}
8354
8355/*
8356 * "argc()" function
8357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008360 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008363 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364}
8365
8366/*
8367 * "argidx()" function
8368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008370f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008371 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008374 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375}
8376
8377/*
8378 * "argv(nr)" function
8379 */
8380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008381f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008382 typval_T *argvars;
8383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384{
8385 int idx;
8386
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008387 if (argvars[0].v_type != VAR_UNKNOWN)
8388 {
8389 idx = get_tv_number_chk(&argvars[0], NULL);
8390 if (idx >= 0 && idx < ARGCOUNT)
8391 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8392 else
8393 rettv->vval.v_string = NULL;
8394 rettv->v_type = VAR_STRING;
8395 }
8396 else if (rettv_list_alloc(rettv) == OK)
8397 for (idx = 0; idx < ARGCOUNT; ++idx)
8398 list_append_string(rettv->vval.v_list,
8399 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400}
8401
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008402#ifdef FEAT_FLOAT
8403static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8404
8405/*
8406 * Get the float value of "argvars[0]" into "f".
8407 * Returns FAIL when the argument is not a Number or Float.
8408 */
8409 static int
8410get_float_arg(argvars, f)
8411 typval_T *argvars;
8412 float_T *f;
8413{
8414 if (argvars[0].v_type == VAR_FLOAT)
8415 {
8416 *f = argvars[0].vval.v_float;
8417 return OK;
8418 }
8419 if (argvars[0].v_type == VAR_NUMBER)
8420 {
8421 *f = (float_T)argvars[0].vval.v_number;
8422 return OK;
8423 }
8424 EMSG(_("E808: Number or Float required"));
8425 return FAIL;
8426}
8427
8428/*
8429 * "atan()" function
8430 */
8431 static void
8432f_atan(argvars, rettv)
8433 typval_T *argvars;
8434 typval_T *rettv;
8435{
8436 float_T f;
8437
8438 rettv->v_type = VAR_FLOAT;
8439 if (get_float_arg(argvars, &f) == OK)
8440 rettv->vval.v_float = atan(f);
8441 else
8442 rettv->vval.v_float = 0.0;
8443}
8444#endif
8445
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446/*
8447 * "browse(save, title, initdir, default)" function
8448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008451 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453{
8454#ifdef FEAT_BROWSE
8455 int save;
8456 char_u *title;
8457 char_u *initdir;
8458 char_u *defname;
8459 char_u buf[NUMBUFLEN];
8460 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008461 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008463 save = get_tv_number_chk(&argvars[0], &error);
8464 title = get_tv_string_chk(&argvars[1]);
8465 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8466 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008468 if (error || title == NULL || initdir == NULL || defname == NULL)
8469 rettv->vval.v_string = NULL;
8470 else
8471 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008472 do_browse(save ? BROWSE_SAVE : 0,
8473 title, defname, NULL, initdir, NULL, curbuf);
8474#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008475 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008476#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008477 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008478}
8479
8480/*
8481 * "browsedir(title, initdir)" function
8482 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008484f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008485 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008486 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008487{
8488#ifdef FEAT_BROWSE
8489 char_u *title;
8490 char_u *initdir;
8491 char_u buf[NUMBUFLEN];
8492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008493 title = get_tv_string_chk(&argvars[0]);
8494 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008496 if (title == NULL || initdir == NULL)
8497 rettv->vval.v_string = NULL;
8498 else
8499 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008500 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008504 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505}
8506
Bram Moolenaar33570922005-01-25 22:26:29 +00008507static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008508
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509/*
8510 * Find a buffer by number or exact name.
8511 */
8512 static buf_T *
8513find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008514 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
8516 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008518 if (avar->v_type == VAR_NUMBER)
8519 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008520 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008523 if (buf == NULL)
8524 {
8525 /* No full path name match, try a match with a URL or a "nofile"
8526 * buffer, these don't use the full path. */
8527 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8528 if (buf->b_fname != NULL
8529 && (path_with_url(buf->b_fname)
8530#ifdef FEAT_QUICKFIX
8531 || bt_nofile(buf)
8532#endif
8533 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008534 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008535 break;
8536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 }
8538 return buf;
8539}
8540
8541/*
8542 * "bufexists(expr)" function
8543 */
8544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008545f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008546 typval_T *argvars;
8547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008549 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550}
8551
8552/*
8553 * "buflisted(expr)" function
8554 */
8555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008556f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008557 typval_T *argvars;
8558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559{
8560 buf_T *buf;
8561
8562 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008563 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564}
8565
8566/*
8567 * "bufloaded(expr)" function
8568 */
8569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008570f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 typval_T *argvars;
8572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
8574 buf_T *buf;
8575
8576 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008577 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578}
8579
Bram Moolenaar33570922005-01-25 22:26:29 +00008580static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008581
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582/*
8583 * Get buffer by number or pattern.
8584 */
8585 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008587 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008589 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 int save_magic;
8591 char_u *save_cpo;
8592 buf_T *buf;
8593
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008594 if (tv->v_type == VAR_NUMBER)
8595 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008596 if (tv->v_type != VAR_STRING)
8597 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 if (name == NULL || *name == NUL)
8599 return curbuf;
8600 if (name[0] == '$' && name[1] == NUL)
8601 return lastbuf;
8602
8603 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8604 save_magic = p_magic;
8605 p_magic = TRUE;
8606 save_cpo = p_cpo;
8607 p_cpo = (char_u *)"";
8608
8609 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8610 TRUE, FALSE));
8611
8612 p_magic = save_magic;
8613 p_cpo = save_cpo;
8614
8615 /* If not found, try expanding the name, like done for bufexists(). */
8616 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008617 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618
8619 return buf;
8620}
8621
8622/*
8623 * "bufname(expr)" function
8624 */
8625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008626f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008627 typval_T *argvars;
8628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629{
8630 buf_T *buf;
8631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008632 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008634 buf = get_buf_tv(&argvars[0]);
8635 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008637 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 --emsg_off;
8641}
8642
8643/*
8644 * "bufnr(expr)" function
8645 */
8646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008647f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008648 typval_T *argvars;
8649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650{
8651 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008652 int error = FALSE;
8653 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008655 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008658 --emsg_off;
8659
8660 /* If the buffer isn't found and the second argument is not zero create a
8661 * new buffer. */
8662 if (buf == NULL
8663 && argvars[1].v_type != VAR_UNKNOWN
8664 && get_tv_number_chk(&argvars[1], &error) != 0
8665 && !error
8666 && (name = get_tv_string_chk(&argvars[0])) != NULL
8667 && !error)
8668 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8669
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008671 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674}
8675
8676/*
8677 * "bufwinnr(nr)" function
8678 */
8679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008680f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008681 typval_T *argvars;
8682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683{
8684#ifdef FEAT_WINDOWS
8685 win_T *wp;
8686 int winnr = 0;
8687#endif
8688 buf_T *buf;
8689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008690 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008692 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693#ifdef FEAT_WINDOWS
8694 for (wp = firstwin; wp; wp = wp->w_next)
8695 {
8696 ++winnr;
8697 if (wp->w_buffer == buf)
8698 break;
8699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008700 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008702 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703#endif
8704 --emsg_off;
8705}
8706
8707/*
8708 * "byte2line(byte)" function
8709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008712 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714{
8715#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008716 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717#else
8718 long boff = 0;
8719
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008720 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008724 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 (linenr_T)0, &boff);
8726#endif
8727}
8728
8729/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008730 * "byteidx()" function
8731 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008733f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008734 typval_T *argvars;
8735 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008736{
8737#ifdef FEAT_MBYTE
8738 char_u *t;
8739#endif
8740 char_u *str;
8741 long idx;
8742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008743 str = get_tv_string_chk(&argvars[0]);
8744 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008745 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008746 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008747 return;
8748
8749#ifdef FEAT_MBYTE
8750 t = str;
8751 for ( ; idx > 0; idx--)
8752 {
8753 if (*t == NUL) /* EOL reached */
8754 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008755 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008756 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008757 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008758#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008759 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008761#endif
8762}
8763
8764/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008765 * "call(func, arglist)" function
8766 */
8767 static void
8768f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008769 typval_T *argvars;
8770 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008771{
8772 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008773 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008774 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008775 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008776 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008777 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008778
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008779 if (argvars[1].v_type != VAR_LIST)
8780 {
8781 EMSG(_(e_listreq));
8782 return;
8783 }
8784 if (argvars[1].vval.v_list == NULL)
8785 return;
8786
8787 if (argvars[0].v_type == VAR_FUNC)
8788 func = argvars[0].vval.v_string;
8789 else
8790 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008791 if (*func == NUL)
8792 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008793
Bram Moolenaare9a41262005-01-15 22:18:47 +00008794 if (argvars[2].v_type != VAR_UNKNOWN)
8795 {
8796 if (argvars[2].v_type != VAR_DICT)
8797 {
8798 EMSG(_(e_dictreq));
8799 return;
8800 }
8801 selfdict = argvars[2].vval.v_dict;
8802 }
8803
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008804 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8805 item = item->li_next)
8806 {
8807 if (argc == MAX_FUNC_ARGS)
8808 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008809 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008810 break;
8811 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008812 /* Make a copy of each argument. This is needed to be able to set
8813 * v_lock to VAR_FIXED in the copy without changing the original list.
8814 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008815 copy_tv(&item->li_tv, &argv[argc++]);
8816 }
8817
8818 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008819 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008820 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
8821 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008822
8823 /* Free the arguments. */
8824 while (argc > 0)
8825 clear_tv(&argv[--argc]);
8826}
8827
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008828#ifdef FEAT_FLOAT
8829/*
8830 * "ceil({float})" function
8831 */
8832 static void
8833f_ceil(argvars, rettv)
8834 typval_T *argvars;
8835 typval_T *rettv;
8836{
8837 float_T f;
8838
8839 rettv->v_type = VAR_FLOAT;
8840 if (get_float_arg(argvars, &f) == OK)
8841 rettv->vval.v_float = ceil(f);
8842 else
8843 rettv->vval.v_float = 0.0;
8844}
8845#endif
8846
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008847/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008848 * "changenr()" function
8849 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008850 static void
8851f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008852 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008853 typval_T *rettv;
8854{
8855 rettv->vval.v_number = curbuf->b_u_seq_cur;
8856}
8857
8858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 * "char2nr(string)" function
8860 */
8861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008863 typval_T *argvars;
8864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
8866#ifdef FEAT_MBYTE
8867 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008868 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 else
8870#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872}
8873
8874/*
8875 * "cindent(lnum)" function
8876 */
8877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008878f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008879 typval_T *argvars;
8880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881{
8882#ifdef FEAT_CINDENT
8883 pos_T pos;
8884 linenr_T lnum;
8885
8886 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008887 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8889 {
8890 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 curwin->w_cursor = pos;
8893 }
8894 else
8895#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008896 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897}
8898
8899/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008900 * "clearmatches()" function
8901 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008902 static void
8903f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008904 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008905 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008906{
8907#ifdef FEAT_SEARCH_EXTRA
8908 clear_matches(curwin);
8909#endif
8910}
8911
8912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 * "col(string)" function
8914 */
8915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008917 typval_T *argvars;
8918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919{
8920 colnr_T col = 0;
8921 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008922 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008924 fp = var2fpos(&argvars[0], FALSE, &fnum);
8925 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 {
8927 if (fp->col == MAXCOL)
8928 {
8929 /* '> can be MAXCOL, get the length of the line then */
8930 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008931 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 else
8933 col = MAXCOL;
8934 }
8935 else
8936 {
8937 col = fp->col + 1;
8938#ifdef FEAT_VIRTUALEDIT
8939 /* col(".") when the cursor is on the NUL at the end of the line
8940 * because of "coladd" can be seen as an extra column. */
8941 if (virtual_active() && fp == &curwin->w_cursor)
8942 {
8943 char_u *p = ml_get_cursor();
8944
8945 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
8946 curwin->w_virtcol - curwin->w_cursor.coladd))
8947 {
8948# ifdef FEAT_MBYTE
8949 int l;
8950
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008951 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 col += l;
8953# else
8954 if (*p != NUL && p[1] == NUL)
8955 ++col;
8956# endif
8957 }
8958 }
8959#endif
8960 }
8961 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963}
8964
Bram Moolenaar572cb562005-08-05 21:35:02 +00008965#if defined(FEAT_INS_EXPAND)
8966/*
Bram Moolenaarade00832006-03-10 21:46:58 +00008967 * "complete()" function
8968 */
Bram Moolenaarade00832006-03-10 21:46:58 +00008969 static void
8970f_complete(argvars, rettv)
8971 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008972 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00008973{
8974 int startcol;
8975
8976 if ((State & INSERT) == 0)
8977 {
8978 EMSG(_("E785: complete() can only be used in Insert mode"));
8979 return;
8980 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00008981
8982 /* Check for undo allowed here, because if something was already inserted
8983 * the line was already saved for undo and this check isn't done. */
8984 if (!undo_allowed())
8985 return;
8986
Bram Moolenaarade00832006-03-10 21:46:58 +00008987 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
8988 {
8989 EMSG(_(e_invarg));
8990 return;
8991 }
8992
8993 startcol = get_tv_number_chk(&argvars[0], NULL);
8994 if (startcol <= 0)
8995 return;
8996
8997 set_completion(startcol - 1, argvars[1].vval.v_list);
8998}
8999
9000/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009001 * "complete_add()" function
9002 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009003 static void
9004f_complete_add(argvars, rettv)
9005 typval_T *argvars;
9006 typval_T *rettv;
9007{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009008 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009009}
9010
9011/*
9012 * "complete_check()" function
9013 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009014 static void
9015f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009016 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009017 typval_T *rettv;
9018{
9019 int saved = RedrawingDisabled;
9020
9021 RedrawingDisabled = 0;
9022 ins_compl_check_keys(0);
9023 rettv->vval.v_number = compl_interrupted;
9024 RedrawingDisabled = saved;
9025}
9026#endif
9027
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028/*
9029 * "confirm(message, buttons[, default [, type]])" function
9030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009033 typval_T *argvars UNUSED;
9034 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035{
9036#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9037 char_u *message;
9038 char_u *buttons = NULL;
9039 char_u buf[NUMBUFLEN];
9040 char_u buf2[NUMBUFLEN];
9041 int def = 1;
9042 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 char_u *typestr;
9044 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009046 message = get_tv_string_chk(&argvars[0]);
9047 if (message == NULL)
9048 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009049 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9052 if (buttons == NULL)
9053 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009054 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009056 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009057 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009059 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9060 if (typestr == NULL)
9061 error = TRUE;
9062 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009064 switch (TOUPPER_ASC(*typestr))
9065 {
9066 case 'E': type = VIM_ERROR; break;
9067 case 'Q': type = VIM_QUESTION; break;
9068 case 'I': type = VIM_INFO; break;
9069 case 'W': type = VIM_WARNING; break;
9070 case 'G': type = VIM_GENERIC; break;
9071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 }
9073 }
9074 }
9075 }
9076
9077 if (buttons == NULL || *buttons == NUL)
9078 buttons = (char_u *)_("&Ok");
9079
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009080 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009081 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083#endif
9084}
9085
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009086/*
9087 * "copy()" function
9088 */
9089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009091 typval_T *argvars;
9092 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009093{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009094 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009095}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009097#ifdef FEAT_FLOAT
9098/*
9099 * "cos()" function
9100 */
9101 static void
9102f_cos(argvars, rettv)
9103 typval_T *argvars;
9104 typval_T *rettv;
9105{
9106 float_T f;
9107
9108 rettv->v_type = VAR_FLOAT;
9109 if (get_float_arg(argvars, &f) == OK)
9110 rettv->vval.v_float = cos(f);
9111 else
9112 rettv->vval.v_float = 0.0;
9113}
9114#endif
9115
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009117 * "count()" function
9118 */
9119 static void
9120f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009121 typval_T *argvars;
9122 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009123{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009124 long n = 0;
9125 int ic = FALSE;
9126
Bram Moolenaare9a41262005-01-15 22:18:47 +00009127 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009128 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009129 listitem_T *li;
9130 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009131 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009132
Bram Moolenaare9a41262005-01-15 22:18:47 +00009133 if ((l = argvars[0].vval.v_list) != NULL)
9134 {
9135 li = l->lv_first;
9136 if (argvars[2].v_type != VAR_UNKNOWN)
9137 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009138 int error = FALSE;
9139
9140 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009141 if (argvars[3].v_type != VAR_UNKNOWN)
9142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009143 idx = get_tv_number_chk(&argvars[3], &error);
9144 if (!error)
9145 {
9146 li = list_find(l, idx);
9147 if (li == NULL)
9148 EMSGN(_(e_listidx), idx);
9149 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009150 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009151 if (error)
9152 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009153 }
9154
9155 for ( ; li != NULL; li = li->li_next)
9156 if (tv_equal(&li->li_tv, &argvars[1], ic))
9157 ++n;
9158 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009159 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009160 else if (argvars[0].v_type == VAR_DICT)
9161 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009162 int todo;
9163 dict_T *d;
9164 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009165
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009166 if ((d = argvars[0].vval.v_dict) != NULL)
9167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009168 int error = FALSE;
9169
Bram Moolenaare9a41262005-01-15 22:18:47 +00009170 if (argvars[2].v_type != VAR_UNKNOWN)
9171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009172 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009173 if (argvars[3].v_type != VAR_UNKNOWN)
9174 EMSG(_(e_invarg));
9175 }
9176
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009177 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009178 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009179 {
9180 if (!HASHITEM_EMPTY(hi))
9181 {
9182 --todo;
9183 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9184 ++n;
9185 }
9186 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009187 }
9188 }
9189 else
9190 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009191 rettv->vval.v_number = n;
9192}
9193
9194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9196 *
9197 * Checks the existence of a cscope connection.
9198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009201 typval_T *argvars UNUSED;
9202 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203{
9204#ifdef FEAT_CSCOPE
9205 int num = 0;
9206 char_u *dbpath = NULL;
9207 char_u *prepend = NULL;
9208 char_u buf[NUMBUFLEN];
9209
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009210 if (argvars[0].v_type != VAR_UNKNOWN
9211 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009213 num = (int)get_tv_number(&argvars[0]);
9214 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009215 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 }
9218
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220#endif
9221}
9222
9223/*
9224 * "cursor(lnum, col)" function
9225 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009226 * Moves the cursor to the specified line and column.
9227 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009230f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009231 typval_T *argvars;
9232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233{
9234 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009235#ifdef FEAT_VIRTUALEDIT
9236 long coladd = 0;
9237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009239 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009240 if (argvars[1].v_type == VAR_UNKNOWN)
9241 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009242 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009243
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009244 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009245 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009246 line = pos.lnum;
9247 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009248#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009249 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009250#endif
9251 }
9252 else
9253 {
9254 line = get_tv_lnum(argvars);
9255 col = get_tv_number_chk(&argvars[1], NULL);
9256#ifdef FEAT_VIRTUALEDIT
9257 if (argvars[2].v_type != VAR_UNKNOWN)
9258 coladd = get_tv_number_chk(&argvars[2], NULL);
9259#endif
9260 }
9261 if (line < 0 || col < 0
9262#ifdef FEAT_VIRTUALEDIT
9263 || coladd < 0
9264#endif
9265 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009266 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 if (line > 0)
9268 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 if (col > 0)
9270 curwin->w_cursor.col = col - 1;
9271#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009272 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273#endif
9274
9275 /* Make sure the cursor is in a valid position. */
9276 check_cursor();
9277#ifdef FEAT_MBYTE
9278 /* Correct cursor for multi-byte character. */
9279 if (has_mbyte)
9280 mb_adjust_cursor();
9281#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009282
9283 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009284 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285}
9286
9287/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009288 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 */
9290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009292 typval_T *argvars;
9293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009295 int noref = 0;
9296
9297 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009298 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009299 if (noref < 0 || noref > 1)
9300 EMSG(_(e_invarg));
9301 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009302 {
9303 current_copyID += COPYID_INC;
9304 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306}
9307
9308/*
9309 * "delete()" function
9310 */
9311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009312f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009313 typval_T *argvars;
9314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315{
9316 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320}
9321
9322/*
9323 * "did_filetype()" function
9324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009327 typval_T *argvars UNUSED;
9328 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329{
9330#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009331 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332#endif
9333}
9334
9335/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009336 * "diff_filler()" function
9337 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009340 typval_T *argvars UNUSED;
9341 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009342{
9343#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009345#endif
9346}
9347
9348/*
9349 * "diff_hlID()" function
9350 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009353 typval_T *argvars UNUSED;
9354 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009355{
9356#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009357 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009358 static linenr_T prev_lnum = 0;
9359 static int changedtick = 0;
9360 static int fnum = 0;
9361 static int change_start = 0;
9362 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009363 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009364 int filler_lines;
9365 int col;
9366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009367 if (lnum < 0) /* ignore type error in {lnum} arg */
9368 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009369 if (lnum != prev_lnum
9370 || changedtick != curbuf->b_changedtick
9371 || fnum != curbuf->b_fnum)
9372 {
9373 /* New line, buffer, change: need to get the values. */
9374 filler_lines = diff_check(curwin, lnum);
9375 if (filler_lines < 0)
9376 {
9377 if (filler_lines == -1)
9378 {
9379 change_start = MAXCOL;
9380 change_end = -1;
9381 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9382 hlID = HLF_ADD; /* added line */
9383 else
9384 hlID = HLF_CHD; /* changed line */
9385 }
9386 else
9387 hlID = HLF_ADD; /* added line */
9388 }
9389 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009390 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009391 prev_lnum = lnum;
9392 changedtick = curbuf->b_changedtick;
9393 fnum = curbuf->b_fnum;
9394 }
9395
9396 if (hlID == HLF_CHD || hlID == HLF_TXD)
9397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009399 if (col >= change_start && col <= change_end)
9400 hlID = HLF_TXD; /* changed text */
9401 else
9402 hlID = HLF_CHD; /* changed line */
9403 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009404 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009405#endif
9406}
9407
9408/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009409 * "empty({expr})" function
9410 */
9411 static void
9412f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009413 typval_T *argvars;
9414 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009415{
9416 int n;
9417
9418 switch (argvars[0].v_type)
9419 {
9420 case VAR_STRING:
9421 case VAR_FUNC:
9422 n = argvars[0].vval.v_string == NULL
9423 || *argvars[0].vval.v_string == NUL;
9424 break;
9425 case VAR_NUMBER:
9426 n = argvars[0].vval.v_number == 0;
9427 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009428#ifdef FEAT_FLOAT
9429 case VAR_FLOAT:
9430 n = argvars[0].vval.v_float == 0.0;
9431 break;
9432#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009433 case VAR_LIST:
9434 n = argvars[0].vval.v_list == NULL
9435 || argvars[0].vval.v_list->lv_first == NULL;
9436 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009437 case VAR_DICT:
9438 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009440 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009441 default:
9442 EMSG2(_(e_intern2), "f_empty()");
9443 n = 0;
9444 }
9445
9446 rettv->vval.v_number = n;
9447}
9448
9449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 * "escape({string}, {chars})" function
9451 */
9452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009453f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009454 typval_T *argvars;
9455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456{
9457 char_u buf[NUMBUFLEN];
9458
Bram Moolenaar758711c2005-02-02 23:11:38 +00009459 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9460 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009461 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462}
9463
9464/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009465 * "eval()" function
9466 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009467 static void
9468f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009469 typval_T *argvars;
9470 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009471{
9472 char_u *s;
9473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 s = get_tv_string_chk(&argvars[0]);
9475 if (s != NULL)
9476 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009478 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9479 {
9480 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009481 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009482 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009483 else if (*s != NUL)
9484 EMSG(_(e_trailing));
9485}
9486
9487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 * "eventhandler()" function
9489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009492 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496}
9497
9498/*
9499 * "executable()" function
9500 */
9501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009503 typval_T *argvars;
9504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507}
9508
9509/*
9510 * "exists()" function
9511 */
9512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009513f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009514 typval_T *argvars;
9515 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
9517 char_u *p;
9518 char_u *name;
9519 int n = FALSE;
9520 int len = 0;
9521
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009522 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 if (*p == '$') /* environment variable */
9524 {
9525 /* first try "normal" environment variables (fast) */
9526 if (mch_getenv(p + 1) != NULL)
9527 n = TRUE;
9528 else
9529 {
9530 /* try expanding things like $VIM and ${HOME} */
9531 p = expand_env_save(p);
9532 if (p != NULL && *p != '$')
9533 n = TRUE;
9534 vim_free(p);
9535 }
9536 }
9537 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009540 if (*skipwhite(p) != NUL)
9541 n = FALSE; /* trailing garbage */
9542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 else if (*p == '*') /* internal or user defined function */
9544 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009545 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 }
9547 else if (*p == ':')
9548 {
9549 n = cmd_exists(p + 1);
9550 }
9551 else if (*p == '#')
9552 {
9553#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009554 if (p[1] == '#')
9555 n = autocmd_supported(p + 2);
9556 else
9557 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558#endif
9559 }
9560 else /* internal variable */
9561 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009562 char_u *tofree;
9563 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009565 /* get_name_len() takes care of expanding curly braces */
9566 name = p;
9567 len = get_name_len(&p, &tofree, TRUE, FALSE);
9568 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009570 if (tofree != NULL)
9571 name = tofree;
9572 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9573 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009575 /* handle d.key, l[idx], f(expr) */
9576 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9577 if (n)
9578 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 }
9580 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009581 if (*p != NUL)
9582 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009584 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 }
9586
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009587 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588}
9589
9590/*
9591 * "expand()" function
9592 */
9593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009595 typval_T *argvars;
9596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
9598 char_u *s;
9599 int len;
9600 char_u *errormsg;
9601 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9602 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009603 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605 rettv->v_type = VAR_STRING;
9606 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 if (*s == '%' || *s == '#' || *s == '<')
9608 {
9609 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009610 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 --emsg_off;
9612 }
9613 else
9614 {
9615 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009616 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009617 if (argvars[1].v_type != VAR_UNKNOWN
9618 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009620 if (!error)
9621 {
9622 ExpandInit(&xpc);
9623 xpc.xp_context = EXPAND_FILES;
9624 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009625 }
9626 else
9627 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 }
9629}
9630
9631/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009632 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009634 */
9635 static void
9636f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009637 typval_T *argvars;
9638 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009639{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009640 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009641 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009642 list_T *l1, *l2;
9643 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009644 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009645 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009646
Bram Moolenaare9a41262005-01-15 22:18:47 +00009647 l1 = argvars[0].vval.v_list;
9648 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009649 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9650 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009651 {
9652 if (argvars[2].v_type != VAR_UNKNOWN)
9653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009654 before = get_tv_number_chk(&argvars[2], &error);
9655 if (error)
9656 return; /* type error; errmsg already given */
9657
Bram Moolenaar758711c2005-02-02 23:11:38 +00009658 if (before == l1->lv_len)
9659 item = NULL;
9660 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009661 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009662 item = list_find(l1, before);
9663 if (item == NULL)
9664 {
9665 EMSGN(_(e_listidx), before);
9666 return;
9667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009668 }
9669 }
9670 else
9671 item = NULL;
9672 list_extend(l1, l2, item);
9673
Bram Moolenaare9a41262005-01-15 22:18:47 +00009674 copy_tv(&argvars[0], rettv);
9675 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009676 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009677 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9678 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009679 dict_T *d1, *d2;
9680 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009681 char_u *action;
9682 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009683 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009684 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009685
9686 d1 = argvars[0].vval.v_dict;
9687 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009688 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9689 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009690 {
9691 /* Check the third argument. */
9692 if (argvars[2].v_type != VAR_UNKNOWN)
9693 {
9694 static char *(av[]) = {"keep", "force", "error"};
9695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009696 action = get_tv_string_chk(&argvars[2]);
9697 if (action == NULL)
9698 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009699 for (i = 0; i < 3; ++i)
9700 if (STRCMP(action, av[i]) == 0)
9701 break;
9702 if (i == 3)
9703 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009704 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009705 return;
9706 }
9707 }
9708 else
9709 action = (char_u *)"force";
9710
9711 /* Go over all entries in the second dict and add them to the
9712 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009713 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009714 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009716 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009717 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009718 --todo;
9719 di1 = dict_find(d1, hi2->hi_key, -1);
9720 if (di1 == NULL)
9721 {
9722 di1 = dictitem_copy(HI2DI(hi2));
9723 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9724 dictitem_free(di1);
9725 }
9726 else if (*action == 'e')
9727 {
9728 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9729 break;
9730 }
9731 else if (*action == 'f')
9732 {
9733 clear_tv(&di1->di_tv);
9734 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9735 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009736 }
9737 }
9738
Bram Moolenaare9a41262005-01-15 22:18:47 +00009739 copy_tv(&argvars[0], rettv);
9740 }
9741 }
9742 else
9743 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009744}
9745
9746/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009747 * "feedkeys()" function
9748 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009749 static void
9750f_feedkeys(argvars, rettv)
9751 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009752 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009753{
9754 int remap = TRUE;
9755 char_u *keys, *flags;
9756 char_u nbuf[NUMBUFLEN];
9757 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009758 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009759
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009760 /* This is not allowed in the sandbox. If the commands would still be
9761 * executed in the sandbox it would be OK, but it probably happens later,
9762 * when "sandbox" is no longer set. */
9763 if (check_secure())
9764 return;
9765
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009766 keys = get_tv_string(&argvars[0]);
9767 if (*keys != NUL)
9768 {
9769 if (argvars[1].v_type != VAR_UNKNOWN)
9770 {
9771 flags = get_tv_string_buf(&argvars[1], nbuf);
9772 for ( ; *flags != NUL; ++flags)
9773 {
9774 switch (*flags)
9775 {
9776 case 'n': remap = FALSE; break;
9777 case 'm': remap = TRUE; break;
9778 case 't': typed = TRUE; break;
9779 }
9780 }
9781 }
9782
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009783 /* Need to escape K_SPECIAL and CSI before putting the string in the
9784 * typeahead buffer. */
9785 keys_esc = vim_strsave_escape_csi(keys);
9786 if (keys_esc != NULL)
9787 {
9788 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009789 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009790 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009791 if (vgetc_busy)
9792 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009793 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009794 }
9795}
9796
9797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 * "filereadable()" function
9799 */
9800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009802 typval_T *argvars;
9803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804{
Bram Moolenaarc236c162008-07-13 17:41:49 +00009805 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 char_u *p;
9807 int n;
9808
Bram Moolenaarc236c162008-07-13 17:41:49 +00009809#ifndef O_NONBLOCK
9810# define O_NONBLOCK 0
9811#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +00009813 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
9814 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 {
9816 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +00009817 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 }
9819 else
9820 n = FALSE;
9821
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823}
9824
9825/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009826 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 * rights to write into.
9828 */
9829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009831 typval_T *argvars;
9832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00009834 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835}
9836
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009837static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009838
9839 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009840findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009841 typval_T *argvars;
9842 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009843 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009844{
9845#ifdef FEAT_SEARCHPATH
9846 char_u *fname;
9847 char_u *fresult = NULL;
9848 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
9849 char_u *p;
9850 char_u pathbuf[NUMBUFLEN];
9851 int count = 1;
9852 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009853 int error = FALSE;
9854#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009855
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009856 rettv->vval.v_string = NULL;
9857 rettv->v_type = VAR_STRING;
9858
9859#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009860 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009861
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009862 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009863 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009864 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
9865 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009866 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009867 else
9868 {
9869 if (*p != NUL)
9870 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009871
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009872 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009873 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009874 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009875 }
9876
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009877 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
9878 error = TRUE;
9879
9880 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009882 do
9883 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009884 if (rettv->v_type == VAR_STRING)
9885 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009886 fresult = find_file_in_path_option(first ? fname : NULL,
9887 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +00009888 0, first, path,
9889 find_what,
9890 curbuf->b_ffname,
9891 find_what == FINDFILE_DIR
9892 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009894
9895 if (fresult != NULL && rettv->v_type == VAR_LIST)
9896 list_append_string(rettv->vval.v_list, fresult, -1);
9897
9898 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009899 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009900
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009901 if (rettv->v_type == VAR_STRING)
9902 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009903#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009904}
9905
Bram Moolenaar33570922005-01-25 22:26:29 +00009906static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
9907static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009908
9909/*
9910 * Implementation of map() and filter().
9911 */
9912 static void
9913filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00009914 typval_T *argvars;
9915 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009916 int map;
9917{
9918 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00009919 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 listitem_T *li, *nli;
9921 list_T *l = NULL;
9922 dictitem_T *di;
9923 hashtab_T *ht;
9924 hashitem_T *hi;
9925 dict_T *d = NULL;
9926 typval_T save_val;
9927 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009929 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009930 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009931 int save_did_emsg;
Bram Moolenaar627b1d32009-11-17 11:20:35 +00009932 int index = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009933
Bram Moolenaare9a41262005-01-15 22:18:47 +00009934 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009935 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009936 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009937 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009938 return;
9939 }
9940 else if (argvars[0].v_type == VAR_DICT)
9941 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009942 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +00009943 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009944 return;
9945 }
9946 else
9947 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009948 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009949 return;
9950 }
9951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009952 expr = get_tv_string_buf_chk(&argvars[1], buf);
9953 /* On type errors, the preceding call has already displayed an error
9954 * message. Avoid a misleading error message for an empty string that
9955 * was not passed as argument. */
9956 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009958 prepare_vimvar(VV_VAL, &save_val);
9959 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009960
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009961 /* We reset "did_emsg" to be able to detect whether an error
9962 * occurred during evaluation of the expression. */
9963 save_did_emsg = did_emsg;
9964 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009965
Bram Moolenaar627b1d32009-11-17 11:20:35 +00009966 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009967 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009969 vimvars[VV_KEY].vv_type = VAR_STRING;
9970
9971 ht = &d->dv_hashtab;
9972 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009973 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009974 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009975 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009976 if (!HASHITEM_EMPTY(hi))
9977 {
9978 --todo;
9979 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +00009980 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009981 break;
9982 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +00009983 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00009984 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009985 break;
9986 if (!map && rem)
9987 dictitem_remove(d, di);
9988 clear_tv(&vimvars[VV_KEY].vv_tv);
9989 }
9990 }
9991 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009992 }
9993 else
9994 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +00009995 vimvars[VV_KEY].vv_type = VAR_NUMBER;
9996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009997 for (li = l->lv_first; li != NULL; li = nli)
9998 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009999 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010000 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010001 nli = li->li_next;
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010002 vimvars[VV_KEY].vv_nr = index;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010003 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010004 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010005 break;
10006 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010007 listitem_remove(l, li);
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010008 ++index;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010009 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010010 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010011
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010012 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010013 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010014
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010015 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010016 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010017
10018 copy_tv(&argvars[0], rettv);
10019}
10020
10021 static int
10022filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010023 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010024 char_u *expr;
10025 int map;
10026 int *remp;
10027{
Bram Moolenaar33570922005-01-25 22:26:29 +000010028 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010029 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010030 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010031
Bram Moolenaar33570922005-01-25 22:26:29 +000010032 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010033 s = expr;
10034 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010035 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010036 if (*s != NUL) /* check for trailing chars after expr */
10037 {
10038 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010039 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010040 }
10041 if (map)
10042 {
10043 /* map(): replace the list item value */
10044 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010045 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010046 *tv = rettv;
10047 }
10048 else
10049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010050 int error = FALSE;
10051
Bram Moolenaare9a41262005-01-15 22:18:47 +000010052 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010053 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010054 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010055 /* On type error, nothing has been removed; return FAIL to stop the
10056 * loop. The error message was given by get_tv_number_chk(). */
10057 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010058 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010059 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010060 retval = OK;
10061theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010063 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010064}
10065
10066/*
10067 * "filter()" function
10068 */
10069 static void
10070f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010071 typval_T *argvars;
10072 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010073{
10074 filter_map(argvars, rettv, FALSE);
10075}
10076
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010077/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010078 * "finddir({fname}[, {path}[, {count}]])" function
10079 */
10080 static void
10081f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010082 typval_T *argvars;
10083 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010084{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010085 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010086}
10087
10088/*
10089 * "findfile({fname}[, {path}[, {count}]])" function
10090 */
10091 static void
10092f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010093 typval_T *argvars;
10094 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010095{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010096 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010097}
10098
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010099#ifdef FEAT_FLOAT
10100/*
10101 * "float2nr({float})" function
10102 */
10103 static void
10104f_float2nr(argvars, rettv)
10105 typval_T *argvars;
10106 typval_T *rettv;
10107{
10108 float_T f;
10109
10110 if (get_float_arg(argvars, &f) == OK)
10111 {
10112 if (f < -0x7fffffff)
10113 rettv->vval.v_number = -0x7fffffff;
10114 else if (f > 0x7fffffff)
10115 rettv->vval.v_number = 0x7fffffff;
10116 else
10117 rettv->vval.v_number = (varnumber_T)f;
10118 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010119}
10120
10121/*
10122 * "floor({float})" function
10123 */
10124 static void
10125f_floor(argvars, rettv)
10126 typval_T *argvars;
10127 typval_T *rettv;
10128{
10129 float_T f;
10130
10131 rettv->v_type = VAR_FLOAT;
10132 if (get_float_arg(argvars, &f) == OK)
10133 rettv->vval.v_float = floor(f);
10134 else
10135 rettv->vval.v_float = 0.0;
10136}
10137#endif
10138
Bram Moolenaar0d660222005-01-07 21:51:51 +000010139/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010140 * "fnameescape({string})" function
10141 */
10142 static void
10143f_fnameescape(argvars, rettv)
10144 typval_T *argvars;
10145 typval_T *rettv;
10146{
10147 rettv->vval.v_string = vim_strsave_fnameescape(
10148 get_tv_string(&argvars[0]), FALSE);
10149 rettv->v_type = VAR_STRING;
10150}
10151
10152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 * "fnamemodify({fname}, {mods})" function
10154 */
10155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010156f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010157 typval_T *argvars;
10158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159{
10160 char_u *fname;
10161 char_u *mods;
10162 int usedlen = 0;
10163 int len;
10164 char_u *fbuf = NULL;
10165 char_u buf[NUMBUFLEN];
10166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010167 fname = get_tv_string_chk(&argvars[0]);
10168 mods = get_tv_string_buf_chk(&argvars[1], buf);
10169 if (fname == NULL || mods == NULL)
10170 fname = NULL;
10171 else
10172 {
10173 len = (int)STRLEN(fname);
10174 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010177 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010179 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010181 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 vim_free(fbuf);
10183}
10184
Bram Moolenaar33570922005-01-25 22:26:29 +000010185static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186
10187/*
10188 * "foldclosed()" function
10189 */
10190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010191foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010192 typval_T *argvars;
10193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 int end;
10195{
10196#ifdef FEAT_FOLDING
10197 linenr_T lnum;
10198 linenr_T first, last;
10199
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010200 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10202 {
10203 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10204 {
10205 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010206 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010208 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 return;
10210 }
10211 }
10212#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214}
10215
10216/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010217 * "foldclosed()" function
10218 */
10219 static void
10220f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010221 typval_T *argvars;
10222 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010223{
10224 foldclosed_both(argvars, rettv, FALSE);
10225}
10226
10227/*
10228 * "foldclosedend()" function
10229 */
10230 static void
10231f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010232 typval_T *argvars;
10233 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010234{
10235 foldclosed_both(argvars, rettv, TRUE);
10236}
10237
10238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 * "foldlevel()" function
10240 */
10241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010242f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010243 typval_T *argvars;
10244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245{
10246#ifdef FEAT_FOLDING
10247 linenr_T lnum;
10248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010249 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010251 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253}
10254
10255/*
10256 * "foldtext()" function
10257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010259f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010260 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262{
10263#ifdef FEAT_FOLDING
10264 linenr_T lnum;
10265 char_u *s;
10266 char_u *r;
10267 int len;
10268 char *txt;
10269#endif
10270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010271 rettv->v_type = VAR_STRING;
10272 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010274 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10275 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10276 <= curbuf->b_ml.ml_line_count
10277 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 {
10279 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010280 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10281 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 {
10283 if (!linewhite(lnum))
10284 break;
10285 ++lnum;
10286 }
10287
10288 /* Find interesting text in this line. */
10289 s = skipwhite(ml_get(lnum));
10290 /* skip C comment-start */
10291 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010294 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010295 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010296 {
10297 s = skipwhite(ml_get(lnum + 1));
10298 if (*s == '*')
10299 s = skipwhite(s + 1);
10300 }
10301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 txt = _("+-%s%3ld lines: ");
10303 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010304 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 + 20 /* for %3ld */
10306 + STRLEN(s))); /* concatenated */
10307 if (r != NULL)
10308 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010309 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10310 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10311 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 len = (int)STRLEN(r);
10313 STRCAT(r, s);
10314 /* remove 'foldmarker' and 'commentstring' */
10315 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010316 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 }
10318 }
10319#endif
10320}
10321
10322/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010323 * "foldtextresult(lnum)" function
10324 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010327 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010328 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010329{
10330#ifdef FEAT_FOLDING
10331 linenr_T lnum;
10332 char_u *text;
10333 char_u buf[51];
10334 foldinfo_T foldinfo;
10335 int fold_count;
10336#endif
10337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010338 rettv->v_type = VAR_STRING;
10339 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010340#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010341 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010342 /* treat illegal types and illegal string values for {lnum} the same */
10343 if (lnum < 0)
10344 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010345 fold_count = foldedCount(curwin, lnum, &foldinfo);
10346 if (fold_count > 0)
10347 {
10348 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10349 &foldinfo, buf);
10350 if (text == buf)
10351 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010352 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010353 }
10354#endif
10355}
10356
10357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 * "foreground()" function
10359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010361f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010362 typval_T *argvars UNUSED;
10363 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365#ifdef FEAT_GUI
10366 if (gui.in_use)
10367 gui_mch_set_foreground();
10368#else
10369# ifdef WIN32
10370 win32_set_foreground();
10371# endif
10372#endif
10373}
10374
10375/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010376 * "function()" function
10377 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010379f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010380 typval_T *argvars;
10381 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010382{
10383 char_u *s;
10384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010385 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010386 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010387 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010388 /* Don't check an autoload name for existence here. */
10389 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010390 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010391 else
10392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393 rettv->vval.v_string = vim_strsave(s);
10394 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010395 }
10396}
10397
10398/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010399 * "garbagecollect()" function
10400 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010401 static void
10402f_garbagecollect(argvars, rettv)
10403 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010404 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010405{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010406 /* This is postponed until we are back at the toplevel, because we may be
10407 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10408 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010409
10410 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10411 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010412}
10413
10414/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010415 * "get()" function
10416 */
10417 static void
10418f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010419 typval_T *argvars;
10420 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010421{
Bram Moolenaar33570922005-01-25 22:26:29 +000010422 listitem_T *li;
10423 list_T *l;
10424 dictitem_T *di;
10425 dict_T *d;
10426 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010427
Bram Moolenaare9a41262005-01-15 22:18:47 +000010428 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010429 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010430 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 int error = FALSE;
10433
10434 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10435 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010436 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010437 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010438 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010439 else if (argvars[0].v_type == VAR_DICT)
10440 {
10441 if ((d = argvars[0].vval.v_dict) != NULL)
10442 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010443 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010444 if (di != NULL)
10445 tv = &di->di_tv;
10446 }
10447 }
10448 else
10449 EMSG2(_(e_listdictarg), "get()");
10450
10451 if (tv == NULL)
10452 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010453 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454 copy_tv(&argvars[2], rettv);
10455 }
10456 else
10457 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010458}
10459
Bram Moolenaar342337a2005-07-21 21:11:17 +000010460static 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 +000010461
10462/*
10463 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010464 * Return a range (from start to end) of lines in rettv from the specified
10465 * buffer.
10466 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010467 */
10468 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010469get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010470 buf_T *buf;
10471 linenr_T start;
10472 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010473 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010474 typval_T *rettv;
10475{
10476 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010477
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010478 if (retlist && rettv_list_alloc(rettv) == FAIL)
10479 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010480
10481 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10482 return;
10483
10484 if (!retlist)
10485 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010486 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10487 p = ml_get_buf(buf, start, FALSE);
10488 else
10489 p = (char_u *)"";
10490
10491 rettv->v_type = VAR_STRING;
10492 rettv->vval.v_string = vim_strsave(p);
10493 }
10494 else
10495 {
10496 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010497 return;
10498
10499 if (start < 1)
10500 start = 1;
10501 if (end > buf->b_ml.ml_line_count)
10502 end = buf->b_ml.ml_line_count;
10503 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010504 if (list_append_string(rettv->vval.v_list,
10505 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010506 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010507 }
10508}
10509
10510/*
10511 * "getbufline()" function
10512 */
10513 static void
10514f_getbufline(argvars, rettv)
10515 typval_T *argvars;
10516 typval_T *rettv;
10517{
10518 linenr_T lnum;
10519 linenr_T end;
10520 buf_T *buf;
10521
10522 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10523 ++emsg_off;
10524 buf = get_buf_tv(&argvars[0]);
10525 --emsg_off;
10526
Bram Moolenaar661b1822005-07-28 22:36:45 +000010527 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010528 if (argvars[2].v_type == VAR_UNKNOWN)
10529 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010530 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010531 end = get_tv_lnum_buf(&argvars[2], buf);
10532
Bram Moolenaar342337a2005-07-21 21:11:17 +000010533 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010534}
10535
Bram Moolenaar0d660222005-01-07 21:51:51 +000010536/*
10537 * "getbufvar()" function
10538 */
10539 static void
10540f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010541 typval_T *argvars;
10542 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010543{
10544 buf_T *buf;
10545 buf_T *save_curbuf;
10546 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010547 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010549 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10550 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010551 ++emsg_off;
10552 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010553
10554 rettv->v_type = VAR_STRING;
10555 rettv->vval.v_string = NULL;
10556
10557 if (buf != NULL && varname != NULL)
10558 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010559 /* set curbuf to be our buf, temporarily */
10560 save_curbuf = curbuf;
10561 curbuf = buf;
10562
Bram Moolenaar0d660222005-01-07 21:51:51 +000010563 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010564 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010565 else
10566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010567 if (*varname == NUL)
10568 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10569 * scope prefix before the NUL byte is required by
10570 * find_var_in_ht(). */
10571 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010572 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010573 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010574 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010575 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010576 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010577
10578 /* restore previous notion of curbuf */
10579 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010580 }
10581
10582 --emsg_off;
10583}
10584
10585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 * "getchar()" function
10587 */
10588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010589f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010590 typval_T *argvars;
10591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592{
10593 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010594 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010596 /* Position the cursor. Needed after a message that ends in a space. */
10597 windgoto(msg_row, msg_col);
10598
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 ++no_mapping;
10600 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010601 for (;;)
10602 {
10603 if (argvars[0].v_type == VAR_UNKNOWN)
10604 /* getchar(): blocking wait. */
10605 n = safe_vgetc();
10606 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10607 /* getchar(1): only check if char avail */
10608 n = vpeekc();
10609 else if (error || vpeekc() == NUL)
10610 /* illegal argument or getchar(0) and no char avail: return zero */
10611 n = 0;
10612 else
10613 /* getchar(0) and char avail: return char */
10614 n = safe_vgetc();
10615 if (n == K_IGNORE)
10616 continue;
10617 break;
10618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 --no_mapping;
10620 --allow_keys;
10621
Bram Moolenaar219b8702006-11-01 14:32:36 +000010622 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10623 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10624 vimvars[VV_MOUSE_COL].vv_nr = 0;
10625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010626 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 if (IS_SPECIAL(n) || mod_mask != 0)
10628 {
10629 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10630 int i = 0;
10631
10632 /* Turn a special key into three bytes, plus modifier. */
10633 if (mod_mask != 0)
10634 {
10635 temp[i++] = K_SPECIAL;
10636 temp[i++] = KS_MODIFIER;
10637 temp[i++] = mod_mask;
10638 }
10639 if (IS_SPECIAL(n))
10640 {
10641 temp[i++] = K_SPECIAL;
10642 temp[i++] = K_SECOND(n);
10643 temp[i++] = K_THIRD(n);
10644 }
10645#ifdef FEAT_MBYTE
10646 else if (has_mbyte)
10647 i += (*mb_char2bytes)(n, temp + i);
10648#endif
10649 else
10650 temp[i++] = n;
10651 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010652 rettv->v_type = VAR_STRING;
10653 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010654
10655#ifdef FEAT_MOUSE
10656 if (n == K_LEFTMOUSE
10657 || n == K_LEFTMOUSE_NM
10658 || n == K_LEFTDRAG
10659 || n == K_LEFTRELEASE
10660 || n == K_LEFTRELEASE_NM
10661 || n == K_MIDDLEMOUSE
10662 || n == K_MIDDLEDRAG
10663 || n == K_MIDDLERELEASE
10664 || n == K_RIGHTMOUSE
10665 || n == K_RIGHTDRAG
10666 || n == K_RIGHTRELEASE
10667 || n == K_X1MOUSE
10668 || n == K_X1DRAG
10669 || n == K_X1RELEASE
10670 || n == K_X2MOUSE
10671 || n == K_X2DRAG
10672 || n == K_X2RELEASE
10673 || n == K_MOUSEDOWN
10674 || n == K_MOUSEUP)
10675 {
10676 int row = mouse_row;
10677 int col = mouse_col;
10678 win_T *win;
10679 linenr_T lnum;
10680# ifdef FEAT_WINDOWS
10681 win_T *wp;
10682# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010683 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010684
10685 if (row >= 0 && col >= 0)
10686 {
10687 /* Find the window at the mouse coordinates and compute the
10688 * text position. */
10689 win = mouse_find_win(&row, &col);
10690 (void)mouse_comp_pos(win, &row, &col, &lnum);
10691# ifdef FEAT_WINDOWS
10692 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010693 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010694# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010695 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010696 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10697 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10698 }
10699 }
10700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 }
10702}
10703
10704/*
10705 * "getcharmod()" function
10706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010708f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010709 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713}
10714
10715/*
10716 * "getcmdline()" function
10717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010720 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010723 rettv->v_type = VAR_STRING;
10724 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725}
10726
10727/*
10728 * "getcmdpos()" function
10729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010732 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736}
10737
10738/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010739 * "getcmdtype()" function
10740 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010741 static void
10742f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010743 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010744 typval_T *rettv;
10745{
10746 rettv->v_type = VAR_STRING;
10747 rettv->vval.v_string = alloc(2);
10748 if (rettv->vval.v_string != NULL)
10749 {
10750 rettv->vval.v_string[0] = get_cmdline_type();
10751 rettv->vval.v_string[1] = NUL;
10752 }
10753}
10754
10755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 * "getcwd()" function
10757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010759f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010760 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762{
10763 char_u cwd[MAXPATHL];
10764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010765 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010767 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 else
10769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000010772 if (rettv->vval.v_string != NULL)
10773 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774#endif
10775 }
10776}
10777
10778/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010779 * "getfontname()" function
10780 */
10781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010783 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010784 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010786 rettv->v_type = VAR_STRING;
10787 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010788#ifdef FEAT_GUI
10789 if (gui.in_use)
10790 {
10791 GuiFont font;
10792 char_u *name = NULL;
10793
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010794 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010795 {
10796 /* Get the "Normal" font. Either the name saved by
10797 * hl_set_font_name() or from the font ID. */
10798 font = gui.norm_font;
10799 name = hl_get_font_name();
10800 }
10801 else
10802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010803 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010804 if (STRCMP(name, "*") == 0) /* don't use font dialog */
10805 return;
10806 font = gui_mch_get_font(name, FALSE);
10807 if (font == NOFONT)
10808 return; /* Invalid font name, return empty string. */
10809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010810 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010811 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000010812 gui_mch_free_font(font);
10813 }
10814#endif
10815}
10816
10817/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010818 * "getfperm({fname})" function
10819 */
10820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010821f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010822 typval_T *argvars;
10823 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010824{
10825 char_u *fname;
10826 struct stat st;
10827 char_u *perm = NULL;
10828 char_u flags[] = "rwx";
10829 int i;
10830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010834 if (mch_stat((char *)fname, &st) >= 0)
10835 {
10836 perm = vim_strsave((char_u *)"---------");
10837 if (perm != NULL)
10838 {
10839 for (i = 0; i < 9; i++)
10840 {
10841 if (st.st_mode & (1 << (8 - i)))
10842 perm[i] = flags[i % 3];
10843 }
10844 }
10845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010847}
10848
10849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850 * "getfsize({fname})" function
10851 */
10852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010854 typval_T *argvars;
10855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856{
10857 char_u *fname;
10858 struct stat st;
10859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010860 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863
10864 if (mch_stat((char *)fname, &st) >= 0)
10865 {
10866 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000010869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010870 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000010871
10872 /* non-perfect check for overflow */
10873 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
10874 rettv->vval.v_number = -2;
10875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 }
10877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879}
10880
10881/*
10882 * "getftime({fname})" function
10883 */
10884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010886 typval_T *argvars;
10887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888{
10889 char_u *fname;
10890 struct stat st;
10891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010892 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893
10894 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898}
10899
10900/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010901 * "getftype({fname})" function
10902 */
10903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010904f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010905 typval_T *argvars;
10906 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010907{
10908 char_u *fname;
10909 struct stat st;
10910 char_u *type = NULL;
10911 char *t;
10912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010916 if (mch_lstat((char *)fname, &st) >= 0)
10917 {
10918#ifdef S_ISREG
10919 if (S_ISREG(st.st_mode))
10920 t = "file";
10921 else if (S_ISDIR(st.st_mode))
10922 t = "dir";
10923# ifdef S_ISLNK
10924 else if (S_ISLNK(st.st_mode))
10925 t = "link";
10926# endif
10927# ifdef S_ISBLK
10928 else if (S_ISBLK(st.st_mode))
10929 t = "bdev";
10930# endif
10931# ifdef S_ISCHR
10932 else if (S_ISCHR(st.st_mode))
10933 t = "cdev";
10934# endif
10935# ifdef S_ISFIFO
10936 else if (S_ISFIFO(st.st_mode))
10937 t = "fifo";
10938# endif
10939# ifdef S_ISSOCK
10940 else if (S_ISSOCK(st.st_mode))
10941 t = "fifo";
10942# endif
10943 else
10944 t = "other";
10945#else
10946# ifdef S_IFMT
10947 switch (st.st_mode & S_IFMT)
10948 {
10949 case S_IFREG: t = "file"; break;
10950 case S_IFDIR: t = "dir"; break;
10951# ifdef S_IFLNK
10952 case S_IFLNK: t = "link"; break;
10953# endif
10954# ifdef S_IFBLK
10955 case S_IFBLK: t = "bdev"; break;
10956# endif
10957# ifdef S_IFCHR
10958 case S_IFCHR: t = "cdev"; break;
10959# endif
10960# ifdef S_IFIFO
10961 case S_IFIFO: t = "fifo"; break;
10962# endif
10963# ifdef S_IFSOCK
10964 case S_IFSOCK: t = "socket"; break;
10965# endif
10966 default: t = "other";
10967 }
10968# else
10969 if (mch_isdir(fname))
10970 t = "dir";
10971 else
10972 t = "file";
10973# endif
10974#endif
10975 type = vim_strsave((char_u *)t);
10976 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010978}
10979
10980/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010981 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000010982 */
10983 static void
10984f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010985 typval_T *argvars;
10986 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010987{
10988 linenr_T lnum;
10989 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010990 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010991
10992 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010993 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010994 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010995 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010996 retlist = FALSE;
10997 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010998 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000010999 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011000 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011001 retlist = TRUE;
11002 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011003
Bram Moolenaar342337a2005-07-21 21:11:17 +000011004 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011005}
11006
11007/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011008 * "getmatches()" function
11009 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011010 static void
11011f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011012 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011013 typval_T *rettv;
11014{
11015#ifdef FEAT_SEARCH_EXTRA
11016 dict_T *dict;
11017 matchitem_T *cur = curwin->w_match_head;
11018
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011019 if (rettv_list_alloc(rettv) == OK)
11020 {
11021 while (cur != NULL)
11022 {
11023 dict = dict_alloc();
11024 if (dict == NULL)
11025 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011026 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11027 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11028 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11029 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11030 list_append_dict(rettv->vval.v_list, dict);
11031 cur = cur->next;
11032 }
11033 }
11034#endif
11035}
11036
11037/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011038 * "getpid()" function
11039 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011040 static void
11041f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011042 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011043 typval_T *rettv;
11044{
11045 rettv->vval.v_number = mch_get_pid();
11046}
11047
11048/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011049 * "getpos(string)" function
11050 */
11051 static void
11052f_getpos(argvars, rettv)
11053 typval_T *argvars;
11054 typval_T *rettv;
11055{
11056 pos_T *fp;
11057 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011058 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011059
11060 if (rettv_list_alloc(rettv) == OK)
11061 {
11062 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011063 fp = var2fpos(&argvars[0], TRUE, &fnum);
11064 if (fnum != -1)
11065 list_append_number(l, (varnumber_T)fnum);
11066 else
11067 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011068 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11069 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011070 list_append_number(l, (fp != NULL)
11071 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011072 : (varnumber_T)0);
11073 list_append_number(l,
11074#ifdef FEAT_VIRTUALEDIT
11075 (fp != NULL) ? (varnumber_T)fp->coladd :
11076#endif
11077 (varnumber_T)0);
11078 }
11079 else
11080 rettv->vval.v_number = FALSE;
11081}
11082
11083/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011084 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011085 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011086 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011087f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011088 typval_T *argvars UNUSED;
11089 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011090{
11091#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011092 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011093#endif
11094
Bram Moolenaar2641f772005-03-25 21:58:17 +000011095#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011096 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011097 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011098 wp = NULL;
11099 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11100 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011101 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011102 if (wp == NULL)
11103 return;
11104 }
11105
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011106 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011107 }
11108#endif
11109}
11110
11111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 * "getreg()" function
11113 */
11114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 typval_T *argvars;
11117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118{
11119 char_u *strregname;
11120 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011121 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011122 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011124 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 strregname = get_tv_string_chk(&argvars[0]);
11127 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011128 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011129 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011132 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 regname = (strregname == NULL ? '"' : *strregname);
11134 if (regname == 0)
11135 regname = '"';
11136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011138 rettv->vval.v_string = error ? NULL :
11139 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140}
11141
11142/*
11143 * "getregtype()" function
11144 */
11145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011147 typval_T *argvars;
11148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149{
11150 char_u *strregname;
11151 int regname;
11152 char_u buf[NUMBUFLEN + 2];
11153 long reglen = 0;
11154
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011155 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011156 {
11157 strregname = get_tv_string_chk(&argvars[0]);
11158 if (strregname == NULL) /* type error; errmsg already given */
11159 {
11160 rettv->v_type = VAR_STRING;
11161 rettv->vval.v_string = NULL;
11162 return;
11163 }
11164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 else
11166 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011167 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168
11169 regname = (strregname == NULL ? '"' : *strregname);
11170 if (regname == 0)
11171 regname = '"';
11172
11173 buf[0] = NUL;
11174 buf[1] = NUL;
11175 switch (get_reg_type(regname, &reglen))
11176 {
11177 case MLINE: buf[0] = 'V'; break;
11178 case MCHAR: buf[0] = 'v'; break;
11179#ifdef FEAT_VISUAL
11180 case MBLOCK:
11181 buf[0] = Ctrl_V;
11182 sprintf((char *)buf + 1, "%ld", reglen + 1);
11183 break;
11184#endif
11185 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186 rettv->v_type = VAR_STRING;
11187 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188}
11189
11190/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011191 * "gettabwinvar()" function
11192 */
11193 static void
11194f_gettabwinvar(argvars, rettv)
11195 typval_T *argvars;
11196 typval_T *rettv;
11197{
11198 getwinvar(argvars, rettv, 1);
11199}
11200
11201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 * "getwinposx()" function
11203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011205f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011206 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210#ifdef FEAT_GUI
11211 if (gui.in_use)
11212 {
11213 int x, y;
11214
11215 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011216 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 }
11218#endif
11219}
11220
11221/*
11222 * "getwinposy()" function
11223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011226 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230#ifdef FEAT_GUI
11231 if (gui.in_use)
11232 {
11233 int x, y;
11234
11235 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 }
11238#endif
11239}
11240
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011241/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011242 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011243 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011244 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011245find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011246 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011247 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011248{
11249#ifdef FEAT_WINDOWS
11250 win_T *wp;
11251#endif
11252 int nr;
11253
11254 nr = get_tv_number_chk(vp, NULL);
11255
11256#ifdef FEAT_WINDOWS
11257 if (nr < 0)
11258 return NULL;
11259 if (nr == 0)
11260 return curwin;
11261
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011262 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11263 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011264 if (--nr <= 0)
11265 break;
11266 return wp;
11267#else
11268 if (nr == 0 || nr == 1)
11269 return curwin;
11270 return NULL;
11271#endif
11272}
11273
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274/*
11275 * "getwinvar()" function
11276 */
11277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011278f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011279 typval_T *argvars;
11280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011282 getwinvar(argvars, rettv, 0);
11283}
11284
11285/*
11286 * getwinvar() and gettabwinvar()
11287 */
11288 static void
11289getwinvar(argvars, rettv, off)
11290 typval_T *argvars;
11291 typval_T *rettv;
11292 int off; /* 1 for gettabwinvar() */
11293{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 win_T *win, *oldcurwin;
11295 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011297 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011299#ifdef FEAT_WINDOWS
11300 if (off == 1)
11301 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11302 else
11303 tp = curtab;
11304#endif
11305 win = find_win_by_nr(&argvars[off], tp);
11306 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011307 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 rettv->v_type = VAR_STRING;
11310 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311
11312 if (win != NULL && varname != NULL)
11313 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011314 /* Set curwin to be our win, temporarily. Also set curbuf, so
11315 * that we can get buffer-local options. */
11316 oldcurwin = curwin;
11317 curwin = win;
11318 curbuf = win->w_buffer;
11319
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322 else
11323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011324 if (*varname == NUL)
11325 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11326 * scope prefix before the NUL byte is required by
11327 * find_var_in_ht(). */
11328 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011330 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011332 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011334
11335 /* restore previous notion of curwin */
11336 curwin = oldcurwin;
11337 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 }
11339
11340 --emsg_off;
11341}
11342
11343/*
11344 * "glob()" function
11345 */
11346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011348 typval_T *argvars;
11349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011351 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011353 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011355 /* When the optional second argument is non-zero, don't remove matches
11356 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11357 if (argvars[1].v_type != VAR_UNKNOWN
11358 && get_tv_number_chk(&argvars[1], &error))
11359 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011361 if (!error)
11362 {
11363 ExpandInit(&xpc);
11364 xpc.xp_context = EXPAND_FILES;
11365 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11366 NULL, flags, WILD_ALL);
11367 }
11368 else
11369 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370}
11371
11372/*
11373 * "globpath()" function
11374 */
11375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011377 typval_T *argvars;
11378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011380 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011382 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011383 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011385 /* When the optional second argument is non-zero, don't remove matches
11386 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11387 if (argvars[2].v_type != VAR_UNKNOWN
11388 && get_tv_number_chk(&argvars[2], &error))
11389 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011391 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011392 rettv->vval.v_string = NULL;
11393 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011394 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11395 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396}
11397
11398/*
11399 * "has()" function
11400 */
11401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011403 typval_T *argvars;
11404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405{
11406 int i;
11407 char_u *name;
11408 int n = FALSE;
11409 static char *(has_list[]) =
11410 {
11411#ifdef AMIGA
11412 "amiga",
11413# ifdef FEAT_ARP
11414 "arp",
11415# endif
11416#endif
11417#ifdef __BEOS__
11418 "beos",
11419#endif
11420#ifdef MSDOS
11421# ifdef DJGPP
11422 "dos32",
11423# else
11424 "dos16",
11425# endif
11426#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011427#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 "mac",
11429#endif
11430#if defined(MACOS_X_UNIX)
11431 "macunix",
11432#endif
11433#ifdef OS2
11434 "os2",
11435#endif
11436#ifdef __QNX__
11437 "qnx",
11438#endif
11439#ifdef RISCOS
11440 "riscos",
11441#endif
11442#ifdef UNIX
11443 "unix",
11444#endif
11445#ifdef VMS
11446 "vms",
11447#endif
11448#ifdef WIN16
11449 "win16",
11450#endif
11451#ifdef WIN32
11452 "win32",
11453#endif
11454#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11455 "win32unix",
11456#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011457#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458 "win64",
11459#endif
11460#ifdef EBCDIC
11461 "ebcdic",
11462#endif
11463#ifndef CASE_INSENSITIVE_FILENAME
11464 "fname_case",
11465#endif
11466#ifdef FEAT_ARABIC
11467 "arabic",
11468#endif
11469#ifdef FEAT_AUTOCMD
11470 "autocmd",
11471#endif
11472#ifdef FEAT_BEVAL
11473 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011474# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11475 "balloon_multiline",
11476# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477#endif
11478#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11479 "builtin_terms",
11480# ifdef ALL_BUILTIN_TCAPS
11481 "all_builtin_terms",
11482# endif
11483#endif
11484#ifdef FEAT_BYTEOFF
11485 "byte_offset",
11486#endif
11487#ifdef FEAT_CINDENT
11488 "cindent",
11489#endif
11490#ifdef FEAT_CLIENTSERVER
11491 "clientserver",
11492#endif
11493#ifdef FEAT_CLIPBOARD
11494 "clipboard",
11495#endif
11496#ifdef FEAT_CMDL_COMPL
11497 "cmdline_compl",
11498#endif
11499#ifdef FEAT_CMDHIST
11500 "cmdline_hist",
11501#endif
11502#ifdef FEAT_COMMENTS
11503 "comments",
11504#endif
11505#ifdef FEAT_CRYPT
11506 "cryptv",
11507#endif
11508#ifdef FEAT_CSCOPE
11509 "cscope",
11510#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011511#ifdef CURSOR_SHAPE
11512 "cursorshape",
11513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514#ifdef DEBUG
11515 "debug",
11516#endif
11517#ifdef FEAT_CON_DIALOG
11518 "dialog_con",
11519#endif
11520#ifdef FEAT_GUI_DIALOG
11521 "dialog_gui",
11522#endif
11523#ifdef FEAT_DIFF
11524 "diff",
11525#endif
11526#ifdef FEAT_DIGRAPHS
11527 "digraphs",
11528#endif
11529#ifdef FEAT_DND
11530 "dnd",
11531#endif
11532#ifdef FEAT_EMACS_TAGS
11533 "emacs_tags",
11534#endif
11535 "eval", /* always present, of course! */
11536#ifdef FEAT_EX_EXTRA
11537 "ex_extra",
11538#endif
11539#ifdef FEAT_SEARCH_EXTRA
11540 "extra_search",
11541#endif
11542#ifdef FEAT_FKMAP
11543 "farsi",
11544#endif
11545#ifdef FEAT_SEARCHPATH
11546 "file_in_path",
11547#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011548#if defined(UNIX) && !defined(USE_SYSTEM)
11549 "filterpipe",
11550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551#ifdef FEAT_FIND_ID
11552 "find_in_path",
11553#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011554#ifdef FEAT_FLOAT
11555 "float",
11556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557#ifdef FEAT_FOLDING
11558 "folding",
11559#endif
11560#ifdef FEAT_FOOTER
11561 "footer",
11562#endif
11563#if !defined(USE_SYSTEM) && defined(UNIX)
11564 "fork",
11565#endif
11566#ifdef FEAT_GETTEXT
11567 "gettext",
11568#endif
11569#ifdef FEAT_GUI
11570 "gui",
11571#endif
11572#ifdef FEAT_GUI_ATHENA
11573# ifdef FEAT_GUI_NEXTAW
11574 "gui_neXtaw",
11575# else
11576 "gui_athena",
11577# endif
11578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579#ifdef FEAT_GUI_GTK
11580 "gui_gtk",
11581# ifdef HAVE_GTK2
11582 "gui_gtk2",
11583# endif
11584#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011585#ifdef FEAT_GUI_GNOME
11586 "gui_gnome",
11587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588#ifdef FEAT_GUI_MAC
11589 "gui_mac",
11590#endif
11591#ifdef FEAT_GUI_MOTIF
11592 "gui_motif",
11593#endif
11594#ifdef FEAT_GUI_PHOTON
11595 "gui_photon",
11596#endif
11597#ifdef FEAT_GUI_W16
11598 "gui_win16",
11599#endif
11600#ifdef FEAT_GUI_W32
11601 "gui_win32",
11602#endif
11603#ifdef FEAT_HANGULIN
11604 "hangul_input",
11605#endif
11606#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11607 "iconv",
11608#endif
11609#ifdef FEAT_INS_EXPAND
11610 "insert_expand",
11611#endif
11612#ifdef FEAT_JUMPLIST
11613 "jumplist",
11614#endif
11615#ifdef FEAT_KEYMAP
11616 "keymap",
11617#endif
11618#ifdef FEAT_LANGMAP
11619 "langmap",
11620#endif
11621#ifdef FEAT_LIBCALL
11622 "libcall",
11623#endif
11624#ifdef FEAT_LINEBREAK
11625 "linebreak",
11626#endif
11627#ifdef FEAT_LISP
11628 "lispindent",
11629#endif
11630#ifdef FEAT_LISTCMDS
11631 "listcmds",
11632#endif
11633#ifdef FEAT_LOCALMAP
11634 "localmap",
11635#endif
11636#ifdef FEAT_MENU
11637 "menu",
11638#endif
11639#ifdef FEAT_SESSION
11640 "mksession",
11641#endif
11642#ifdef FEAT_MODIFY_FNAME
11643 "modify_fname",
11644#endif
11645#ifdef FEAT_MOUSE
11646 "mouse",
11647#endif
11648#ifdef FEAT_MOUSESHAPE
11649 "mouseshape",
11650#endif
11651#if defined(UNIX) || defined(VMS)
11652# ifdef FEAT_MOUSE_DEC
11653 "mouse_dec",
11654# endif
11655# ifdef FEAT_MOUSE_GPM
11656 "mouse_gpm",
11657# endif
11658# ifdef FEAT_MOUSE_JSB
11659 "mouse_jsbterm",
11660# endif
11661# ifdef FEAT_MOUSE_NET
11662 "mouse_netterm",
11663# endif
11664# ifdef FEAT_MOUSE_PTERM
11665 "mouse_pterm",
11666# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011667# ifdef FEAT_SYSMOUSE
11668 "mouse_sysmouse",
11669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670# ifdef FEAT_MOUSE_XTERM
11671 "mouse_xterm",
11672# endif
11673#endif
11674#ifdef FEAT_MBYTE
11675 "multi_byte",
11676#endif
11677#ifdef FEAT_MBYTE_IME
11678 "multi_byte_ime",
11679#endif
11680#ifdef FEAT_MULTI_LANG
11681 "multi_lang",
11682#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011683#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011684#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011685 "mzscheme",
11686#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688#ifdef FEAT_OLE
11689 "ole",
11690#endif
11691#ifdef FEAT_OSFILETYPE
11692 "osfiletype",
11693#endif
11694#ifdef FEAT_PATH_EXTRA
11695 "path_extra",
11696#endif
11697#ifdef FEAT_PERL
11698#ifndef DYNAMIC_PERL
11699 "perl",
11700#endif
11701#endif
11702#ifdef FEAT_PYTHON
11703#ifndef DYNAMIC_PYTHON
11704 "python",
11705#endif
11706#endif
11707#ifdef FEAT_POSTSCRIPT
11708 "postscript",
11709#endif
11710#ifdef FEAT_PRINTER
11711 "printer",
11712#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011713#ifdef FEAT_PROFILE
11714 "profile",
11715#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011716#ifdef FEAT_RELTIME
11717 "reltime",
11718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719#ifdef FEAT_QUICKFIX
11720 "quickfix",
11721#endif
11722#ifdef FEAT_RIGHTLEFT
11723 "rightleft",
11724#endif
11725#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11726 "ruby",
11727#endif
11728#ifdef FEAT_SCROLLBIND
11729 "scrollbind",
11730#endif
11731#ifdef FEAT_CMDL_INFO
11732 "showcmd",
11733 "cmdline_info",
11734#endif
11735#ifdef FEAT_SIGNS
11736 "signs",
11737#endif
11738#ifdef FEAT_SMARTINDENT
11739 "smartindent",
11740#endif
11741#ifdef FEAT_SNIFF
11742 "sniff",
11743#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000011744#ifdef STARTUPTIME
11745 "startuptime",
11746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747#ifdef FEAT_STL_OPT
11748 "statusline",
11749#endif
11750#ifdef FEAT_SUN_WORKSHOP
11751 "sun_workshop",
11752#endif
11753#ifdef FEAT_NETBEANS_INTG
11754 "netbeans_intg",
11755#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000011756#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011757 "spell",
11758#endif
11759#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 "syntax",
11761#endif
11762#if defined(USE_SYSTEM) || !defined(UNIX)
11763 "system",
11764#endif
11765#ifdef FEAT_TAG_BINS
11766 "tag_binary",
11767#endif
11768#ifdef FEAT_TAG_OLDSTATIC
11769 "tag_old_static",
11770#endif
11771#ifdef FEAT_TAG_ANYWHITE
11772 "tag_any_white",
11773#endif
11774#ifdef FEAT_TCL
11775# ifndef DYNAMIC_TCL
11776 "tcl",
11777# endif
11778#endif
11779#ifdef TERMINFO
11780 "terminfo",
11781#endif
11782#ifdef FEAT_TERMRESPONSE
11783 "termresponse",
11784#endif
11785#ifdef FEAT_TEXTOBJ
11786 "textobjects",
11787#endif
11788#ifdef HAVE_TGETENT
11789 "tgetent",
11790#endif
11791#ifdef FEAT_TITLE
11792 "title",
11793#endif
11794#ifdef FEAT_TOOLBAR
11795 "toolbar",
11796#endif
11797#ifdef FEAT_USR_CMDS
11798 "user-commands", /* was accidentally included in 5.4 */
11799 "user_commands",
11800#endif
11801#ifdef FEAT_VIMINFO
11802 "viminfo",
11803#endif
11804#ifdef FEAT_VERTSPLIT
11805 "vertsplit",
11806#endif
11807#ifdef FEAT_VIRTUALEDIT
11808 "virtualedit",
11809#endif
11810#ifdef FEAT_VISUAL
11811 "visual",
11812#endif
11813#ifdef FEAT_VISUALEXTRA
11814 "visualextra",
11815#endif
11816#ifdef FEAT_VREPLACE
11817 "vreplace",
11818#endif
11819#ifdef FEAT_WILDIGN
11820 "wildignore",
11821#endif
11822#ifdef FEAT_WILDMENU
11823 "wildmenu",
11824#endif
11825#ifdef FEAT_WINDOWS
11826 "windows",
11827#endif
11828#ifdef FEAT_WAK
11829 "winaltkeys",
11830#endif
11831#ifdef FEAT_WRITEBACKUP
11832 "writebackup",
11833#endif
11834#ifdef FEAT_XIM
11835 "xim",
11836#endif
11837#ifdef FEAT_XFONTSET
11838 "xfontset",
11839#endif
11840#ifdef USE_XSMP
11841 "xsmp",
11842#endif
11843#ifdef USE_XSMP_INTERACT
11844 "xsmp_interact",
11845#endif
11846#ifdef FEAT_XCLIPBOARD
11847 "xterm_clipboard",
11848#endif
11849#ifdef FEAT_XTERM_SAVE
11850 "xterm_save",
11851#endif
11852#if defined(UNIX) && defined(FEAT_X11)
11853 "X11",
11854#endif
11855 NULL
11856 };
11857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011858 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 for (i = 0; has_list[i] != NULL; ++i)
11860 if (STRICMP(name, has_list[i]) == 0)
11861 {
11862 n = TRUE;
11863 break;
11864 }
11865
11866 if (n == FALSE)
11867 {
11868 if (STRNICMP(name, "patch", 5) == 0)
11869 n = has_patch(atoi((char *)name + 5));
11870 else if (STRICMP(name, "vim_starting") == 0)
11871 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000011872#ifdef FEAT_MBYTE
11873 else if (STRICMP(name, "multi_byte_encoding") == 0)
11874 n = has_mbyte;
11875#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000011876#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11877 else if (STRICMP(name, "balloon_multiline") == 0)
11878 n = multiline_balloon_available();
11879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880#ifdef DYNAMIC_TCL
11881 else if (STRICMP(name, "tcl") == 0)
11882 n = tcl_enabled(FALSE);
11883#endif
11884#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11885 else if (STRICMP(name, "iconv") == 0)
11886 n = iconv_enabled(FALSE);
11887#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011888#ifdef DYNAMIC_MZSCHEME
11889 else if (STRICMP(name, "mzscheme") == 0)
11890 n = mzscheme_enabled(FALSE);
11891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892#ifdef DYNAMIC_RUBY
11893 else if (STRICMP(name, "ruby") == 0)
11894 n = ruby_enabled(FALSE);
11895#endif
11896#ifdef DYNAMIC_PYTHON
11897 else if (STRICMP(name, "python") == 0)
11898 n = python_enabled(FALSE);
11899#endif
11900#ifdef DYNAMIC_PERL
11901 else if (STRICMP(name, "perl") == 0)
11902 n = perl_enabled(FALSE);
11903#endif
11904#ifdef FEAT_GUI
11905 else if (STRICMP(name, "gui_running") == 0)
11906 n = (gui.in_use || gui.starting);
11907# ifdef FEAT_GUI_W32
11908 else if (STRICMP(name, "gui_win32s") == 0)
11909 n = gui_is_win32s();
11910# endif
11911# ifdef FEAT_BROWSE
11912 else if (STRICMP(name, "browse") == 0)
11913 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
11914# endif
11915#endif
11916#ifdef FEAT_SYN_HL
11917 else if (STRICMP(name, "syntax_items") == 0)
11918 n = syntax_present(curbuf);
11919#endif
11920#if defined(WIN3264)
11921 else if (STRICMP(name, "win95") == 0)
11922 n = mch_windows95();
11923#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000011924#ifdef FEAT_NETBEANS_INTG
11925 else if (STRICMP(name, "netbeans_enabled") == 0)
11926 n = usingNetbeans;
11927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 }
11929
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931}
11932
11933/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000011934 * "has_key()" function
11935 */
11936 static void
11937f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011938 typval_T *argvars;
11939 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011940{
Bram Moolenaare9a41262005-01-15 22:18:47 +000011941 if (argvars[0].v_type != VAR_DICT)
11942 {
11943 EMSG(_(e_dictreq));
11944 return;
11945 }
11946 if (argvars[0].vval.v_dict == NULL)
11947 return;
11948
11949 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011950 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011951}
11952
11953/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011954 * "haslocaldir()" function
11955 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011956 static void
11957f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011958 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000011959 typval_T *rettv;
11960{
11961 rettv->vval.v_number = (curwin->w_localdir != NULL);
11962}
11963
11964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 * "hasmapto()" function
11966 */
11967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011969 typval_T *argvars;
11970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971{
11972 char_u *name;
11973 char_u *mode;
11974 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000011975 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011977 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011978 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 mode = (char_u *)"nvo";
11980 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000011981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011982 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000011983 if (argvars[2].v_type != VAR_UNKNOWN)
11984 abbr = get_tv_number(&argvars[2]);
11985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986
Bram Moolenaar2c932302006-03-18 21:42:09 +000011987 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011990 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991}
11992
11993/*
11994 * "histadd()" function
11995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011997f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011998 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000{
12001#ifdef FEAT_CMDHIST
12002 int histype;
12003 char_u *str;
12004 char_u buf[NUMBUFLEN];
12005#endif
12006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 if (check_restricted() || check_secure())
12009 return;
12010#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012011 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12012 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013 if (histype >= 0)
12014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012015 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 if (*str != NUL)
12017 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012018 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012020 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 return;
12022 }
12023 }
12024#endif
12025}
12026
12027/*
12028 * "histdel()" function
12029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012032 typval_T *argvars UNUSED;
12033 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034{
12035#ifdef FEAT_CMDHIST
12036 int n;
12037 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012038 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012040 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12041 if (str == NULL)
12042 n = 0;
12043 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012045 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012046 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012048 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012049 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 else
12051 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012052 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012053 get_tv_string_buf(&argvars[1], buf));
12054 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055#endif
12056}
12057
12058/*
12059 * "histget()" function
12060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012063 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065{
12066#ifdef FEAT_CMDHIST
12067 int type;
12068 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012069 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012071 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12072 if (str == NULL)
12073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012075 {
12076 type = get_histtype(str);
12077 if (argvars[1].v_type == VAR_UNKNOWN)
12078 idx = get_history_idx(type);
12079 else
12080 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12081 /* -1 on type error */
12082 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012085 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088}
12089
12090/*
12091 * "histnr()" function
12092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012094f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012095 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097{
12098 int i;
12099
12100#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012101 char_u *history = get_tv_string_chk(&argvars[0]);
12102
12103 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 if (i >= HIST_CMD && i < HIST_COUNT)
12105 i = get_history_idx(i);
12106 else
12107#endif
12108 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012109 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110}
12111
12112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113 * "highlightID(name)" function
12114 */
12115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012116f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012117 typval_T *argvars;
12118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012120 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121}
12122
12123/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012124 * "highlight_exists()" function
12125 */
12126 static void
12127f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012128 typval_T *argvars;
12129 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130{
12131 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12132}
12133
12134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 * "hostname()" function
12136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012139 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012140 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141{
12142 char_u hostname[256];
12143
12144 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012145 rettv->v_type = VAR_STRING;
12146 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147}
12148
12149/*
12150 * iconv() function
12151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012153f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012154 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156{
12157#ifdef FEAT_MBYTE
12158 char_u buf1[NUMBUFLEN];
12159 char_u buf2[NUMBUFLEN];
12160 char_u *from, *to, *str;
12161 vimconv_T vimconv;
12162#endif
12163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012164 rettv->v_type = VAR_STRING;
12165 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166
12167#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012168 str = get_tv_string(&argvars[0]);
12169 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12170 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 vimconv.vc_type = CONV_NONE;
12172 convert_setup(&vimconv, from, to);
12173
12174 /* If the encodings are equal, no conversion needed. */
12175 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012176 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012178 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179
12180 convert_setup(&vimconv, NULL, NULL);
12181 vim_free(from);
12182 vim_free(to);
12183#endif
12184}
12185
12186/*
12187 * "indent()" function
12188 */
12189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012190f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012191 typval_T *argvars;
12192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193{
12194 linenr_T lnum;
12195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012196 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012198 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012200 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201}
12202
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012203/*
12204 * "index()" function
12205 */
12206 static void
12207f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012208 typval_T *argvars;
12209 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012210{
Bram Moolenaar33570922005-01-25 22:26:29 +000012211 list_T *l;
12212 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012213 long idx = 0;
12214 int ic = FALSE;
12215
12216 rettv->vval.v_number = -1;
12217 if (argvars[0].v_type != VAR_LIST)
12218 {
12219 EMSG(_(e_listreq));
12220 return;
12221 }
12222 l = argvars[0].vval.v_list;
12223 if (l != NULL)
12224 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012225 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012226 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012228 int error = FALSE;
12229
Bram Moolenaar758711c2005-02-02 23:11:38 +000012230 /* Start at specified item. Use the cached index that list_find()
12231 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012232 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012233 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012234 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012235 ic = get_tv_number_chk(&argvars[3], &error);
12236 if (error)
12237 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012238 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012239
Bram Moolenaar758711c2005-02-02 23:11:38 +000012240 for ( ; item != NULL; item = item->li_next, ++idx)
12241 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012242 {
12243 rettv->vval.v_number = idx;
12244 break;
12245 }
12246 }
12247}
12248
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249static int inputsecret_flag = 0;
12250
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012251static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12252
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012254 * This function is used by f_input() and f_inputdialog() functions. The third
12255 * argument to f_input() specifies the type of completion to use at the
12256 * prompt. The third argument to f_inputdialog() specifies the value to return
12257 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258 */
12259 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012260get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012261 typval_T *argvars;
12262 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012263 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012265 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 char_u *p = NULL;
12267 int c;
12268 char_u buf[NUMBUFLEN];
12269 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012270 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012271 int xp_type = EXPAND_NOTHING;
12272 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012274 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012275 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276
12277#ifdef NO_CONSOLE_INPUT
12278 /* While starting up, there is no place to enter text. */
12279 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281#endif
12282
12283 cmd_silent = FALSE; /* Want to see the prompt. */
12284 if (prompt != NULL)
12285 {
12286 /* Only the part of the message after the last NL is considered as
12287 * prompt for the command line */
12288 p = vim_strrchr(prompt, '\n');
12289 if (p == NULL)
12290 p = prompt;
12291 else
12292 {
12293 ++p;
12294 c = *p;
12295 *p = NUL;
12296 msg_start();
12297 msg_clr_eos();
12298 msg_puts_attr(prompt, echo_attr);
12299 msg_didout = FALSE;
12300 msg_starthere();
12301 *p = c;
12302 }
12303 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012305 if (argvars[1].v_type != VAR_UNKNOWN)
12306 {
12307 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12308 if (defstr != NULL)
12309 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012311 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012312 {
12313 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012314 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012315 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012316
Bram Moolenaar4463f292005-09-25 22:20:24 +000012317 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012318
Bram Moolenaar4463f292005-09-25 22:20:24 +000012319 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12320 if (xp_name == NULL)
12321 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012322
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012323 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012324
Bram Moolenaar4463f292005-09-25 22:20:24 +000012325 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12326 &xp_arg) == FAIL)
12327 return;
12328 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012329 }
12330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012331 if (defstr != NULL)
12332 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012333 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12334 xp_type, xp_arg);
12335
12336 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012338 /* since the user typed this, no need to wait for return */
12339 need_wait_return = FALSE;
12340 msg_didout = FALSE;
12341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 cmd_silent = cmd_silent_save;
12343}
12344
12345/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012346 * "input()" function
12347 * Also handles inputsecret() when inputsecret is set.
12348 */
12349 static void
12350f_input(argvars, rettv)
12351 typval_T *argvars;
12352 typval_T *rettv;
12353{
12354 get_user_input(argvars, rettv, FALSE);
12355}
12356
12357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 * "inputdialog()" function
12359 */
12360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012361f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012362 typval_T *argvars;
12363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364{
12365#if defined(FEAT_GUI_TEXTDIALOG)
12366 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12367 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12368 {
12369 char_u *message;
12370 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012371 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012373 message = get_tv_string_chk(&argvars[0]);
12374 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012375 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012376 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 else
12378 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012379 if (message != NULL && defstr != NULL
12380 && do_dialog(VIM_QUESTION, NULL, message,
12381 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012382 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 else
12384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012385 if (message != NULL && defstr != NULL
12386 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012387 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012388 rettv->vval.v_string = vim_strsave(
12389 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012393 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394 }
12395 else
12396#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012397 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398}
12399
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012400/*
12401 * "inputlist()" function
12402 */
12403 static void
12404f_inputlist(argvars, rettv)
12405 typval_T *argvars;
12406 typval_T *rettv;
12407{
12408 listitem_T *li;
12409 int selected;
12410 int mouse_used;
12411
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012412#ifdef NO_CONSOLE_INPUT
12413 /* While starting up, there is no place to enter text. */
12414 if (no_console_input())
12415 return;
12416#endif
12417 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12418 {
12419 EMSG2(_(e_listarg), "inputlist()");
12420 return;
12421 }
12422
12423 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012424 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012425 lines_left = Rows; /* avoid more prompt */
12426 msg_scroll = TRUE;
12427 msg_clr_eos();
12428
12429 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12430 {
12431 msg_puts(get_tv_string(&li->li_tv));
12432 msg_putchar('\n');
12433 }
12434
12435 /* Ask for choice. */
12436 selected = prompt_for_number(&mouse_used);
12437 if (mouse_used)
12438 selected -= lines_left;
12439
12440 rettv->vval.v_number = selected;
12441}
12442
12443
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12445
12446/*
12447 * "inputrestore()" function
12448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012450f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012451 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453{
12454 if (ga_userinput.ga_len > 0)
12455 {
12456 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12458 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012459 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460 }
12461 else if (p_verbose > 1)
12462 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012463 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012464 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465 }
12466}
12467
12468/*
12469 * "inputsave()" function
12470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012472f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012473 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012476 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477 if (ga_grow(&ga_userinput, 1) == OK)
12478 {
12479 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12480 + ga_userinput.ga_len);
12481 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012482 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483 }
12484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012485 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486}
12487
12488/*
12489 * "inputsecret()" function
12490 */
12491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012492f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012493 typval_T *argvars;
12494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495{
12496 ++cmdline_star;
12497 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012498 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499 --cmdline_star;
12500 --inputsecret_flag;
12501}
12502
12503/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012504 * "insert()" function
12505 */
12506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012507f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012508 typval_T *argvars;
12509 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012510{
12511 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012512 listitem_T *item;
12513 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012514 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012515
12516 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012517 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012518 else if ((l = argvars[0].vval.v_list) != NULL
12519 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012520 {
12521 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012522 before = get_tv_number_chk(&argvars[2], &error);
12523 if (error)
12524 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012525
Bram Moolenaar758711c2005-02-02 23:11:38 +000012526 if (before == l->lv_len)
12527 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012528 else
12529 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012530 item = list_find(l, before);
12531 if (item == NULL)
12532 {
12533 EMSGN(_(e_listidx), before);
12534 l = NULL;
12535 }
12536 }
12537 if (l != NULL)
12538 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012539 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012540 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012541 }
12542 }
12543}
12544
12545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546 * "isdirectory()" function
12547 */
12548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012549f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012550 typval_T *argvars;
12551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012553 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554}
12555
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012556/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012557 * "islocked()" function
12558 */
12559 static void
12560f_islocked(argvars, rettv)
12561 typval_T *argvars;
12562 typval_T *rettv;
12563{
12564 lval_T lv;
12565 char_u *end;
12566 dictitem_T *di;
12567
12568 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012569 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12570 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012571 if (end != NULL && lv.ll_name != NULL)
12572 {
12573 if (*end != NUL)
12574 EMSG(_(e_trailing));
12575 else
12576 {
12577 if (lv.ll_tv == NULL)
12578 {
12579 if (check_changedtick(lv.ll_name))
12580 rettv->vval.v_number = 1; /* always locked */
12581 else
12582 {
12583 di = find_var(lv.ll_name, NULL);
12584 if (di != NULL)
12585 {
12586 /* Consider a variable locked when:
12587 * 1. the variable itself is locked
12588 * 2. the value of the variable is locked.
12589 * 3. the List or Dict value is locked.
12590 */
12591 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12592 || tv_islocked(&di->di_tv));
12593 }
12594 }
12595 }
12596 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012597 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012598 else if (lv.ll_newkey != NULL)
12599 EMSG2(_(e_dictkey), lv.ll_newkey);
12600 else if (lv.ll_list != NULL)
12601 /* List item. */
12602 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12603 else
12604 /* Dictionary item. */
12605 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12606 }
12607 }
12608
12609 clear_lval(&lv);
12610}
12611
Bram Moolenaar33570922005-01-25 22:26:29 +000012612static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012613
12614/*
12615 * Turn a dict into a list:
12616 * "what" == 0: list of keys
12617 * "what" == 1: list of values
12618 * "what" == 2: list of items
12619 */
12620 static void
12621dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012622 typval_T *argvars;
12623 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012624 int what;
12625{
Bram Moolenaar33570922005-01-25 22:26:29 +000012626 list_T *l2;
12627 dictitem_T *di;
12628 hashitem_T *hi;
12629 listitem_T *li;
12630 listitem_T *li2;
12631 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012632 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012633
Bram Moolenaar8c711452005-01-14 21:53:12 +000012634 if (argvars[0].v_type != VAR_DICT)
12635 {
12636 EMSG(_(e_dictreq));
12637 return;
12638 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012639 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012640 return;
12641
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012642 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012643 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012644
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012645 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012646 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012647 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012648 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012649 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012650 --todo;
12651 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012652
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012653 li = listitem_alloc();
12654 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012655 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012656 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012657
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012658 if (what == 0)
12659 {
12660 /* keys() */
12661 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012662 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012663 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12664 }
12665 else if (what == 1)
12666 {
12667 /* values() */
12668 copy_tv(&di->di_tv, &li->li_tv);
12669 }
12670 else
12671 {
12672 /* items() */
12673 l2 = list_alloc();
12674 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012675 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012676 li->li_tv.vval.v_list = l2;
12677 if (l2 == NULL)
12678 break;
12679 ++l2->lv_refcount;
12680
12681 li2 = listitem_alloc();
12682 if (li2 == NULL)
12683 break;
12684 list_append(l2, li2);
12685 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012686 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012687 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12688
12689 li2 = listitem_alloc();
12690 if (li2 == NULL)
12691 break;
12692 list_append(l2, li2);
12693 copy_tv(&di->di_tv, &li2->li_tv);
12694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012695 }
12696 }
12697}
12698
12699/*
12700 * "items(dict)" function
12701 */
12702 static void
12703f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012704 typval_T *argvars;
12705 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012706{
12707 dict_list(argvars, rettv, 2);
12708}
12709
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012711 * "join()" function
12712 */
12713 static void
12714f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012715 typval_T *argvars;
12716 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012717{
12718 garray_T ga;
12719 char_u *sep;
12720
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012721 if (argvars[0].v_type != VAR_LIST)
12722 {
12723 EMSG(_(e_listreq));
12724 return;
12725 }
12726 if (argvars[0].vval.v_list == NULL)
12727 return;
12728 if (argvars[1].v_type == VAR_UNKNOWN)
12729 sep = (char_u *)" ";
12730 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012732
12733 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012734
12735 if (sep != NULL)
12736 {
12737 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000012738 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012739 ga_append(&ga, NUL);
12740 rettv->vval.v_string = (char_u *)ga.ga_data;
12741 }
12742 else
12743 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012744}
12745
12746/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012747 * "keys()" function
12748 */
12749 static void
12750f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012751 typval_T *argvars;
12752 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012753{
12754 dict_list(argvars, rettv, 0);
12755}
12756
12757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 * "last_buffer_nr()" function.
12759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012762 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764{
12765 int n = 0;
12766 buf_T *buf;
12767
12768 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
12769 if (n < buf->b_fnum)
12770 n = buf->b_fnum;
12771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012773}
12774
12775/*
12776 * "len()" function
12777 */
12778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012779f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012780 typval_T *argvars;
12781 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012782{
12783 switch (argvars[0].v_type)
12784 {
12785 case VAR_STRING:
12786 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787 rettv->vval.v_number = (varnumber_T)STRLEN(
12788 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012789 break;
12790 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012792 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012793 case VAR_DICT:
12794 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
12795 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012796 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012797 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012798 break;
12799 }
12800}
12801
Bram Moolenaar33570922005-01-25 22:26:29 +000012802static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012803
12804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000012806 typval_T *argvars;
12807 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012808 int type;
12809{
12810#ifdef FEAT_LIBCALL
12811 char_u *string_in;
12812 char_u **string_result;
12813 int nr_result;
12814#endif
12815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012817 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012819
12820 if (check_restricted() || check_secure())
12821 return;
12822
12823#ifdef FEAT_LIBCALL
12824 /* The first two args must be strings, otherwise its meaningless */
12825 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
12826 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012827 string_in = NULL;
12828 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012829 string_in = argvars[2].vval.v_string;
12830 if (type == VAR_NUMBER)
12831 string_result = NULL;
12832 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012834 if (mch_libcall(argvars[0].vval.v_string,
12835 argvars[1].vval.v_string,
12836 string_in,
12837 argvars[2].vval.v_number,
12838 string_result,
12839 &nr_result) == OK
12840 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012842 }
12843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844}
12845
12846/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012847 * "libcall()" function
12848 */
12849 static void
12850f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012851 typval_T *argvars;
12852 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012853{
12854 libcall_common(argvars, rettv, VAR_STRING);
12855}
12856
12857/*
12858 * "libcallnr()" function
12859 */
12860 static void
12861f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012862 typval_T *argvars;
12863 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012864{
12865 libcall_common(argvars, rettv, VAR_NUMBER);
12866}
12867
12868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 * "line(string)" function
12870 */
12871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012873 typval_T *argvars;
12874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875{
12876 linenr_T lnum = 0;
12877 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012878 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012880 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881 if (fp != NULL)
12882 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884}
12885
12886/*
12887 * "line2byte(lnum)" function
12888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012890f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012891 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893{
12894#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012895 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896#else
12897 linenr_T lnum;
12898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012903 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
12904 if (rettv->vval.v_number >= 0)
12905 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906#endif
12907}
12908
12909/*
12910 * "lispindent(lnum)" function
12911 */
12912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012914 typval_T *argvars;
12915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916{
12917#ifdef FEAT_LISP
12918 pos_T pos;
12919 linenr_T lnum;
12920
12921 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12924 {
12925 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012926 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 curwin->w_cursor = pos;
12928 }
12929 else
12930#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932}
12933
12934/*
12935 * "localtime()" function
12936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012939 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943}
12944
Bram Moolenaar33570922005-01-25 22:26:29 +000012945static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946
12947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000012949 typval_T *argvars;
12950 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 int exact;
12952{
12953 char_u *keys;
12954 char_u *which;
12955 char_u buf[NUMBUFLEN];
12956 char_u *keys_buf = NULL;
12957 char_u *rhs;
12958 int mode;
12959 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000012960 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961
12962 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963 rettv->v_type = VAR_STRING;
12964 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012966 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967 if (*keys == NUL)
12968 return;
12969
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012970 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000012971 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012972 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012973 if (argvars[2].v_type != VAR_UNKNOWN)
12974 abbr = get_tv_number(&argvars[2]);
12975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976 else
12977 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012978 if (which == NULL)
12979 return;
12980
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 mode = get_map_mode(&which, 0);
12982
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000012983 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012984 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985 vim_free(keys_buf);
12986 if (rhs != NULL)
12987 {
12988 ga_init(&ga);
12989 ga.ga_itemsize = 1;
12990 ga.ga_growsize = 40;
12991
12992 while (*rhs != NUL)
12993 ga_concat(&ga, str2special(&rhs, FALSE));
12994
12995 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012996 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 }
12998}
12999
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013000#ifdef FEAT_FLOAT
13001/*
13002 * "log10()" function
13003 */
13004 static void
13005f_log10(argvars, rettv)
13006 typval_T *argvars;
13007 typval_T *rettv;
13008{
13009 float_T f;
13010
13011 rettv->v_type = VAR_FLOAT;
13012 if (get_float_arg(argvars, &f) == OK)
13013 rettv->vval.v_float = log10(f);
13014 else
13015 rettv->vval.v_float = 0.0;
13016}
13017#endif
13018
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013020 * "map()" function
13021 */
13022 static void
13023f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013024 typval_T *argvars;
13025 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013026{
13027 filter_map(argvars, rettv, TRUE);
13028}
13029
13030/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013031 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032 */
13033 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013034f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013035 typval_T *argvars;
13036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013038 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039}
13040
13041/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013042 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 */
13044 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013045f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013046 typval_T *argvars;
13047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013049 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050}
13051
Bram Moolenaar33570922005-01-25 22:26:29 +000013052static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053
13054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013055find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013056 typval_T *argvars;
13057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058 int type;
13059{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013060 char_u *str = NULL;
13061 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 char_u *pat;
13063 regmatch_T regmatch;
13064 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013065 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066 char_u *save_cpo;
13067 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013068 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013069 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013070 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013071 list_T *l = NULL;
13072 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013073 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013074 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075
13076 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13077 save_cpo = p_cpo;
13078 p_cpo = (char_u *)"";
13079
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013080 rettv->vval.v_number = -1;
13081 if (type == 3)
13082 {
13083 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013084 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013085 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013086 }
13087 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013089 rettv->v_type = VAR_STRING;
13090 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013093 if (argvars[0].v_type == VAR_LIST)
13094 {
13095 if ((l = argvars[0].vval.v_list) == NULL)
13096 goto theend;
13097 li = l->lv_first;
13098 }
13099 else
13100 expr = str = get_tv_string(&argvars[0]);
13101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013102 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13103 if (pat == NULL)
13104 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013105
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013106 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013108 int error = FALSE;
13109
13110 start = get_tv_number_chk(&argvars[2], &error);
13111 if (error)
13112 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013113 if (l != NULL)
13114 {
13115 li = list_find(l, start);
13116 if (li == NULL)
13117 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013118 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013119 }
13120 else
13121 {
13122 if (start < 0)
13123 start = 0;
13124 if (start > (long)STRLEN(str))
13125 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013126 /* When "count" argument is there ignore matches before "start",
13127 * otherwise skip part of the string. Differs when pattern is "^"
13128 * or "\<". */
13129 if (argvars[3].v_type != VAR_UNKNOWN)
13130 startcol = start;
13131 else
13132 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013133 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013134
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013135 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013136 nth = get_tv_number_chk(&argvars[3], &error);
13137 if (error)
13138 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139 }
13140
13141 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13142 if (regmatch.regprog != NULL)
13143 {
13144 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013145
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013146 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013147 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013148 if (l != NULL)
13149 {
13150 if (li == NULL)
13151 {
13152 match = FALSE;
13153 break;
13154 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013155 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013156 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013157 if (str == NULL)
13158 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013159 }
13160
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013161 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013162
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013163 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013164 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013165 if (l == NULL && !match)
13166 break;
13167
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013168 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013169 if (l != NULL)
13170 {
13171 li = li->li_next;
13172 ++idx;
13173 }
13174 else
13175 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013176#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013177 startcol = (colnr_T)(regmatch.startp[0]
13178 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013179#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013180 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013181#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013182 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013183 }
13184
13185 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013187 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013188 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013189 int i;
13190
13191 /* return list with matched string and submatches */
13192 for (i = 0; i < NSUBEXP; ++i)
13193 {
13194 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013195 {
13196 if (list_append_string(rettv->vval.v_list,
13197 (char_u *)"", 0) == FAIL)
13198 break;
13199 }
13200 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013201 regmatch.startp[i],
13202 (int)(regmatch.endp[i] - regmatch.startp[i]))
13203 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013204 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013205 }
13206 }
13207 else if (type == 2)
13208 {
13209 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013210 if (l != NULL)
13211 copy_tv(&li->li_tv, rettv);
13212 else
13213 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013215 }
13216 else if (l != NULL)
13217 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218 else
13219 {
13220 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013221 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 (varnumber_T)(regmatch.startp[0] - str);
13223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013224 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013226 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 }
13228 }
13229 vim_free(regmatch.regprog);
13230 }
13231
13232theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013233 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234 p_cpo = save_cpo;
13235}
13236
13237/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013238 * "match()" function
13239 */
13240 static void
13241f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013242 typval_T *argvars;
13243 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013244{
13245 find_some_match(argvars, rettv, 1);
13246}
13247
13248/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013249 * "matchadd()" function
13250 */
13251 static void
13252f_matchadd(argvars, rettv)
13253 typval_T *argvars;
13254 typval_T *rettv;
13255{
13256#ifdef FEAT_SEARCH_EXTRA
13257 char_u buf[NUMBUFLEN];
13258 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13259 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13260 int prio = 10; /* default priority */
13261 int id = -1;
13262 int error = FALSE;
13263
13264 rettv->vval.v_number = -1;
13265
13266 if (grp == NULL || pat == NULL)
13267 return;
13268 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013269 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013270 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013271 if (argvars[3].v_type != VAR_UNKNOWN)
13272 id = get_tv_number_chk(&argvars[3], &error);
13273 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013274 if (error == TRUE)
13275 return;
13276 if (id >= 1 && id <= 3)
13277 {
13278 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13279 return;
13280 }
13281
13282 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13283#endif
13284}
13285
13286/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013287 * "matcharg()" function
13288 */
13289 static void
13290f_matcharg(argvars, rettv)
13291 typval_T *argvars;
13292 typval_T *rettv;
13293{
13294 if (rettv_list_alloc(rettv) == OK)
13295 {
13296#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013297 int id = get_tv_number(&argvars[0]);
13298 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013299
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013300 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013301 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013302 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13303 {
13304 list_append_string(rettv->vval.v_list,
13305 syn_id2name(m->hlg_id), -1);
13306 list_append_string(rettv->vval.v_list, m->pattern, -1);
13307 }
13308 else
13309 {
13310 list_append_string(rettv->vval.v_list, NUL, -1);
13311 list_append_string(rettv->vval.v_list, NUL, -1);
13312 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013313 }
13314#endif
13315 }
13316}
13317
13318/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013319 * "matchdelete()" function
13320 */
13321 static void
13322f_matchdelete(argvars, rettv)
13323 typval_T *argvars;
13324 typval_T *rettv;
13325{
13326#ifdef FEAT_SEARCH_EXTRA
13327 rettv->vval.v_number = match_delete(curwin,
13328 (int)get_tv_number(&argvars[0]), TRUE);
13329#endif
13330}
13331
13332/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013333 * "matchend()" function
13334 */
13335 static void
13336f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013337 typval_T *argvars;
13338 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013339{
13340 find_some_match(argvars, rettv, 0);
13341}
13342
13343/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013344 * "matchlist()" function
13345 */
13346 static void
13347f_matchlist(argvars, rettv)
13348 typval_T *argvars;
13349 typval_T *rettv;
13350{
13351 find_some_match(argvars, rettv, 3);
13352}
13353
13354/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013355 * "matchstr()" function
13356 */
13357 static void
13358f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013359 typval_T *argvars;
13360 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013361{
13362 find_some_match(argvars, rettv, 2);
13363}
13364
Bram Moolenaar33570922005-01-25 22:26:29 +000013365static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013366
13367 static void
13368max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013369 typval_T *argvars;
13370 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013371 int domax;
13372{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013373 long n = 0;
13374 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013375 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013376
13377 if (argvars[0].v_type == VAR_LIST)
13378 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 list_T *l;
13380 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013381
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013382 l = argvars[0].vval.v_list;
13383 if (l != NULL)
13384 {
13385 li = l->lv_first;
13386 if (li != NULL)
13387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013388 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013389 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013390 {
13391 li = li->li_next;
13392 if (li == NULL)
13393 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013394 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013395 if (domax ? i > n : i < n)
13396 n = i;
13397 }
13398 }
13399 }
13400 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013401 else if (argvars[0].v_type == VAR_DICT)
13402 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013403 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013404 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013405 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013406 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013407
13408 d = argvars[0].vval.v_dict;
13409 if (d != NULL)
13410 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013411 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013412 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013413 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013414 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013415 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013416 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013417 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013418 if (first)
13419 {
13420 n = i;
13421 first = FALSE;
13422 }
13423 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013424 n = i;
13425 }
13426 }
13427 }
13428 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013429 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013430 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013431 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013432}
13433
13434/*
13435 * "max()" function
13436 */
13437 static void
13438f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013439 typval_T *argvars;
13440 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013441{
13442 max_min(argvars, rettv, TRUE);
13443}
13444
13445/*
13446 * "min()" function
13447 */
13448 static void
13449f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013450 typval_T *argvars;
13451 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013452{
13453 max_min(argvars, rettv, FALSE);
13454}
13455
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013456static int mkdir_recurse __ARGS((char_u *dir, int prot));
13457
13458/*
13459 * Create the directory in which "dir" is located, and higher levels when
13460 * needed.
13461 */
13462 static int
13463mkdir_recurse(dir, prot)
13464 char_u *dir;
13465 int prot;
13466{
13467 char_u *p;
13468 char_u *updir;
13469 int r = FAIL;
13470
13471 /* Get end of directory name in "dir".
13472 * We're done when it's "/" or "c:/". */
13473 p = gettail_sep(dir);
13474 if (p <= get_past_head(dir))
13475 return OK;
13476
13477 /* If the directory exists we're done. Otherwise: create it.*/
13478 updir = vim_strnsave(dir, (int)(p - dir));
13479 if (updir == NULL)
13480 return FAIL;
13481 if (mch_isdir(updir))
13482 r = OK;
13483 else if (mkdir_recurse(updir, prot) == OK)
13484 r = vim_mkdir_emsg(updir, prot);
13485 vim_free(updir);
13486 return r;
13487}
13488
13489#ifdef vim_mkdir
13490/*
13491 * "mkdir()" function
13492 */
13493 static void
13494f_mkdir(argvars, rettv)
13495 typval_T *argvars;
13496 typval_T *rettv;
13497{
13498 char_u *dir;
13499 char_u buf[NUMBUFLEN];
13500 int prot = 0755;
13501
13502 rettv->vval.v_number = FAIL;
13503 if (check_restricted() || check_secure())
13504 return;
13505
13506 dir = get_tv_string_buf(&argvars[0], buf);
13507 if (argvars[1].v_type != VAR_UNKNOWN)
13508 {
13509 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013510 prot = get_tv_number_chk(&argvars[2], NULL);
13511 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013512 mkdir_recurse(dir, prot);
13513 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013514 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013515}
13516#endif
13517
Bram Moolenaar0d660222005-01-07 21:51:51 +000013518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519 * "mode()" function
13520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013522f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013523 typval_T *argvars;
13524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013526 char_u buf[3];
13527
13528 buf[1] = NUL;
13529 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530
13531#ifdef FEAT_VISUAL
13532 if (VIsual_active)
13533 {
13534 if (VIsual_select)
13535 buf[0] = VIsual_mode + 's' - 'v';
13536 else
13537 buf[0] = VIsual_mode;
13538 }
13539 else
13540#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013541 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13542 || State == CONFIRM)
13543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013545 if (State == ASKMORE)
13546 buf[1] = 'm';
13547 else if (State == CONFIRM)
13548 buf[1] = '?';
13549 }
13550 else if (State == EXTERNCMD)
13551 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 else if (State & INSERT)
13553 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013554#ifdef FEAT_VREPLACE
13555 if (State & VREPLACE_FLAG)
13556 {
13557 buf[0] = 'R';
13558 buf[1] = 'v';
13559 }
13560 else
13561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 if (State & REPLACE_FLAG)
13563 buf[0] = 'R';
13564 else
13565 buf[0] = 'i';
13566 }
13567 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013570 if (exmode_active)
13571 buf[1] = 'v';
13572 }
13573 else if (exmode_active)
13574 {
13575 buf[0] = 'c';
13576 buf[1] = 'e';
13577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013579 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013581 if (finish_op)
13582 buf[1] = 'o';
13583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013585 /* Clear out the minor mode when the argument is not a non-zero number or
13586 * non-empty string. */
13587 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013588 buf[1] = NUL;
13589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013590 rettv->vval.v_string = vim_strsave(buf);
13591 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592}
13593
13594/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013595 * "nextnonblank()" function
13596 */
13597 static void
13598f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013599 typval_T *argvars;
13600 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013601{
13602 linenr_T lnum;
13603
13604 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013606 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013607 {
13608 lnum = 0;
13609 break;
13610 }
13611 if (*skipwhite(ml_get(lnum)) != NUL)
13612 break;
13613 }
13614 rettv->vval.v_number = lnum;
13615}
13616
13617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 * "nr2char()" function
13619 */
13620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013621f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013622 typval_T *argvars;
13623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624{
13625 char_u buf[NUMBUFLEN];
13626
13627#ifdef FEAT_MBYTE
13628 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 else
13631#endif
13632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 buf[1] = NUL;
13635 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636 rettv->v_type = VAR_STRING;
13637 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013638}
13639
13640/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013641 * "pathshorten()" function
13642 */
13643 static void
13644f_pathshorten(argvars, rettv)
13645 typval_T *argvars;
13646 typval_T *rettv;
13647{
13648 char_u *p;
13649
13650 rettv->v_type = VAR_STRING;
13651 p = get_tv_string_chk(&argvars[0]);
13652 if (p == NULL)
13653 rettv->vval.v_string = NULL;
13654 else
13655 {
13656 p = vim_strsave(p);
13657 rettv->vval.v_string = p;
13658 if (p != NULL)
13659 shorten_dir(p);
13660 }
13661}
13662
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013663#ifdef FEAT_FLOAT
13664/*
13665 * "pow()" function
13666 */
13667 static void
13668f_pow(argvars, rettv)
13669 typval_T *argvars;
13670 typval_T *rettv;
13671{
13672 float_T fx, fy;
13673
13674 rettv->v_type = VAR_FLOAT;
13675 if (get_float_arg(argvars, &fx) == OK
13676 && get_float_arg(&argvars[1], &fy) == OK)
13677 rettv->vval.v_float = pow(fx, fy);
13678 else
13679 rettv->vval.v_float = 0.0;
13680}
13681#endif
13682
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013683/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013684 * "prevnonblank()" function
13685 */
13686 static void
13687f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013688 typval_T *argvars;
13689 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013690{
13691 linenr_T lnum;
13692
13693 lnum = get_tv_lnum(argvars);
13694 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
13695 lnum = 0;
13696 else
13697 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
13698 --lnum;
13699 rettv->vval.v_number = lnum;
13700}
13701
Bram Moolenaara6c840d2005-08-22 22:59:46 +000013702#ifdef HAVE_STDARG_H
13703/* This dummy va_list is here because:
13704 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13705 * - locally in the function results in a "used before set" warning
13706 * - using va_start() to initialize it gives "function with fixed args" error */
13707static va_list ap;
13708#endif
13709
Bram Moolenaar8c711452005-01-14 21:53:12 +000013710/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013711 * "printf()" function
13712 */
13713 static void
13714f_printf(argvars, rettv)
13715 typval_T *argvars;
13716 typval_T *rettv;
13717{
13718 rettv->v_type = VAR_STRING;
13719 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000013720#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013721 {
13722 char_u buf[NUMBUFLEN];
13723 int len;
13724 char_u *s;
13725 int saved_did_emsg = did_emsg;
13726 char *fmt;
13727
13728 /* Get the required length, allocate the buffer and do it for real. */
13729 did_emsg = FALSE;
13730 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013731 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013732 if (!did_emsg)
13733 {
13734 s = alloc(len + 1);
13735 if (s != NULL)
13736 {
13737 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000013738 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000013739 }
13740 }
13741 did_emsg |= saved_did_emsg;
13742 }
13743#endif
13744}
13745
13746/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013747 * "pumvisible()" function
13748 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013749 static void
13750f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013751 typval_T *argvars UNUSED;
13752 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013753{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000013754#ifdef FEAT_INS_EXPAND
13755 if (pum_visible())
13756 rettv->vval.v_number = 1;
13757#endif
13758}
13759
13760/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013761 * "range()" function
13762 */
13763 static void
13764f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013765 typval_T *argvars;
13766 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013767{
13768 long start;
13769 long end;
13770 long stride = 1;
13771 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013772 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013774 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013775 if (argvars[1].v_type == VAR_UNKNOWN)
13776 {
13777 end = start - 1;
13778 start = 0;
13779 }
13780 else
13781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013782 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013783 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013785 }
13786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013787 if (error)
13788 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000013789 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013790 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000013791 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013792 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013793 else
13794 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013795 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013796 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013797 if (list_append_number(rettv->vval.v_list,
13798 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013799 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013800 }
13801}
13802
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013803/*
13804 * "readfile()" function
13805 */
13806 static void
13807f_readfile(argvars, rettv)
13808 typval_T *argvars;
13809 typval_T *rettv;
13810{
13811 int binary = FALSE;
13812 char_u *fname;
13813 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013814 listitem_T *li;
13815#define FREAD_SIZE 200 /* optimized for text lines */
13816 char_u buf[FREAD_SIZE];
13817 int readlen; /* size of last fread() */
13818 int buflen; /* nr of valid chars in buf[] */
13819 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
13820 int tolist; /* first byte in buf[] still to be put in list */
13821 int chop; /* how many CR to chop off */
13822 char_u *prev = NULL; /* previously read bytes, if any */
13823 int prevlen = 0; /* length of "prev" if not NULL */
13824 char_u *s;
13825 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013826 long maxline = MAXLNUM;
13827 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013828
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013829 if (argvars[1].v_type != VAR_UNKNOWN)
13830 {
13831 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
13832 binary = TRUE;
13833 if (argvars[2].v_type != VAR_UNKNOWN)
13834 maxline = get_tv_number(&argvars[2]);
13835 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013836
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013837 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013838 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013839
13840 /* Always open the file in binary mode, library functions have a mind of
13841 * their own about CR-LF conversion. */
13842 fname = get_tv_string(&argvars[0]);
13843 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
13844 {
13845 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
13846 return;
13847 }
13848
13849 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013850 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013851 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013852 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013853 buflen = filtd + readlen;
13854 tolist = 0;
13855 for ( ; filtd < buflen || readlen <= 0; ++filtd)
13856 {
13857 if (buf[filtd] == '\n' || readlen <= 0)
13858 {
13859 /* Only when in binary mode add an empty list item when the
13860 * last line ends in a '\n'. */
13861 if (!binary && readlen == 0 && filtd == 0)
13862 break;
13863
13864 /* Found end-of-line or end-of-file: add a text line to the
13865 * list. */
13866 chop = 0;
13867 if (!binary)
13868 while (filtd - chop - 1 >= tolist
13869 && buf[filtd - chop - 1] == '\r')
13870 ++chop;
13871 len = filtd - tolist - chop;
13872 if (prev == NULL)
13873 s = vim_strnsave(buf + tolist, len);
13874 else
13875 {
13876 s = alloc((unsigned)(prevlen + len + 1));
13877 if (s != NULL)
13878 {
13879 mch_memmove(s, prev, prevlen);
13880 vim_free(prev);
13881 prev = NULL;
13882 mch_memmove(s + prevlen, buf + tolist, len);
13883 s[prevlen + len] = NUL;
13884 }
13885 }
13886 tolist = filtd + 1;
13887
13888 li = listitem_alloc();
13889 if (li == NULL)
13890 {
13891 vim_free(s);
13892 break;
13893 }
13894 li->li_tv.v_type = VAR_STRING;
13895 li->li_tv.v_lock = 0;
13896 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013897 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013898
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013899 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013900 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013901 if (readlen <= 0)
13902 break;
13903 }
13904 else if (buf[filtd] == NUL)
13905 buf[filtd] = '\n';
13906 }
13907 if (readlen <= 0)
13908 break;
13909
13910 if (tolist == 0)
13911 {
13912 /* "buf" is full, need to move text to an allocated buffer */
13913 if (prev == NULL)
13914 {
13915 prev = vim_strnsave(buf, buflen);
13916 prevlen = buflen;
13917 }
13918 else
13919 {
13920 s = alloc((unsigned)(prevlen + buflen));
13921 if (s != NULL)
13922 {
13923 mch_memmove(s, prev, prevlen);
13924 mch_memmove(s + prevlen, buf, buflen);
13925 vim_free(prev);
13926 prev = s;
13927 prevlen += buflen;
13928 }
13929 }
13930 filtd = 0;
13931 }
13932 else
13933 {
13934 mch_memmove(buf, buf + tolist, buflen - tolist);
13935 filtd -= tolist;
13936 }
13937 }
13938
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013939 /*
13940 * For a negative line count use only the lines at the end of the file,
13941 * free the rest.
13942 */
13943 if (maxline < 0)
13944 while (cnt > -maxline)
13945 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013946 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000013947 --cnt;
13948 }
13949
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013950 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013951 fclose(fd);
13952}
13953
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013954#if defined(FEAT_RELTIME)
13955static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
13956
13957/*
13958 * Convert a List to proftime_T.
13959 * Return FAIL when there is something wrong.
13960 */
13961 static int
13962list2proftime(arg, tm)
13963 typval_T *arg;
13964 proftime_T *tm;
13965{
13966 long n1, n2;
13967 int error = FALSE;
13968
13969 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
13970 || arg->vval.v_list->lv_len != 2)
13971 return FAIL;
13972 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
13973 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
13974# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000013975 tm->HighPart = n1;
13976 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013977# else
13978 tm->tv_sec = n1;
13979 tm->tv_usec = n2;
13980# endif
13981 return error ? FAIL : OK;
13982}
13983#endif /* FEAT_RELTIME */
13984
13985/*
13986 * "reltime()" function
13987 */
13988 static void
13989f_reltime(argvars, rettv)
13990 typval_T *argvars;
13991 typval_T *rettv;
13992{
13993#ifdef FEAT_RELTIME
13994 proftime_T res;
13995 proftime_T start;
13996
13997 if (argvars[0].v_type == VAR_UNKNOWN)
13998 {
13999 /* No arguments: get current time. */
14000 profile_start(&res);
14001 }
14002 else if (argvars[1].v_type == VAR_UNKNOWN)
14003 {
14004 if (list2proftime(&argvars[0], &res) == FAIL)
14005 return;
14006 profile_end(&res);
14007 }
14008 else
14009 {
14010 /* Two arguments: compute the difference. */
14011 if (list2proftime(&argvars[0], &start) == FAIL
14012 || list2proftime(&argvars[1], &res) == FAIL)
14013 return;
14014 profile_sub(&res, &start);
14015 }
14016
14017 if (rettv_list_alloc(rettv) == OK)
14018 {
14019 long n1, n2;
14020
14021# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014022 n1 = res.HighPart;
14023 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014024# else
14025 n1 = res.tv_sec;
14026 n2 = res.tv_usec;
14027# endif
14028 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14029 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14030 }
14031#endif
14032}
14033
14034/*
14035 * "reltimestr()" function
14036 */
14037 static void
14038f_reltimestr(argvars, rettv)
14039 typval_T *argvars;
14040 typval_T *rettv;
14041{
14042#ifdef FEAT_RELTIME
14043 proftime_T tm;
14044#endif
14045
14046 rettv->v_type = VAR_STRING;
14047 rettv->vval.v_string = NULL;
14048#ifdef FEAT_RELTIME
14049 if (list2proftime(&argvars[0], &tm) == OK)
14050 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14051#endif
14052}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014053
Bram Moolenaar0d660222005-01-07 21:51:51 +000014054#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14055static void make_connection __ARGS((void));
14056static int check_connection __ARGS((void));
14057
14058 static void
14059make_connection()
14060{
14061 if (X_DISPLAY == NULL
14062# ifdef FEAT_GUI
14063 && !gui.in_use
14064# endif
14065 )
14066 {
14067 x_force_connect = TRUE;
14068 setup_term_clip();
14069 x_force_connect = FALSE;
14070 }
14071}
14072
14073 static int
14074check_connection()
14075{
14076 make_connection();
14077 if (X_DISPLAY == NULL)
14078 {
14079 EMSG(_("E240: No connection to Vim server"));
14080 return FAIL;
14081 }
14082 return OK;
14083}
14084#endif
14085
14086#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014087static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014088
14089 static void
14090remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014091 typval_T *argvars;
14092 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014093 int expr;
14094{
14095 char_u *server_name;
14096 char_u *keys;
14097 char_u *r = NULL;
14098 char_u buf[NUMBUFLEN];
14099# ifdef WIN32
14100 HWND w;
14101# else
14102 Window w;
14103# endif
14104
14105 if (check_restricted() || check_secure())
14106 return;
14107
14108# ifdef FEAT_X11
14109 if (check_connection() == FAIL)
14110 return;
14111# endif
14112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 server_name = get_tv_string_chk(&argvars[0]);
14114 if (server_name == NULL)
14115 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116 keys = get_tv_string_buf(&argvars[1], buf);
14117# ifdef WIN32
14118 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14119# else
14120 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14121 < 0)
14122# endif
14123 {
14124 if (r != NULL)
14125 EMSG(r); /* sending worked but evaluation failed */
14126 else
14127 EMSG2(_("E241: Unable to send to %s"), server_name);
14128 return;
14129 }
14130
14131 rettv->vval.v_string = r;
14132
14133 if (argvars[2].v_type != VAR_UNKNOWN)
14134 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014135 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014136 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014137 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014138
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014139 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014140 v.di_tv.v_type = VAR_STRING;
14141 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014142 idvar = get_tv_string_chk(&argvars[2]);
14143 if (idvar != NULL)
14144 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014145 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014146 }
14147}
14148#endif
14149
14150/*
14151 * "remote_expr()" function
14152 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014153 static void
14154f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014155 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014156 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014157{
14158 rettv->v_type = VAR_STRING;
14159 rettv->vval.v_string = NULL;
14160#ifdef FEAT_CLIENTSERVER
14161 remote_common(argvars, rettv, TRUE);
14162#endif
14163}
14164
14165/*
14166 * "remote_foreground()" function
14167 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014168 static void
14169f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014170 typval_T *argvars UNUSED;
14171 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014172{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014173#ifdef FEAT_CLIENTSERVER
14174# ifdef WIN32
14175 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014176 {
14177 char_u *server_name = get_tv_string_chk(&argvars[0]);
14178
14179 if (server_name != NULL)
14180 serverForeground(server_name);
14181 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014182# else
14183 /* Send a foreground() expression to the server. */
14184 argvars[1].v_type = VAR_STRING;
14185 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14186 argvars[2].v_type = VAR_UNKNOWN;
14187 remote_common(argvars, rettv, TRUE);
14188 vim_free(argvars[1].vval.v_string);
14189# endif
14190#endif
14191}
14192
Bram Moolenaar0d660222005-01-07 21:51:51 +000014193 static void
14194f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014195 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014196 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014197{
14198#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014199 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014200 char_u *s = NULL;
14201# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014202 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014203# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014204 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014205
14206 if (check_restricted() || check_secure())
14207 {
14208 rettv->vval.v_number = -1;
14209 return;
14210 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014211 serverid = get_tv_string_chk(&argvars[0]);
14212 if (serverid == NULL)
14213 {
14214 rettv->vval.v_number = -1;
14215 return; /* type error; errmsg already given */
14216 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014217# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014218 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014219 if (n == 0)
14220 rettv->vval.v_number = -1;
14221 else
14222 {
14223 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14224 rettv->vval.v_number = (s != NULL);
14225 }
14226# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014227 if (check_connection() == FAIL)
14228 return;
14229
14230 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014232# endif
14233
14234 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236 char_u *retvar;
14237
Bram Moolenaar33570922005-01-25 22:26:29 +000014238 v.di_tv.v_type = VAR_STRING;
14239 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014240 retvar = get_tv_string_chk(&argvars[1]);
14241 if (retvar != NULL)
14242 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014243 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014244 }
14245#else
14246 rettv->vval.v_number = -1;
14247#endif
14248}
14249
Bram Moolenaar0d660222005-01-07 21:51:51 +000014250 static void
14251f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014252 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014253 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014254{
14255 char_u *r = NULL;
14256
14257#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014258 char_u *serverid = get_tv_string_chk(&argvars[0]);
14259
14260 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014261 {
14262# ifdef WIN32
14263 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014264 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014265
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014266 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014267 if (n != 0)
14268 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14269 if (r == NULL)
14270# else
14271 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014272 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014273# endif
14274 EMSG(_("E277: Unable to read a server reply"));
14275 }
14276#endif
14277 rettv->v_type = VAR_STRING;
14278 rettv->vval.v_string = r;
14279}
14280
14281/*
14282 * "remote_send()" function
14283 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014284 static void
14285f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014286 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014287 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014288{
14289 rettv->v_type = VAR_STRING;
14290 rettv->vval.v_string = NULL;
14291#ifdef FEAT_CLIENTSERVER
14292 remote_common(argvars, rettv, FALSE);
14293#endif
14294}
14295
14296/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014297 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014298 */
14299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014300f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014301 typval_T *argvars;
14302 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014303{
Bram Moolenaar33570922005-01-25 22:26:29 +000014304 list_T *l;
14305 listitem_T *item, *item2;
14306 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014307 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014308 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014309 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014310 dict_T *d;
14311 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014312
Bram Moolenaar8c711452005-01-14 21:53:12 +000014313 if (argvars[0].v_type == VAR_DICT)
14314 {
14315 if (argvars[2].v_type != VAR_UNKNOWN)
14316 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014317 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014318 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014320 key = get_tv_string_chk(&argvars[1]);
14321 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014323 di = dict_find(d, key, -1);
14324 if (di == NULL)
14325 EMSG2(_(e_dictkey), key);
14326 else
14327 {
14328 *rettv = di->di_tv;
14329 init_tv(&di->di_tv);
14330 dictitem_remove(d, di);
14331 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014332 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014333 }
14334 }
14335 else if (argvars[0].v_type != VAR_LIST)
14336 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014337 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014338 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014340 int error = FALSE;
14341
14342 idx = get_tv_number_chk(&argvars[1], &error);
14343 if (error)
14344 ; /* type error: do nothing, errmsg already given */
14345 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014346 EMSGN(_(e_listidx), idx);
14347 else
14348 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014349 if (argvars[2].v_type == VAR_UNKNOWN)
14350 {
14351 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014352 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014353 *rettv = item->li_tv;
14354 vim_free(item);
14355 }
14356 else
14357 {
14358 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014359 end = get_tv_number_chk(&argvars[2], &error);
14360 if (error)
14361 ; /* type error: do nothing */
14362 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014363 EMSGN(_(e_listidx), end);
14364 else
14365 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014366 int cnt = 0;
14367
14368 for (li = item; li != NULL; li = li->li_next)
14369 {
14370 ++cnt;
14371 if (li == item2)
14372 break;
14373 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014374 if (li == NULL) /* didn't find "item2" after "item" */
14375 EMSG(_(e_invrange));
14376 else
14377 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014378 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014379 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014380 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014381 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014382 l->lv_first = item;
14383 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014384 item->li_prev = NULL;
14385 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014386 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014387 }
14388 }
14389 }
14390 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014391 }
14392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393}
14394
14395/*
14396 * "rename({from}, {to})" function
14397 */
14398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014400 typval_T *argvars;
14401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402{
14403 char_u buf[NUMBUFLEN];
14404
14405 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014406 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014408 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14409 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410}
14411
14412/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014413 * "repeat()" function
14414 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014415 static void
14416f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014417 typval_T *argvars;
14418 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014419{
14420 char_u *p;
14421 int n;
14422 int slen;
14423 int len;
14424 char_u *r;
14425 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014426
14427 n = get_tv_number(&argvars[1]);
14428 if (argvars[0].v_type == VAR_LIST)
14429 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014430 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014431 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014432 if (list_extend(rettv->vval.v_list,
14433 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014434 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014435 }
14436 else
14437 {
14438 p = get_tv_string(&argvars[0]);
14439 rettv->v_type = VAR_STRING;
14440 rettv->vval.v_string = NULL;
14441
14442 slen = (int)STRLEN(p);
14443 len = slen * n;
14444 if (len <= 0)
14445 return;
14446
14447 r = alloc(len + 1);
14448 if (r != NULL)
14449 {
14450 for (i = 0; i < n; i++)
14451 mch_memmove(r + i * slen, p, (size_t)slen);
14452 r[len] = NUL;
14453 }
14454
14455 rettv->vval.v_string = r;
14456 }
14457}
14458
14459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460 * "resolve()" function
14461 */
14462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014463f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014464 typval_T *argvars;
14465 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466{
14467 char_u *p;
14468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014469 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470#ifdef FEAT_SHORTCUT
14471 {
14472 char_u *v = NULL;
14473
14474 v = mch_resolve_shortcut(p);
14475 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014476 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014478 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 }
14480#else
14481# ifdef HAVE_READLINK
14482 {
14483 char_u buf[MAXPATHL + 1];
14484 char_u *cpy;
14485 int len;
14486 char_u *remain = NULL;
14487 char_u *q;
14488 int is_relative_to_current = FALSE;
14489 int has_trailing_pathsep = FALSE;
14490 int limit = 100;
14491
14492 p = vim_strsave(p);
14493
14494 if (p[0] == '.' && (vim_ispathsep(p[1])
14495 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14496 is_relative_to_current = TRUE;
14497
14498 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014499 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500 has_trailing_pathsep = TRUE;
14501
14502 q = getnextcomp(p);
14503 if (*q != NUL)
14504 {
14505 /* Separate the first path component in "p", and keep the
14506 * remainder (beginning with the path separator). */
14507 remain = vim_strsave(q - 1);
14508 q[-1] = NUL;
14509 }
14510
14511 for (;;)
14512 {
14513 for (;;)
14514 {
14515 len = readlink((char *)p, (char *)buf, MAXPATHL);
14516 if (len <= 0)
14517 break;
14518 buf[len] = NUL;
14519
14520 if (limit-- == 0)
14521 {
14522 vim_free(p);
14523 vim_free(remain);
14524 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014525 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526 goto fail;
14527 }
14528
14529 /* Ensure that the result will have a trailing path separator
14530 * if the argument has one. */
14531 if (remain == NULL && has_trailing_pathsep)
14532 add_pathsep(buf);
14533
14534 /* Separate the first path component in the link value and
14535 * concatenate the remainders. */
14536 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14537 if (*q != NUL)
14538 {
14539 if (remain == NULL)
14540 remain = vim_strsave(q - 1);
14541 else
14542 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014543 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 if (cpy != NULL)
14545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 vim_free(remain);
14547 remain = cpy;
14548 }
14549 }
14550 q[-1] = NUL;
14551 }
14552
14553 q = gettail(p);
14554 if (q > p && *q == NUL)
14555 {
14556 /* Ignore trailing path separator. */
14557 q[-1] = NUL;
14558 q = gettail(p);
14559 }
14560 if (q > p && !mch_isFullName(buf))
14561 {
14562 /* symlink is relative to directory of argument */
14563 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14564 if (cpy != NULL)
14565 {
14566 STRCPY(cpy, p);
14567 STRCPY(gettail(cpy), buf);
14568 vim_free(p);
14569 p = cpy;
14570 }
14571 }
14572 else
14573 {
14574 vim_free(p);
14575 p = vim_strsave(buf);
14576 }
14577 }
14578
14579 if (remain == NULL)
14580 break;
14581
14582 /* Append the first path component of "remain" to "p". */
14583 q = getnextcomp(remain + 1);
14584 len = q - remain - (*q != NUL);
14585 cpy = vim_strnsave(p, STRLEN(p) + len);
14586 if (cpy != NULL)
14587 {
14588 STRNCAT(cpy, remain, len);
14589 vim_free(p);
14590 p = cpy;
14591 }
14592 /* Shorten "remain". */
14593 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014594 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 else
14596 {
14597 vim_free(remain);
14598 remain = NULL;
14599 }
14600 }
14601
14602 /* If the result is a relative path name, make it explicitly relative to
14603 * the current directory if and only if the argument had this form. */
14604 if (!vim_ispathsep(*p))
14605 {
14606 if (is_relative_to_current
14607 && *p != NUL
14608 && !(p[0] == '.'
14609 && (p[1] == NUL
14610 || vim_ispathsep(p[1])
14611 || (p[1] == '.'
14612 && (p[2] == NUL
14613 || vim_ispathsep(p[2]))))))
14614 {
14615 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014616 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617 if (cpy != NULL)
14618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 vim_free(p);
14620 p = cpy;
14621 }
14622 }
14623 else if (!is_relative_to_current)
14624 {
14625 /* Strip leading "./". */
14626 q = p;
14627 while (q[0] == '.' && vim_ispathsep(q[1]))
14628 q += 2;
14629 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014630 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631 }
14632 }
14633
14634 /* Ensure that the result will have no trailing path separator
14635 * if the argument had none. But keep "/" or "//". */
14636 if (!has_trailing_pathsep)
14637 {
14638 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014639 if (after_pathsep(p, q))
14640 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641 }
14642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644 }
14645# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014646 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647# endif
14648#endif
14649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014650 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651
14652#ifdef HAVE_READLINK
14653fail:
14654#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014655 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656}
14657
14658/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014659 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660 */
14661 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014662f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014663 typval_T *argvars;
14664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665{
Bram Moolenaar33570922005-01-25 22:26:29 +000014666 list_T *l;
14667 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668
Bram Moolenaar0d660222005-01-07 21:51:51 +000014669 if (argvars[0].v_type != VAR_LIST)
14670 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014671 else if ((l = argvars[0].vval.v_list) != NULL
14672 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014673 {
14674 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014675 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014676 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014677 while (li != NULL)
14678 {
14679 ni = li->li_prev;
14680 list_append(l, li);
14681 li = ni;
14682 }
14683 rettv->vval.v_list = l;
14684 rettv->v_type = VAR_LIST;
14685 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000014686 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688}
14689
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014690#define SP_NOMOVE 0x01 /* don't move cursor */
14691#define SP_REPEAT 0x02 /* repeat to find outer pair */
14692#define SP_RETCOUNT 0x04 /* return matchcount */
14693#define SP_SETPCMARK 0x08 /* set previous context mark */
14694#define SP_START 0x10 /* accept match at start position */
14695#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14696#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014697
Bram Moolenaar33570922005-01-25 22:26:29 +000014698static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014699
14700/*
14701 * Get flags for a search function.
14702 * Possibly sets "p_ws".
14703 * Returns BACKWARD, FORWARD or zero (for an error).
14704 */
14705 static int
14706get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014707 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014708 int *flagsp;
14709{
14710 int dir = FORWARD;
14711 char_u *flags;
14712 char_u nbuf[NUMBUFLEN];
14713 int mask;
14714
14715 if (varp->v_type != VAR_UNKNOWN)
14716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014717 flags = get_tv_string_buf_chk(varp, nbuf);
14718 if (flags == NULL)
14719 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014720 while (*flags != NUL)
14721 {
14722 switch (*flags)
14723 {
14724 case 'b': dir = BACKWARD; break;
14725 case 'w': p_ws = TRUE; break;
14726 case 'W': p_ws = FALSE; break;
14727 default: mask = 0;
14728 if (flagsp != NULL)
14729 switch (*flags)
14730 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014731 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014732 case 'e': mask = SP_END; break;
14733 case 'm': mask = SP_RETCOUNT; break;
14734 case 'n': mask = SP_NOMOVE; break;
14735 case 'p': mask = SP_SUBPAT; break;
14736 case 'r': mask = SP_REPEAT; break;
14737 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014738 }
14739 if (mask == 0)
14740 {
14741 EMSG2(_(e_invarg2), flags);
14742 dir = 0;
14743 }
14744 else
14745 *flagsp |= mask;
14746 }
14747 if (dir == 0)
14748 break;
14749 ++flags;
14750 }
14751 }
14752 return dir;
14753}
14754
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014756 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014758 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014759search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014760 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014761 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014762 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014764 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 char_u *pat;
14766 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014767 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768 int save_p_ws = p_ws;
14769 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014770 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014771 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014772 proftime_T tm;
14773#ifdef FEAT_RELTIME
14774 long time_limit = 0;
14775#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014776 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014777 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014779 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014780 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014781 if (dir == 0)
14782 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014783 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014784 if (flags & SP_START)
14785 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014786 if (flags & SP_END)
14787 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014788
Bram Moolenaar76929292008-01-06 19:07:36 +000014789 /* Optional arguments: line number to stop searching and timeout. */
14790 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014791 {
14792 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
14793 if (lnum_stop < 0)
14794 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014795#ifdef FEAT_RELTIME
14796 if (argvars[3].v_type != VAR_UNKNOWN)
14797 {
14798 time_limit = get_tv_number_chk(&argvars[3], NULL);
14799 if (time_limit < 0)
14800 goto theend;
14801 }
14802#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014803 }
14804
Bram Moolenaar76929292008-01-06 19:07:36 +000014805#ifdef FEAT_RELTIME
14806 /* Set the time limit, if there is one. */
14807 profile_setlimit(time_limit, &tm);
14808#endif
14809
Bram Moolenaar231334e2005-07-25 20:46:57 +000014810 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014811 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014812 * Check to make sure only those flags are set.
14813 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14814 * flags cannot be set. Check for that condition also.
14815 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014816 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014817 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014818 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014819 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014820 goto theend;
14821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014823 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014824 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000014825 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014826 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014828 if (flags & SP_SUBPAT)
14829 retval = subpatnum;
14830 else
14831 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000014832 if (flags & SP_SETPCMARK)
14833 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014835 if (match_pos != NULL)
14836 {
14837 /* Store the match cursor position */
14838 match_pos->lnum = pos.lnum;
14839 match_pos->col = pos.col + 1;
14840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 /* "/$" will put the cursor after the end of the line, may need to
14842 * correct that here */
14843 check_cursor();
14844 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014845
14846 /* If 'n' flag is used: restore cursor position. */
14847 if (flags & SP_NOMOVE)
14848 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000014849 else
14850 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014851theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014853
14854 return retval;
14855}
14856
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014857#ifdef FEAT_FLOAT
14858/*
14859 * "round({float})" function
14860 */
14861 static void
14862f_round(argvars, rettv)
14863 typval_T *argvars;
14864 typval_T *rettv;
14865{
14866 float_T f;
14867
14868 rettv->v_type = VAR_FLOAT;
14869 if (get_float_arg(argvars, &f) == OK)
14870 /* round() is not in C90, use ceil() or floor() instead. */
14871 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
14872 else
14873 rettv->vval.v_float = 0.0;
14874}
14875#endif
14876
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014877/*
14878 * "search()" function
14879 */
14880 static void
14881f_search(argvars, rettv)
14882 typval_T *argvars;
14883 typval_T *rettv;
14884{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014885 int flags = 0;
14886
14887 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888}
14889
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014891 * "searchdecl()" function
14892 */
14893 static void
14894f_searchdecl(argvars, rettv)
14895 typval_T *argvars;
14896 typval_T *rettv;
14897{
14898 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014899 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014900 int error = FALSE;
14901 char_u *name;
14902
14903 rettv->vval.v_number = 1; /* default: FAIL */
14904
14905 name = get_tv_string_chk(&argvars[0]);
14906 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000014907 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014908 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000014909 if (!error && argvars[2].v_type != VAR_UNKNOWN)
14910 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
14911 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014912 if (!error && name != NULL)
14913 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000014914 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000014915}
14916
14917/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014918 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014920 static int
14921searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000014922 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014923 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924{
14925 char_u *spat, *mpat, *epat;
14926 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928 int dir;
14929 int flags = 0;
14930 char_u nbuf1[NUMBUFLEN];
14931 char_u nbuf2[NUMBUFLEN];
14932 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014933 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014934 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000014935 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014938 spat = get_tv_string_chk(&argvars[0]);
14939 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
14940 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
14941 if (spat == NULL || mpat == NULL || epat == NULL)
14942 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944 /* Handle the optional fourth argument: flags */
14945 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014946 if (dir == 0)
14947 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014948
14949 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000014950 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14951 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014952 if ((flags & (SP_END | SP_SUBPAT)) != 0
14953 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000014954 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000014955 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000014956 goto theend;
14957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958
Bram Moolenaar92de73d2008-01-22 10:59:38 +000014959 /* Using 'r' implies 'W', otherwise it doesn't work. */
14960 if (flags & SP_REPEAT)
14961 p_ws = FALSE;
14962
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014963 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014964 if (argvars[3].v_type == VAR_UNKNOWN
14965 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 skip = (char_u *)"";
14967 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014969 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014970 if (argvars[5].v_type != VAR_UNKNOWN)
14971 {
14972 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
14973 if (lnum_stop < 0)
14974 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000014975#ifdef FEAT_RELTIME
14976 if (argvars[6].v_type != VAR_UNKNOWN)
14977 {
14978 time_limit = get_tv_number_chk(&argvars[6], NULL);
14979 if (time_limit < 0)
14980 goto theend;
14981 }
14982#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014983 }
14984 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014985 if (skip == NULL)
14986 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014988 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000014989 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000014990
14991theend:
14992 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014993
14994 return retval;
14995}
14996
14997/*
14998 * "searchpair()" function
14999 */
15000 static void
15001f_searchpair(argvars, rettv)
15002 typval_T *argvars;
15003 typval_T *rettv;
15004{
15005 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15006}
15007
15008/*
15009 * "searchpairpos()" function
15010 */
15011 static void
15012f_searchpairpos(argvars, rettv)
15013 typval_T *argvars;
15014 typval_T *rettv;
15015{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015016 pos_T match_pos;
15017 int lnum = 0;
15018 int col = 0;
15019
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015020 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015021 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015022
15023 if (searchpair_cmn(argvars, &match_pos) > 0)
15024 {
15025 lnum = match_pos.lnum;
15026 col = match_pos.col;
15027 }
15028
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015029 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15030 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015031}
15032
15033/*
15034 * Search for a start/middle/end thing.
15035 * Used by searchpair(), see its documentation for the details.
15036 * Returns 0 or -1 for no match,
15037 */
15038 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015039do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15040 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015041 char_u *spat; /* start pattern */
15042 char_u *mpat; /* middle pattern */
15043 char_u *epat; /* end pattern */
15044 int dir; /* BACKWARD or FORWARD */
15045 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015046 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015047 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015048 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015049 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015050{
15051 char_u *save_cpo;
15052 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15053 long retval = 0;
15054 pos_T pos;
15055 pos_T firstpos;
15056 pos_T foundpos;
15057 pos_T save_cursor;
15058 pos_T save_pos;
15059 int n;
15060 int r;
15061 int nest = 1;
15062 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015063 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015064 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015065
15066 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15067 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015068 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015069
Bram Moolenaar76929292008-01-06 19:07:36 +000015070#ifdef FEAT_RELTIME
15071 /* Set the time limit, if there is one. */
15072 profile_setlimit(time_limit, &tm);
15073#endif
15074
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015075 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15076 * start/middle/end (pat3, for the top pair). */
15077 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15078 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15079 if (pat2 == NULL || pat3 == NULL)
15080 goto theend;
15081 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15082 if (*mpat == NUL)
15083 STRCPY(pat3, pat2);
15084 else
15085 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15086 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015087 if (flags & SP_START)
15088 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015089
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090 save_cursor = curwin->w_cursor;
15091 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015092 clearpos(&firstpos);
15093 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094 pat = pat3;
15095 for (;;)
15096 {
15097 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015098 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015099 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15100 /* didn't find it or found the first match again: FAIL */
15101 break;
15102
15103 if (firstpos.lnum == 0)
15104 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015105 if (equalpos(pos, foundpos))
15106 {
15107 /* Found the same position again. Can happen with a pattern that
15108 * has "\zs" at the end and searching backwards. Advance one
15109 * character and try again. */
15110 if (dir == BACKWARD)
15111 decl(&pos);
15112 else
15113 incl(&pos);
15114 }
15115 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015117 /* clear the start flag to avoid getting stuck here */
15118 options &= ~SEARCH_START;
15119
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120 /* If the skip pattern matches, ignore this match. */
15121 if (*skip != NUL)
15122 {
15123 save_pos = curwin->w_cursor;
15124 curwin->w_cursor = pos;
15125 r = eval_to_bool(skip, &err, NULL, FALSE);
15126 curwin->w_cursor = save_pos;
15127 if (err)
15128 {
15129 /* Evaluating {skip} caused an error, break here. */
15130 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015131 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132 break;
15133 }
15134 if (r)
15135 continue;
15136 }
15137
15138 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15139 {
15140 /* Found end when searching backwards or start when searching
15141 * forward: nested pair. */
15142 ++nest;
15143 pat = pat2; /* nested, don't search for middle */
15144 }
15145 else
15146 {
15147 /* Found end when searching forward or start when searching
15148 * backward: end of (nested) pair; or found middle in outer pair. */
15149 if (--nest == 1)
15150 pat = pat3; /* outer level, search for middle */
15151 }
15152
15153 if (nest == 0)
15154 {
15155 /* Found the match: return matchcount or line number. */
15156 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015157 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015159 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015160 if (flags & SP_SETPCMARK)
15161 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162 curwin->w_cursor = pos;
15163 if (!(flags & SP_REPEAT))
15164 break;
15165 nest = 1; /* search for next unmatched */
15166 }
15167 }
15168
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015169 if (match_pos != NULL)
15170 {
15171 /* Store the match cursor position */
15172 match_pos->lnum = curwin->w_cursor.lnum;
15173 match_pos->col = curwin->w_cursor.col + 1;
15174 }
15175
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015177 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015178 curwin->w_cursor = save_cursor;
15179
15180theend:
15181 vim_free(pat2);
15182 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015183 if (p_cpo == empty_option)
15184 p_cpo = save_cpo;
15185 else
15186 /* Darn, evaluating the {skip} expression changed the value. */
15187 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015188
15189 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190}
15191
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015192/*
15193 * "searchpos()" function
15194 */
15195 static void
15196f_searchpos(argvars, rettv)
15197 typval_T *argvars;
15198 typval_T *rettv;
15199{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015200 pos_T match_pos;
15201 int lnum = 0;
15202 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015203 int n;
15204 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015205
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015206 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015207 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015208
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015209 n = search_cmn(argvars, &match_pos, &flags);
15210 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015211 {
15212 lnum = match_pos.lnum;
15213 col = match_pos.col;
15214 }
15215
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015216 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15217 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015218 if (flags & SP_SUBPAT)
15219 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015220}
15221
15222
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223 static void
15224f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015225 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015227{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015228#ifdef FEAT_CLIENTSERVER
15229 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015230 char_u *server = get_tv_string_chk(&argvars[0]);
15231 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232
Bram Moolenaar0d660222005-01-07 21:51:51 +000015233 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015234 if (server == NULL || reply == NULL)
15235 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015236 if (check_restricted() || check_secure())
15237 return;
15238# ifdef FEAT_X11
15239 if (check_connection() == FAIL)
15240 return;
15241# endif
15242
15243 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015245 EMSG(_("E258: Unable to send to client"));
15246 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015248 rettv->vval.v_number = 0;
15249#else
15250 rettv->vval.v_number = -1;
15251#endif
15252}
15253
Bram Moolenaar0d660222005-01-07 21:51:51 +000015254 static void
15255f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015256 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015257 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015258{
15259 char_u *r = NULL;
15260
15261#ifdef FEAT_CLIENTSERVER
15262# ifdef WIN32
15263 r = serverGetVimNames();
15264# else
15265 make_connection();
15266 if (X_DISPLAY != NULL)
15267 r = serverGetVimNames(X_DISPLAY);
15268# endif
15269#endif
15270 rettv->v_type = VAR_STRING;
15271 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272}
15273
15274/*
15275 * "setbufvar()" function
15276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015278f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015279 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015280 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281{
15282 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015285 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286 char_u nbuf[NUMBUFLEN];
15287
15288 if (check_restricted() || check_secure())
15289 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015290 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15291 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015292 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293 varp = &argvars[2];
15294
15295 if (buf != NULL && varname != NULL && varp != NULL)
15296 {
15297 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015299
15300 if (*varname == '&')
15301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015302 long numval;
15303 char_u *strval;
15304 int error = FALSE;
15305
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015307 numval = get_tv_number_chk(varp, &error);
15308 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015309 if (!error && strval != NULL)
15310 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311 }
15312 else
15313 {
15314 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15315 if (bufvarname != NULL)
15316 {
15317 STRCPY(bufvarname, "b:");
15318 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015319 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320 vim_free(bufvarname);
15321 }
15322 }
15323
15324 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327}
15328
15329/*
15330 * "setcmdpos()" function
15331 */
15332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015333f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015334 typval_T *argvars;
15335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015337 int pos = (int)get_tv_number(&argvars[0]) - 1;
15338
15339 if (pos >= 0)
15340 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341}
15342
15343/*
15344 * "setline()" function
15345 */
15346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015347f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015348 typval_T *argvars;
15349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350{
15351 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015352 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015353 list_T *l = NULL;
15354 listitem_T *li = NULL;
15355 long added = 0;
15356 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015358 lnum = get_tv_lnum(&argvars[0]);
15359 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015361 l = argvars[1].vval.v_list;
15362 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015364 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015365 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015366
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015367 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015368 for (;;)
15369 {
15370 if (l != NULL)
15371 {
15372 /* list argument, get next string */
15373 if (li == NULL)
15374 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015375 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015376 li = li->li_next;
15377 }
15378
15379 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015380 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015381 break;
15382 if (lnum <= curbuf->b_ml.ml_line_count)
15383 {
15384 /* existing line, replace it */
15385 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15386 {
15387 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015388 if (lnum == curwin->w_cursor.lnum)
15389 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015390 rettv->vval.v_number = 0; /* OK */
15391 }
15392 }
15393 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15394 {
15395 /* lnum is one past the last line, append the line */
15396 ++added;
15397 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15398 rettv->vval.v_number = 0; /* OK */
15399 }
15400
15401 if (l == NULL) /* only one string argument */
15402 break;
15403 ++lnum;
15404 }
15405
15406 if (added > 0)
15407 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408}
15409
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015410static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15411
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015413 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015414 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015415 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015416set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015417 win_T *wp UNUSED;
15418 typval_T *list_arg UNUSED;
15419 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015420 typval_T *rettv;
15421{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015422#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015423 char_u *act;
15424 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015425#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015426
Bram Moolenaar2641f772005-03-25 21:58:17 +000015427 rettv->vval.v_number = -1;
15428
15429#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015430 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015431 EMSG(_(e_listreq));
15432 else
15433 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015434 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015435
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015436 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015437 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015438 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015439 if (act == NULL)
15440 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015441 if (*act == 'a' || *act == 'r')
15442 action = *act;
15443 }
15444
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015445 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015446 rettv->vval.v_number = 0;
15447 }
15448#endif
15449}
15450
15451/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015452 * "setloclist()" function
15453 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015454 static void
15455f_setloclist(argvars, rettv)
15456 typval_T *argvars;
15457 typval_T *rettv;
15458{
15459 win_T *win;
15460
15461 rettv->vval.v_number = -1;
15462
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015463 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015464 if (win != NULL)
15465 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15466}
15467
15468/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015469 * "setmatches()" function
15470 */
15471 static void
15472f_setmatches(argvars, rettv)
15473 typval_T *argvars;
15474 typval_T *rettv;
15475{
15476#ifdef FEAT_SEARCH_EXTRA
15477 list_T *l;
15478 listitem_T *li;
15479 dict_T *d;
15480
15481 rettv->vval.v_number = -1;
15482 if (argvars[0].v_type != VAR_LIST)
15483 {
15484 EMSG(_(e_listreq));
15485 return;
15486 }
15487 if ((l = argvars[0].vval.v_list) != NULL)
15488 {
15489
15490 /* To some extent make sure that we are dealing with a list from
15491 * "getmatches()". */
15492 li = l->lv_first;
15493 while (li != NULL)
15494 {
15495 if (li->li_tv.v_type != VAR_DICT
15496 || (d = li->li_tv.vval.v_dict) == NULL)
15497 {
15498 EMSG(_(e_invarg));
15499 return;
15500 }
15501 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15502 && dict_find(d, (char_u *)"pattern", -1) != NULL
15503 && dict_find(d, (char_u *)"priority", -1) != NULL
15504 && dict_find(d, (char_u *)"id", -1) != NULL))
15505 {
15506 EMSG(_(e_invarg));
15507 return;
15508 }
15509 li = li->li_next;
15510 }
15511
15512 clear_matches(curwin);
15513 li = l->lv_first;
15514 while (li != NULL)
15515 {
15516 d = li->li_tv.vval.v_dict;
15517 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15518 get_dict_string(d, (char_u *)"pattern", FALSE),
15519 (int)get_dict_number(d, (char_u *)"priority"),
15520 (int)get_dict_number(d, (char_u *)"id"));
15521 li = li->li_next;
15522 }
15523 rettv->vval.v_number = 0;
15524 }
15525#endif
15526}
15527
15528/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015529 * "setpos()" function
15530 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015531 static void
15532f_setpos(argvars, rettv)
15533 typval_T *argvars;
15534 typval_T *rettv;
15535{
15536 pos_T pos;
15537 int fnum;
15538 char_u *name;
15539
Bram Moolenaar08250432008-02-13 11:42:46 +000015540 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015541 name = get_tv_string_chk(argvars);
15542 if (name != NULL)
15543 {
15544 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15545 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015546 if (--pos.col < 0)
15547 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015548 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015549 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015550 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015551 if (fnum == curbuf->b_fnum)
15552 {
15553 curwin->w_cursor = pos;
15554 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015555 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015556 }
15557 else
15558 EMSG(_(e_invarg));
15559 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015560 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15561 {
15562 /* set mark */
15563 if (setmark_pos(name[1], &pos, fnum) == OK)
15564 rettv->vval.v_number = 0;
15565 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015566 else
15567 EMSG(_(e_invarg));
15568 }
15569 }
15570}
15571
15572/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015573 * "setqflist()" function
15574 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015575 static void
15576f_setqflist(argvars, rettv)
15577 typval_T *argvars;
15578 typval_T *rettv;
15579{
15580 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15581}
15582
15583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 * "setreg()" function
15585 */
15586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015587f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015588 typval_T *argvars;
15589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590{
15591 int regname;
15592 char_u *strregname;
15593 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015594 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595 int append;
15596 char_u yank_type;
15597 long block_len;
15598
15599 block_len = -1;
15600 yank_type = MAUTO;
15601 append = FALSE;
15602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015603 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015604 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015606 if (strregname == NULL)
15607 return; /* type error; errmsg already given */
15608 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609 if (regname == 0 || regname == '@')
15610 regname = '"';
15611 else if (regname == '=')
15612 return;
15613
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015614 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015616 stropt = get_tv_string_chk(&argvars[2]);
15617 if (stropt == NULL)
15618 return; /* type error */
15619 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 switch (*stropt)
15621 {
15622 case 'a': case 'A': /* append */
15623 append = TRUE;
15624 break;
15625 case 'v': case 'c': /* character-wise selection */
15626 yank_type = MCHAR;
15627 break;
15628 case 'V': case 'l': /* line-wise selection */
15629 yank_type = MLINE;
15630 break;
15631#ifdef FEAT_VISUAL
15632 case 'b': case Ctrl_V: /* block-wise selection */
15633 yank_type = MBLOCK;
15634 if (VIM_ISDIGIT(stropt[1]))
15635 {
15636 ++stropt;
15637 block_len = getdigits(&stropt) - 1;
15638 --stropt;
15639 }
15640 break;
15641#endif
15642 }
15643 }
15644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015645 strval = get_tv_string_chk(&argvars[1]);
15646 if (strval != NULL)
15647 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015649 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650}
15651
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015652/*
15653 * "settabwinvar()" function
15654 */
15655 static void
15656f_settabwinvar(argvars, rettv)
15657 typval_T *argvars;
15658 typval_T *rettv;
15659{
15660 setwinvar(argvars, rettv, 1);
15661}
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662
15663/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015664 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015667f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015668 typval_T *argvars;
15669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015671 setwinvar(argvars, rettv, 0);
15672}
15673
15674/*
15675 * "setwinvar()" and "settabwinvar()" functions
15676 */
15677 static void
15678setwinvar(argvars, rettv, off)
15679 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015680 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015681 int off;
15682{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683 win_T *win;
15684#ifdef FEAT_WINDOWS
15685 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015686 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687#endif
15688 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015689 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015691 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692
15693 if (check_restricted() || check_secure())
15694 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015695
15696#ifdef FEAT_WINDOWS
15697 if (off == 1)
15698 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15699 else
15700 tp = curtab;
15701#endif
15702 win = find_win_by_nr(&argvars[off], tp);
15703 varname = get_tv_string_chk(&argvars[off + 1]);
15704 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705
15706 if (win != NULL && varname != NULL && varp != NULL)
15707 {
15708#ifdef FEAT_WINDOWS
15709 /* set curwin to be our win, temporarily */
15710 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015711 save_curtab = curtab;
15712 goto_tabpage_tp(tp);
15713 if (!win_valid(win))
15714 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 curwin = win;
15716 curbuf = curwin->w_buffer;
15717#endif
15718
15719 if (*varname == '&')
15720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015721 long numval;
15722 char_u *strval;
15723 int error = FALSE;
15724
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015726 numval = get_tv_number_chk(varp, &error);
15727 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015728 if (!error && strval != NULL)
15729 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 }
15731 else
15732 {
15733 winvarname = alloc((unsigned)STRLEN(varname) + 3);
15734 if (winvarname != NULL)
15735 {
15736 STRCPY(winvarname, "w:");
15737 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015738 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 vim_free(winvarname);
15740 }
15741 }
15742
15743#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015744 /* Restore current tabpage and window, if still valid (autocomands can
15745 * make them invalid). */
15746 if (valid_tabpage(save_curtab))
15747 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 if (win_valid(save_curwin))
15749 {
15750 curwin = save_curwin;
15751 curbuf = curwin->w_buffer;
15752 }
15753#endif
15754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755}
15756
15757/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015758 * "shellescape({string})" function
15759 */
15760 static void
15761f_shellescape(argvars, rettv)
15762 typval_T *argvars;
15763 typval_T *rettv;
15764{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015765 rettv->vval.v_string = vim_strsave_shellescape(
15766 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000015767 rettv->v_type = VAR_STRING;
15768}
15769
15770/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015771 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 */
15773 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015775 typval_T *argvars;
15776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015778 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780 p = get_tv_string(&argvars[0]);
15781 rettv->vval.v_string = vim_strsave(p);
15782 simplify_filename(rettv->vval.v_string); /* simplify in place */
15783 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784}
15785
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015786#ifdef FEAT_FLOAT
15787/*
15788 * "sin()" function
15789 */
15790 static void
15791f_sin(argvars, rettv)
15792 typval_T *argvars;
15793 typval_T *rettv;
15794{
15795 float_T f;
15796
15797 rettv->v_type = VAR_FLOAT;
15798 if (get_float_arg(argvars, &f) == OK)
15799 rettv->vval.v_float = sin(f);
15800 else
15801 rettv->vval.v_float = 0.0;
15802}
15803#endif
15804
Bram Moolenaar0d660222005-01-07 21:51:51 +000015805static int
15806#ifdef __BORLANDC__
15807 _RTLENTRYF
15808#endif
15809 item_compare __ARGS((const void *s1, const void *s2));
15810static int
15811#ifdef __BORLANDC__
15812 _RTLENTRYF
15813#endif
15814 item_compare2 __ARGS((const void *s1, const void *s2));
15815
15816static int item_compare_ic;
15817static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015818static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015819#define ITEM_COMPARE_FAIL 999
15820
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015824 static int
15825#ifdef __BORLANDC__
15826_RTLENTRYF
15827#endif
15828item_compare(s1, s2)
15829 const void *s1;
15830 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015832 char_u *p1, *p2;
15833 char_u *tofree1, *tofree2;
15834 int res;
15835 char_u numbuf1[NUMBUFLEN];
15836 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015838 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
15839 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000015840 if (p1 == NULL)
15841 p1 = (char_u *)"";
15842 if (p2 == NULL)
15843 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000015844 if (item_compare_ic)
15845 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015847 res = STRCMP(p1, p2);
15848 vim_free(tofree1);
15849 vim_free(tofree2);
15850 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851}
15852
15853 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000015854#ifdef __BORLANDC__
15855_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000015857item_compare2(s1, s2)
15858 const void *s1;
15859 const void *s2;
15860{
15861 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000015862 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015863 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000015864 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015866 /* shortcut after failure in previous call; compare all items equal */
15867 if (item_compare_func_err)
15868 return 0;
15869
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015870 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15871 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015872 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
15873 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015874
15875 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015876 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000015877 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015878 clear_tv(&argv[0]);
15879 clear_tv(&argv[1]);
15880
15881 if (res == FAIL)
15882 res = ITEM_COMPARE_FAIL;
15883 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015884 res = get_tv_number_chk(&rettv, &item_compare_func_err);
15885 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000015886 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015887 clear_tv(&rettv);
15888 return res;
15889}
15890
15891/*
15892 * "sort({list})" function
15893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015894 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015895f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015896 typval_T *argvars;
15897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898{
Bram Moolenaar33570922005-01-25 22:26:29 +000015899 list_T *l;
15900 listitem_T *li;
15901 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015902 long len;
15903 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904
Bram Moolenaar0d660222005-01-07 21:51:51 +000015905 if (argvars[0].v_type != VAR_LIST)
15906 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907 else
15908 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015909 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015910 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015911 return;
15912 rettv->vval.v_list = l;
15913 rettv->v_type = VAR_LIST;
15914 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015915
Bram Moolenaar0d660222005-01-07 21:51:51 +000015916 len = list_len(l);
15917 if (len <= 1)
15918 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919
Bram Moolenaar0d660222005-01-07 21:51:51 +000015920 item_compare_ic = FALSE;
15921 item_compare_func = NULL;
15922 if (argvars[1].v_type != VAR_UNKNOWN)
15923 {
15924 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015925 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015926 else
15927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015928 int error = FALSE;
15929
15930 i = get_tv_number_chk(&argvars[1], &error);
15931 if (error)
15932 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015933 if (i == 1)
15934 item_compare_ic = TRUE;
15935 else
15936 item_compare_func = get_tv_string(&argvars[1]);
15937 }
15938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939
Bram Moolenaar0d660222005-01-07 21:51:51 +000015940 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015941 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015942 if (ptrs == NULL)
15943 return;
15944 i = 0;
15945 for (li = l->lv_first; li != NULL; li = li->li_next)
15946 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015948 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015949 /* test the compare function */
15950 if (item_compare_func != NULL
15951 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
15952 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015953 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015955 {
15956 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015957 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000015958 item_compare_func == NULL ? item_compare : item_compare2);
15959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015960 if (!item_compare_func_err)
15961 {
15962 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000015963 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015964 l->lv_len = 0;
15965 for (i = 0; i < len; ++i)
15966 list_append(l, ptrs[i]);
15967 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015968 }
15969
15970 vim_free(ptrs);
15971 }
15972}
15973
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015974/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015975 * "soundfold({word})" function
15976 */
15977 static void
15978f_soundfold(argvars, rettv)
15979 typval_T *argvars;
15980 typval_T *rettv;
15981{
15982 char_u *s;
15983
15984 rettv->v_type = VAR_STRING;
15985 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000015986#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000015987 rettv->vval.v_string = eval_soundfold(s);
15988#else
15989 rettv->vval.v_string = vim_strsave(s);
15990#endif
15991}
15992
15993/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015994 * "spellbadword()" function
15995 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015996 static void
15997f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015998 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015999 typval_T *rettv;
16000{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016001 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016002 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016003 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016004
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016005 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016006 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016007
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016008#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016009 if (argvars[0].v_type == VAR_UNKNOWN)
16010 {
16011 /* Find the start and length of the badly spelled word. */
16012 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16013 if (len != 0)
16014 word = ml_get_cursor();
16015 }
16016 else if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16017 {
16018 char_u *str = get_tv_string_chk(&argvars[0]);
16019 int capcol = -1;
16020
16021 if (str != NULL)
16022 {
16023 /* Check the argument for spelling. */
16024 while (*str != NUL)
16025 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016026 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016027 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016028 {
16029 word = str;
16030 break;
16031 }
16032 str += len;
16033 }
16034 }
16035 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016036#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016037
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016038 list_append_string(rettv->vval.v_list, word, len);
16039 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016040 attr == HLF_SPB ? "bad" :
16041 attr == HLF_SPR ? "rare" :
16042 attr == HLF_SPL ? "local" :
16043 attr == HLF_SPC ? "caps" :
16044 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016045}
16046
16047/*
16048 * "spellsuggest()" function
16049 */
16050 static void
16051f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016052 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016053 typval_T *rettv;
16054{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016055#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016056 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016057 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016058 int maxcount;
16059 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016060 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016061 listitem_T *li;
16062 int need_capital = FALSE;
16063#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016064
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016065 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016066 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016067
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016068#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016069 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
16070 {
16071 str = get_tv_string(&argvars[0]);
16072 if (argvars[1].v_type != VAR_UNKNOWN)
16073 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016074 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016075 if (maxcount <= 0)
16076 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016077 if (argvars[2].v_type != VAR_UNKNOWN)
16078 {
16079 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16080 if (typeerr)
16081 return;
16082 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016083 }
16084 else
16085 maxcount = 25;
16086
Bram Moolenaar4770d092006-01-12 23:22:24 +000016087 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016088
16089 for (i = 0; i < ga.ga_len; ++i)
16090 {
16091 str = ((char_u **)ga.ga_data)[i];
16092
16093 li = listitem_alloc();
16094 if (li == NULL)
16095 vim_free(str);
16096 else
16097 {
16098 li->li_tv.v_type = VAR_STRING;
16099 li->li_tv.v_lock = 0;
16100 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016101 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016102 }
16103 }
16104 ga_clear(&ga);
16105 }
16106#endif
16107}
16108
Bram Moolenaar0d660222005-01-07 21:51:51 +000016109 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016110f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016111 typval_T *argvars;
16112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016113{
16114 char_u *str;
16115 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016116 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016117 regmatch_T regmatch;
16118 char_u patbuf[NUMBUFLEN];
16119 char_u *save_cpo;
16120 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016121 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016122 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016123 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016124
16125 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16126 save_cpo = p_cpo;
16127 p_cpo = (char_u *)"";
16128
16129 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016130 if (argvars[1].v_type != VAR_UNKNOWN)
16131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016132 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16133 if (pat == NULL)
16134 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016135 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016136 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016137 }
16138 if (pat == NULL || *pat == NUL)
16139 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016140
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016141 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016143 if (typeerr)
16144 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145
Bram Moolenaar0d660222005-01-07 21:51:51 +000016146 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16147 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016149 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016150 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016151 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016152 if (*str == NUL)
16153 match = FALSE; /* empty item at the end */
16154 else
16155 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016156 if (match)
16157 end = regmatch.startp[0];
16158 else
16159 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016160 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16161 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016162 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016163 if (list_append_string(rettv->vval.v_list, str,
16164 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016165 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016166 }
16167 if (!match)
16168 break;
16169 /* Advance to just after the match. */
16170 if (regmatch.endp[0] > str)
16171 col = 0;
16172 else
16173 {
16174 /* Don't get stuck at the same match. */
16175#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016176 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016177#else
16178 col = 1;
16179#endif
16180 }
16181 str = regmatch.endp[0];
16182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183
Bram Moolenaar0d660222005-01-07 21:51:51 +000016184 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186
Bram Moolenaar0d660222005-01-07 21:51:51 +000016187 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188}
16189
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016190#ifdef FEAT_FLOAT
16191/*
16192 * "sqrt()" function
16193 */
16194 static void
16195f_sqrt(argvars, rettv)
16196 typval_T *argvars;
16197 typval_T *rettv;
16198{
16199 float_T f;
16200
16201 rettv->v_type = VAR_FLOAT;
16202 if (get_float_arg(argvars, &f) == OK)
16203 rettv->vval.v_float = sqrt(f);
16204 else
16205 rettv->vval.v_float = 0.0;
16206}
16207
16208/*
16209 * "str2float()" function
16210 */
16211 static void
16212f_str2float(argvars, rettv)
16213 typval_T *argvars;
16214 typval_T *rettv;
16215{
16216 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16217
16218 if (*p == '+')
16219 p = skipwhite(p + 1);
16220 (void)string2float(p, &rettv->vval.v_float);
16221 rettv->v_type = VAR_FLOAT;
16222}
16223#endif
16224
Bram Moolenaar2c932302006-03-18 21:42:09 +000016225/*
16226 * "str2nr()" function
16227 */
16228 static void
16229f_str2nr(argvars, rettv)
16230 typval_T *argvars;
16231 typval_T *rettv;
16232{
16233 int base = 10;
16234 char_u *p;
16235 long n;
16236
16237 if (argvars[1].v_type != VAR_UNKNOWN)
16238 {
16239 base = get_tv_number(&argvars[1]);
16240 if (base != 8 && base != 10 && base != 16)
16241 {
16242 EMSG(_(e_invarg));
16243 return;
16244 }
16245 }
16246
16247 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016248 if (*p == '+')
16249 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016250 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16251 rettv->vval.v_number = n;
16252}
16253
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254#ifdef HAVE_STRFTIME
16255/*
16256 * "strftime({format}[, {time}])" function
16257 */
16258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016259f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016260 typval_T *argvars;
16261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262{
16263 char_u result_buf[256];
16264 struct tm *curtime;
16265 time_t seconds;
16266 char_u *p;
16267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016268 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016270 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016271 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272 seconds = time(NULL);
16273 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016274 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 curtime = localtime(&seconds);
16276 /* MSVC returns NULL for an invalid value of seconds. */
16277 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016278 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 else
16280 {
16281# ifdef FEAT_MBYTE
16282 vimconv_T conv;
16283 char_u *enc;
16284
16285 conv.vc_type = CONV_NONE;
16286 enc = enc_locale();
16287 convert_setup(&conv, p_enc, enc);
16288 if (conv.vc_type != CONV_NONE)
16289 p = string_convert(&conv, p, NULL);
16290# endif
16291 if (p != NULL)
16292 (void)strftime((char *)result_buf, sizeof(result_buf),
16293 (char *)p, curtime);
16294 else
16295 result_buf[0] = NUL;
16296
16297# ifdef FEAT_MBYTE
16298 if (conv.vc_type != CONV_NONE)
16299 vim_free(p);
16300 convert_setup(&conv, enc, p_enc);
16301 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016302 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303 else
16304# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016305 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306
16307# ifdef FEAT_MBYTE
16308 /* Release conversion descriptors */
16309 convert_setup(&conv, NULL, NULL);
16310 vim_free(enc);
16311# endif
16312 }
16313}
16314#endif
16315
16316/*
16317 * "stridx()" function
16318 */
16319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016320f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016321 typval_T *argvars;
16322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323{
16324 char_u buf[NUMBUFLEN];
16325 char_u *needle;
16326 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016327 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016329 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016331 needle = get_tv_string_chk(&argvars[1]);
16332 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016333 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016334 if (needle == NULL || haystack == NULL)
16335 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336
Bram Moolenaar33570922005-01-25 22:26:29 +000016337 if (argvars[2].v_type != VAR_UNKNOWN)
16338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016339 int error = FALSE;
16340
16341 start_idx = get_tv_number_chk(&argvars[2], &error);
16342 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016343 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016344 if (start_idx >= 0)
16345 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016346 }
16347
16348 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16349 if (pos != NULL)
16350 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351}
16352
16353/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016354 * "string()" function
16355 */
16356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016357f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016358 typval_T *argvars;
16359 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016360{
16361 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016362 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016364 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016365 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016366 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016367 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016368 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369}
16370
16371/*
16372 * "strlen()" function
16373 */
16374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016375f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016376 typval_T *argvars;
16377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016379 rettv->vval.v_number = (varnumber_T)(STRLEN(
16380 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381}
16382
16383/*
16384 * "strpart()" function
16385 */
16386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016387f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016388 typval_T *argvars;
16389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390{
16391 char_u *p;
16392 int n;
16393 int len;
16394 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016395 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016397 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398 slen = (int)STRLEN(p);
16399
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016400 n = get_tv_number_chk(&argvars[1], &error);
16401 if (error)
16402 len = 0;
16403 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016404 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 else
16406 len = slen - n; /* default len: all bytes that are available. */
16407
16408 /*
16409 * Only return the overlap between the specified part and the actual
16410 * string.
16411 */
16412 if (n < 0)
16413 {
16414 len += n;
16415 n = 0;
16416 }
16417 else if (n > slen)
16418 n = slen;
16419 if (len < 0)
16420 len = 0;
16421 else if (n + len > slen)
16422 len = slen - n;
16423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016424 rettv->v_type = VAR_STRING;
16425 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426}
16427
16428/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016429 * "strridx()" function
16430 */
16431 static void
16432f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016433 typval_T *argvars;
16434 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016435{
16436 char_u buf[NUMBUFLEN];
16437 char_u *needle;
16438 char_u *haystack;
16439 char_u *rest;
16440 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016441 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016443 needle = get_tv_string_chk(&argvars[1]);
16444 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016445
16446 rettv->vval.v_number = -1;
16447 if (needle == NULL || haystack == NULL)
16448 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016449
16450 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016451 if (argvars[2].v_type != VAR_UNKNOWN)
16452 {
16453 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016454 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016455 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016456 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016457 }
16458 else
16459 end_idx = haystack_len;
16460
Bram Moolenaar0d660222005-01-07 21:51:51 +000016461 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016462 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016463 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016464 lastmatch = haystack + end_idx;
16465 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016466 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016467 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016468 for (rest = haystack; *rest != '\0'; ++rest)
16469 {
16470 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016471 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016472 break;
16473 lastmatch = rest;
16474 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016475 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016476
16477 if (lastmatch == NULL)
16478 rettv->vval.v_number = -1;
16479 else
16480 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16481}
16482
16483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484 * "strtrans()" function
16485 */
16486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016487f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016488 typval_T *argvars;
16489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016491 rettv->v_type = VAR_STRING;
16492 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493}
16494
16495/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016496 * "submatch()" function
16497 */
16498 static void
16499f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016500 typval_T *argvars;
16501 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016502{
16503 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016504 rettv->vval.v_string =
16505 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016506}
16507
16508/*
16509 * "substitute()" function
16510 */
16511 static void
16512f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 typval_T *argvars;
16514 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016515{
16516 char_u patbuf[NUMBUFLEN];
16517 char_u subbuf[NUMBUFLEN];
16518 char_u flagsbuf[NUMBUFLEN];
16519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016520 char_u *str = get_tv_string_chk(&argvars[0]);
16521 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16522 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16523 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16524
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016526 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16527 rettv->vval.v_string = NULL;
16528 else
16529 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016530}
16531
16532/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016533 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016536f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016537 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539{
16540 int id = 0;
16541#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016542 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 long col;
16544 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016545 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016547 lnum = get_tv_lnum(argvars); /* -1 on type error */
16548 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16549 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016551 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016552 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016553 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554#endif
16555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016556 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557}
16558
16559/*
16560 * "synIDattr(id, what [, mode])" function
16561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016563f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016564 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566{
16567 char_u *p = NULL;
16568#ifdef FEAT_SYN_HL
16569 int id;
16570 char_u *what;
16571 char_u *mode;
16572 char_u modebuf[NUMBUFLEN];
16573 int modec;
16574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016575 id = get_tv_number(&argvars[0]);
16576 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016577 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016579 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580 modec = TOLOWER_ASC(mode[0]);
16581 if (modec != 't' && modec != 'c'
16582#ifdef FEAT_GUI
16583 && modec != 'g'
16584#endif
16585 )
16586 modec = 0; /* replace invalid with current */
16587 }
16588 else
16589 {
16590#ifdef FEAT_GUI
16591 if (gui.in_use)
16592 modec = 'g';
16593 else
16594#endif
16595 if (t_colors > 1)
16596 modec = 'c';
16597 else
16598 modec = 't';
16599 }
16600
16601
16602 switch (TOLOWER_ASC(what[0]))
16603 {
16604 case 'b':
16605 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16606 p = highlight_color(id, what, modec);
16607 else /* bold */
16608 p = highlight_has_attr(id, HL_BOLD, modec);
16609 break;
16610
16611 case 'f': /* fg[#] */
16612 p = highlight_color(id, what, modec);
16613 break;
16614
16615 case 'i':
16616 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16617 p = highlight_has_attr(id, HL_INVERSE, modec);
16618 else /* italic */
16619 p = highlight_has_attr(id, HL_ITALIC, modec);
16620 break;
16621
16622 case 'n': /* name */
16623 p = get_highlight_name(NULL, id - 1);
16624 break;
16625
16626 case 'r': /* reverse */
16627 p = highlight_has_attr(id, HL_INVERSE, modec);
16628 break;
16629
Bram Moolenaar6f507d62008-11-28 10:16:05 +000016630 case 's':
16631 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
16632 p = highlight_color(id, what, modec);
16633 else /* standout */
16634 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 break;
16636
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000016637 case 'u':
16638 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
16639 /* underline */
16640 p = highlight_has_attr(id, HL_UNDERLINE, modec);
16641 else
16642 /* undercurl */
16643 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 break;
16645 }
16646
16647 if (p != NULL)
16648 p = vim_strsave(p);
16649#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016650 rettv->v_type = VAR_STRING;
16651 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652}
16653
16654/*
16655 * "synIDtrans(id)" function
16656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016658f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016659 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661{
16662 int id;
16663
16664#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016665 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666
16667 if (id > 0)
16668 id = syn_get_final_id(id);
16669 else
16670#endif
16671 id = 0;
16672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016673 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674}
16675
16676/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016677 * "synstack(lnum, col)" function
16678 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016679 static void
16680f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016681 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016682 typval_T *rettv;
16683{
16684#ifdef FEAT_SYN_HL
16685 long lnum;
16686 long col;
16687 int i;
16688 int id;
16689#endif
16690
16691 rettv->v_type = VAR_LIST;
16692 rettv->vval.v_list = NULL;
16693
16694#ifdef FEAT_SYN_HL
16695 lnum = get_tv_lnum(argvars); /* -1 on type error */
16696 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16697
16698 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar6cad8bd2008-09-10 13:39:10 +000016699 && col >= 0 && (col == 0 || col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016700 && rettv_list_alloc(rettv) != FAIL)
16701 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016702 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000016703 for (i = 0; ; ++i)
16704 {
16705 id = syn_get_stack_item(i);
16706 if (id < 0)
16707 break;
16708 if (list_append_number(rettv->vval.v_list, id) == FAIL)
16709 break;
16710 }
16711 }
16712#endif
16713}
16714
16715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 * "system()" function
16717 */
16718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016719f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016720 typval_T *argvars;
16721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016723 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016725 char_u *infile = NULL;
16726 char_u buf[NUMBUFLEN];
16727 int err = FALSE;
16728 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016730 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016731 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000016732
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016733 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016734 {
16735 /*
16736 * Write the string to a temp file, to be used for input of the shell
16737 * command.
16738 */
16739 if ((infile = vim_tempname('i')) == NULL)
16740 {
16741 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000016742 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016743 }
16744
16745 fd = mch_fopen((char *)infile, WRITEBIN);
16746 if (fd == NULL)
16747 {
16748 EMSG2(_(e_notopen), infile);
16749 goto done;
16750 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016751 p = get_tv_string_buf_chk(&argvars[1], buf);
16752 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016753 {
16754 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016755 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016756 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016757 if (fwrite(p, STRLEN(p), 1, fd) != 1)
16758 err = TRUE;
16759 if (fclose(fd) != 0)
16760 err = TRUE;
16761 if (err)
16762 {
16763 EMSG(_("E677: Error writing temp file"));
16764 goto done;
16765 }
16766 }
16767
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016768 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
16769 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016770
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771#ifdef USE_CR
16772 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016773 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 {
16775 char_u *s;
16776
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016777 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 {
16779 if (*s == CAR)
16780 *s = NL;
16781 }
16782 }
16783#else
16784# ifdef USE_CRNL
16785 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016786 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 {
16788 char_u *s, *d;
16789
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016790 d = res;
16791 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 {
16793 if (s[0] == CAR && s[1] == NL)
16794 ++s;
16795 *d++ = *s;
16796 }
16797 *d = NUL;
16798 }
16799# endif
16800#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000016801
16802done:
16803 if (infile != NULL)
16804 {
16805 mch_remove(infile);
16806 vim_free(infile);
16807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016808 rettv->v_type = VAR_STRING;
16809 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810}
16811
16812/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016813 * "tabpagebuflist()" function
16814 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016815 static void
16816f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016817 typval_T *argvars UNUSED;
16818 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016819{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016820#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016821 tabpage_T *tp;
16822 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016823
16824 if (argvars[0].v_type == VAR_UNKNOWN)
16825 wp = firstwin;
16826 else
16827 {
16828 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16829 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000016830 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016831 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016832 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016833 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016834 for (; wp != NULL; wp = wp->w_next)
16835 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016836 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016837 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016838 }
16839#endif
16840}
16841
16842
16843/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016844 * "tabpagenr()" function
16845 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016846 static void
16847f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016848 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016849 typval_T *rettv;
16850{
16851 int nr = 1;
16852#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016853 char_u *arg;
16854
16855 if (argvars[0].v_type != VAR_UNKNOWN)
16856 {
16857 arg = get_tv_string_chk(&argvars[0]);
16858 nr = 0;
16859 if (arg != NULL)
16860 {
16861 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000016862 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016863 else
16864 EMSG2(_(e_invexpr2), arg);
16865 }
16866 }
16867 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000016868 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016869#endif
16870 rettv->vval.v_number = nr;
16871}
16872
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016873
16874#ifdef FEAT_WINDOWS
16875static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
16876
16877/*
16878 * Common code for tabpagewinnr() and winnr().
16879 */
16880 static int
16881get_winnr(tp, argvar)
16882 tabpage_T *tp;
16883 typval_T *argvar;
16884{
16885 win_T *twin;
16886 int nr = 1;
16887 win_T *wp;
16888 char_u *arg;
16889
16890 twin = (tp == curtab) ? curwin : tp->tp_curwin;
16891 if (argvar->v_type != VAR_UNKNOWN)
16892 {
16893 arg = get_tv_string_chk(argvar);
16894 if (arg == NULL)
16895 nr = 0; /* type error; errmsg already given */
16896 else if (STRCMP(arg, "$") == 0)
16897 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
16898 else if (STRCMP(arg, "#") == 0)
16899 {
16900 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
16901 if (twin == NULL)
16902 nr = 0;
16903 }
16904 else
16905 {
16906 EMSG2(_(e_invexpr2), arg);
16907 nr = 0;
16908 }
16909 }
16910
16911 if (nr > 0)
16912 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
16913 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016914 {
16915 if (wp == NULL)
16916 {
16917 /* didn't find it in this tabpage */
16918 nr = 0;
16919 break;
16920 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016921 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000016922 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016923 return nr;
16924}
16925#endif
16926
16927/*
16928 * "tabpagewinnr()" function
16929 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016930 static void
16931f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016932 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000016933 typval_T *rettv;
16934{
16935 int nr = 1;
16936#ifdef FEAT_WINDOWS
16937 tabpage_T *tp;
16938
16939 tp = find_tabpage((int)get_tv_number(&argvars[0]));
16940 if (tp == NULL)
16941 nr = 0;
16942 else
16943 nr = get_winnr(tp, &argvars[1]);
16944#endif
16945 rettv->vval.v_number = nr;
16946}
16947
16948
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000016949/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016950 * "tagfiles()" function
16951 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016952 static void
16953f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016954 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016955 typval_T *rettv;
16956{
16957 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016958 tagname_T tn;
16959 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016960
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016961 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016962 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016963
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016964 for (first = TRUE; ; first = FALSE)
16965 if (get_tagfname(&tn, first, fname) == FAIL
16966 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016967 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016968 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000016969}
16970
16971/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000016972 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016973 */
16974 static void
16975f_taglist(argvars, rettv)
16976 typval_T *argvars;
16977 typval_T *rettv;
16978{
16979 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016980
16981 tag_pattern = get_tv_string(&argvars[0]);
16982
16983 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 if (*tag_pattern == NUL)
16985 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016986
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016987 if (rettv_list_alloc(rettv) == OK)
16988 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000016989}
16990
16991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 * "tempname()" function
16993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016995f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016996 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998{
16999 static int x = 'A';
17000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017001 rettv->v_type = VAR_STRING;
17002 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003
17004 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17005 * names. Skip 'I' and 'O', they are used for shell redirection. */
17006 do
17007 {
17008 if (x == 'Z')
17009 x = '0';
17010 else if (x == '9')
17011 x = 'A';
17012 else
17013 {
17014#ifdef EBCDIC
17015 if (x == 'I')
17016 x = 'J';
17017 else if (x == 'R')
17018 x = 'S';
17019 else
17020#endif
17021 ++x;
17022 }
17023 } while (x == 'I' || x == 'O');
17024}
17025
17026/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017027 * "test(list)" function: Just checking the walls...
17028 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017029 static void
17030f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017031 typval_T *argvars UNUSED;
17032 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017033{
17034 /* Used for unit testing. Change the code below to your liking. */
17035#if 0
17036 listitem_T *li;
17037 list_T *l;
17038 char_u *bad, *good;
17039
17040 if (argvars[0].v_type != VAR_LIST)
17041 return;
17042 l = argvars[0].vval.v_list;
17043 if (l == NULL)
17044 return;
17045 li = l->lv_first;
17046 if (li == NULL)
17047 return;
17048 bad = get_tv_string(&li->li_tv);
17049 li = li->li_next;
17050 if (li == NULL)
17051 return;
17052 good = get_tv_string(&li->li_tv);
17053 rettv->vval.v_number = test_edit_score(bad, good);
17054#endif
17055}
17056
17057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 * "tolower(string)" function
17059 */
17060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017061f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017062 typval_T *argvars;
17063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064{
17065 char_u *p;
17066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017067 p = vim_strsave(get_tv_string(&argvars[0]));
17068 rettv->v_type = VAR_STRING;
17069 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070
17071 if (p != NULL)
17072 while (*p != NUL)
17073 {
17074#ifdef FEAT_MBYTE
17075 int l;
17076
17077 if (enc_utf8)
17078 {
17079 int c, lc;
17080
17081 c = utf_ptr2char(p);
17082 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017083 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 /* TODO: reallocate string when byte count changes. */
17085 if (utf_char2len(lc) == l)
17086 utf_char2bytes(lc, p);
17087 p += l;
17088 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017089 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090 p += l; /* skip multi-byte character */
17091 else
17092#endif
17093 {
17094 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17095 ++p;
17096 }
17097 }
17098}
17099
17100/*
17101 * "toupper(string)" function
17102 */
17103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017104f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 typval_T *argvars;
17106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017108 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017109 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110}
17111
17112/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017113 * "tr(string, fromstr, tostr)" function
17114 */
17115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017116f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017117 typval_T *argvars;
17118 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017119{
17120 char_u *instr;
17121 char_u *fromstr;
17122 char_u *tostr;
17123 char_u *p;
17124#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017125 int inlen;
17126 int fromlen;
17127 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017128 int idx;
17129 char_u *cpstr;
17130 int cplen;
17131 int first = TRUE;
17132#endif
17133 char_u buf[NUMBUFLEN];
17134 char_u buf2[NUMBUFLEN];
17135 garray_T ga;
17136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017137 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017138 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17139 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017140
17141 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017142 rettv->v_type = VAR_STRING;
17143 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017144 if (fromstr == NULL || tostr == NULL)
17145 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017146 ga_init2(&ga, (int)sizeof(char), 80);
17147
17148#ifdef FEAT_MBYTE
17149 if (!has_mbyte)
17150#endif
17151 /* not multi-byte: fromstr and tostr must be the same length */
17152 if (STRLEN(fromstr) != STRLEN(tostr))
17153 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017154#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017155error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017156#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017157 EMSG2(_(e_invarg2), fromstr);
17158 ga_clear(&ga);
17159 return;
17160 }
17161
17162 /* fromstr and tostr have to contain the same number of chars */
17163 while (*instr != NUL)
17164 {
17165#ifdef FEAT_MBYTE
17166 if (has_mbyte)
17167 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017168 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017169 cpstr = instr;
17170 cplen = inlen;
17171 idx = 0;
17172 for (p = fromstr; *p != NUL; p += fromlen)
17173 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017174 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017175 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17176 {
17177 for (p = tostr; *p != NUL; p += tolen)
17178 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017179 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017180 if (idx-- == 0)
17181 {
17182 cplen = tolen;
17183 cpstr = p;
17184 break;
17185 }
17186 }
17187 if (*p == NUL) /* tostr is shorter than fromstr */
17188 goto error;
17189 break;
17190 }
17191 ++idx;
17192 }
17193
17194 if (first && cpstr == instr)
17195 {
17196 /* Check that fromstr and tostr have the same number of
17197 * (multi-byte) characters. Done only once when a character
17198 * of instr doesn't appear in fromstr. */
17199 first = FALSE;
17200 for (p = tostr; *p != NUL; p += tolen)
17201 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017202 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017203 --idx;
17204 }
17205 if (idx != 0)
17206 goto error;
17207 }
17208
17209 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017210 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017211 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017212
17213 instr += inlen;
17214 }
17215 else
17216#endif
17217 {
17218 /* When not using multi-byte chars we can do it faster. */
17219 p = vim_strchr(fromstr, *instr);
17220 if (p != NULL)
17221 ga_append(&ga, tostr[p - fromstr]);
17222 else
17223 ga_append(&ga, *instr);
17224 ++instr;
17225 }
17226 }
17227
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017228 /* add a terminating NUL */
17229 ga_grow(&ga, 1);
17230 ga_append(&ga, NUL);
17231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017232 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017233}
17234
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017235#ifdef FEAT_FLOAT
17236/*
17237 * "trunc({float})" function
17238 */
17239 static void
17240f_trunc(argvars, rettv)
17241 typval_T *argvars;
17242 typval_T *rettv;
17243{
17244 float_T f;
17245
17246 rettv->v_type = VAR_FLOAT;
17247 if (get_float_arg(argvars, &f) == OK)
17248 /* trunc() is not in C90, use floor() or ceil() instead. */
17249 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17250 else
17251 rettv->vval.v_float = 0.0;
17252}
17253#endif
17254
Bram Moolenaar8299df92004-07-10 09:47:34 +000017255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256 * "type(expr)" function
17257 */
17258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017259f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017260 typval_T *argvars;
17261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017263 int n;
17264
17265 switch (argvars[0].v_type)
17266 {
17267 case VAR_NUMBER: n = 0; break;
17268 case VAR_STRING: n = 1; break;
17269 case VAR_FUNC: n = 2; break;
17270 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017271 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017272#ifdef FEAT_FLOAT
17273 case VAR_FLOAT: n = 5; break;
17274#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017275 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17276 }
17277 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278}
17279
17280/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017281 * "values(dict)" function
17282 */
17283 static void
17284f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017285 typval_T *argvars;
17286 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017287{
17288 dict_list(argvars, rettv, 1);
17289}
17290
17291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 * "virtcol(string)" function
17293 */
17294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017295f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017296 typval_T *argvars;
17297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298{
17299 colnr_T vcol = 0;
17300 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017301 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017303 fp = var2fpos(&argvars[0], FALSE, &fnum);
17304 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17305 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306 {
17307 getvvcol(curwin, fp, NULL, NULL, &vcol);
17308 ++vcol;
17309 }
17310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017311 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312}
17313
17314/*
17315 * "visualmode()" function
17316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017318f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017319 typval_T *argvars UNUSED;
17320 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321{
17322#ifdef FEAT_VISUAL
17323 char_u str[2];
17324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017325 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 str[0] = curbuf->b_visual_mode_eval;
17327 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017328 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329
17330 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017331 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333#endif
17334}
17335
17336/*
17337 * "winbufnr(nr)" function
17338 */
17339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017340f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017341 typval_T *argvars;
17342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343{
17344 win_T *wp;
17345
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017346 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017350 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351}
17352
17353/*
17354 * "wincol()" function
17355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017357f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017358 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360{
17361 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017362 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363}
17364
17365/*
17366 * "winheight(nr)" function
17367 */
17368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017369f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017370 typval_T *argvars;
17371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372{
17373 win_T *wp;
17374
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017375 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017377 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017379 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380}
17381
17382/*
17383 * "winline()" function
17384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017386f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017387 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389{
17390 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017391 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392}
17393
17394/*
17395 * "winnr()" function
17396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017398f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401{
17402 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017403
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017405 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017407 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408}
17409
17410/*
17411 * "winrestcmd()" function
17412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017414f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017415 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417{
17418#ifdef FEAT_WINDOWS
17419 win_T *wp;
17420 int winnr = 1;
17421 garray_T ga;
17422 char_u buf[50];
17423
17424 ga_init2(&ga, (int)sizeof(char), 70);
17425 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17426 {
17427 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17428 ga_concat(&ga, buf);
17429# ifdef FEAT_VERTSPLIT
17430 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17431 ga_concat(&ga, buf);
17432# endif
17433 ++winnr;
17434 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017435 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017437 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017439 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017441 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442}
17443
17444/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017445 * "winrestview()" function
17446 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017447 static void
17448f_winrestview(argvars, rettv)
17449 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017450 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017451{
17452 dict_T *dict;
17453
17454 if (argvars[0].v_type != VAR_DICT
17455 || (dict = argvars[0].vval.v_dict) == NULL)
17456 EMSG(_(e_invarg));
17457 else
17458 {
17459 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17460 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17461#ifdef FEAT_VIRTUALEDIT
17462 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17463#endif
17464 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017465 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017466
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017467 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017468#ifdef FEAT_DIFF
17469 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17470#endif
17471 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17472 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17473
17474 check_cursor();
17475 changed_cline_bef_curs();
17476 invalidate_botline();
17477 redraw_later(VALID);
17478
17479 if (curwin->w_topline == 0)
17480 curwin->w_topline = 1;
17481 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17482 curwin->w_topline = curbuf->b_ml.ml_line_count;
17483#ifdef FEAT_DIFF
17484 check_topfill(curwin, TRUE);
17485#endif
17486 }
17487}
17488
17489/*
17490 * "winsaveview()" function
17491 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017492 static void
17493f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017494 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017495 typval_T *rettv;
17496{
17497 dict_T *dict;
17498
17499 dict = dict_alloc();
17500 if (dict == NULL)
17501 return;
17502 rettv->v_type = VAR_DICT;
17503 rettv->vval.v_dict = dict;
17504 ++dict->dv_refcount;
17505
17506 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17507 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17508#ifdef FEAT_VIRTUALEDIT
17509 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17510#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017511 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017512 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17513
17514 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17515#ifdef FEAT_DIFF
17516 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17517#endif
17518 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17519 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17520}
17521
17522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 * "winwidth(nr)" function
17524 */
17525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017526f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017527 typval_T *argvars;
17528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529{
17530 win_T *wp;
17531
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017532 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017534 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535 else
17536#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017537 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017539 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540#endif
17541}
17542
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017544 * "writefile()" function
17545 */
17546 static void
17547f_writefile(argvars, rettv)
17548 typval_T *argvars;
17549 typval_T *rettv;
17550{
17551 int binary = FALSE;
17552 char_u *fname;
17553 FILE *fd;
17554 listitem_T *li;
17555 char_u *s;
17556 int ret = 0;
17557 int c;
17558
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017559 if (check_restricted() || check_secure())
17560 return;
17561
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017562 if (argvars[0].v_type != VAR_LIST)
17563 {
17564 EMSG2(_(e_listarg), "writefile()");
17565 return;
17566 }
17567 if (argvars[0].vval.v_list == NULL)
17568 return;
17569
17570 if (argvars[2].v_type != VAR_UNKNOWN
17571 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
17572 binary = TRUE;
17573
17574 /* Always open the file in binary mode, library functions have a mind of
17575 * their own about CR-LF conversion. */
17576 fname = get_tv_string(&argvars[1]);
17577 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
17578 {
17579 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
17580 ret = -1;
17581 }
17582 else
17583 {
17584 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
17585 li = li->li_next)
17586 {
17587 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
17588 {
17589 if (*s == '\n')
17590 c = putc(NUL, fd);
17591 else
17592 c = putc(*s, fd);
17593 if (c == EOF)
17594 {
17595 ret = -1;
17596 break;
17597 }
17598 }
17599 if (!binary || li->li_next != NULL)
17600 if (putc('\n', fd) == EOF)
17601 {
17602 ret = -1;
17603 break;
17604 }
17605 if (ret < 0)
17606 {
17607 EMSG(_(e_write));
17608 break;
17609 }
17610 }
17611 fclose(fd);
17612 }
17613
17614 rettv->vval.v_number = ret;
17615}
17616
17617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017619 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 */
17621 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000017622var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000017623 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017624 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017625 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017627 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017629 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630
Bram Moolenaara5525202006-03-02 22:52:09 +000017631 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017632 if (varp->v_type == VAR_LIST)
17633 {
17634 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017635 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000017636 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000017637 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017638
17639 l = varp->vval.v_list;
17640 if (l == NULL)
17641 return NULL;
17642
17643 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017644 pos.lnum = list_find_nr(l, 0L, &error);
17645 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017646 return NULL; /* invalid line number */
17647
17648 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017649 pos.col = list_find_nr(l, 1L, &error);
17650 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017651 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017652 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000017653
17654 /* We accept "$" for the column number: last column. */
17655 li = list_find(l, 1L);
17656 if (li != NULL && li->li_tv.v_type == VAR_STRING
17657 && li->li_tv.vval.v_string != NULL
17658 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
17659 pos.col = len + 1;
17660
Bram Moolenaara5525202006-03-02 22:52:09 +000017661 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000017662 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017663 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000017664 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017665
Bram Moolenaara5525202006-03-02 22:52:09 +000017666#ifdef FEAT_VIRTUALEDIT
17667 /* Get the virtual offset. Defaults to zero. */
17668 pos.coladd = list_find_nr(l, 2L, &error);
17669 if (error)
17670 pos.coladd = 0;
17671#endif
17672
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017673 return &pos;
17674 }
17675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017676 name = get_tv_string_chk(varp);
17677 if (name == NULL)
17678 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017679 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000017681#ifdef FEAT_VISUAL
17682 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
17683 {
17684 if (VIsual_active)
17685 return &VIsual;
17686 return &curwin->w_cursor;
17687 }
17688#endif
17689 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017691 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
17693 return NULL;
17694 return pp;
17695 }
Bram Moolenaara5525202006-03-02 22:52:09 +000017696
17697#ifdef FEAT_VIRTUALEDIT
17698 pos.coladd = 0;
17699#endif
17700
Bram Moolenaar477933c2007-07-17 14:32:23 +000017701 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017702 {
17703 pos.col = 0;
17704 if (name[1] == '0') /* "w0": first visible line */
17705 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017706 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017707 pos.lnum = curwin->w_topline;
17708 return &pos;
17709 }
17710 else if (name[1] == '$') /* "w$": last visible line */
17711 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000017712 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000017713 pos.lnum = curwin->w_botline - 1;
17714 return &pos;
17715 }
17716 }
17717 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000017719 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 {
17721 pos.lnum = curbuf->b_ml.ml_line_count;
17722 pos.col = 0;
17723 }
17724 else
17725 {
17726 pos.lnum = curwin->w_cursor.lnum;
17727 pos.col = (colnr_T)STRLEN(ml_get_curline());
17728 }
17729 return &pos;
17730 }
17731 return NULL;
17732}
17733
17734/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017735 * Convert list in "arg" into a position and optional file number.
17736 * When "fnump" is NULL there is no file number, only 3 items.
17737 * Note that the column is passed on as-is, the caller may want to decrement
17738 * it to use 1 for the first column.
17739 * Return FAIL when conversion is not possible, doesn't check the position for
17740 * validity.
17741 */
17742 static int
17743list2fpos(arg, posp, fnump)
17744 typval_T *arg;
17745 pos_T *posp;
17746 int *fnump;
17747{
17748 list_T *l = arg->vval.v_list;
17749 long i = 0;
17750 long n;
17751
Bram Moolenaarbde35262006-07-23 20:12:24 +000017752 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17753 * when "fnump" isn't NULL and "coladd" is optional. */
17754 if (arg->v_type != VAR_LIST
17755 || l == NULL
17756 || l->lv_len < (fnump == NULL ? 2 : 3)
17757 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017758 return FAIL;
17759
17760 if (fnump != NULL)
17761 {
17762 n = list_find_nr(l, i++, NULL); /* fnum */
17763 if (n < 0)
17764 return FAIL;
17765 if (n == 0)
17766 n = curbuf->b_fnum; /* current buffer */
17767 *fnump = n;
17768 }
17769
17770 n = list_find_nr(l, i++, NULL); /* lnum */
17771 if (n < 0)
17772 return FAIL;
17773 posp->lnum = n;
17774
17775 n = list_find_nr(l, i++, NULL); /* col */
17776 if (n < 0)
17777 return FAIL;
17778 posp->col = n;
17779
17780#ifdef FEAT_VIRTUALEDIT
17781 n = list_find_nr(l, i, NULL);
17782 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000017783 posp->coladd = 0;
17784 else
17785 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017786#endif
17787
17788 return OK;
17789}
17790
17791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 * Get the length of an environment variable name.
17793 * Advance "arg" to the first character after the name.
17794 * Return 0 for error.
17795 */
17796 static int
17797get_env_len(arg)
17798 char_u **arg;
17799{
17800 char_u *p;
17801 int len;
17802
17803 for (p = *arg; vim_isIDc(*p); ++p)
17804 ;
17805 if (p == *arg) /* no name found */
17806 return 0;
17807
17808 len = (int)(p - *arg);
17809 *arg = p;
17810 return len;
17811}
17812
17813/*
17814 * Get the length of the name of a function or internal variable.
17815 * "arg" is advanced to the first non-white character after the name.
17816 * Return 0 if something is wrong.
17817 */
17818 static int
17819get_id_len(arg)
17820 char_u **arg;
17821{
17822 char_u *p;
17823 int len;
17824
17825 /* Find the end of the name. */
17826 for (p = *arg; eval_isnamec(*p); ++p)
17827 ;
17828 if (p == *arg) /* no name found */
17829 return 0;
17830
17831 len = (int)(p - *arg);
17832 *arg = skipwhite(p);
17833
17834 return len;
17835}
17836
17837/*
Bram Moolenaara7043832005-01-21 11:56:39 +000017838 * Get the length of the name of a variable or function.
17839 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017841 * Return -1 if curly braces expansion failed.
17842 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843 * If the name contains 'magic' {}'s, expand them and return the
17844 * expanded name in an allocated string via 'alias' - caller must free.
17845 */
17846 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017847get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 char_u **arg;
17849 char_u **alias;
17850 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017851 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852{
17853 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854 char_u *p;
17855 char_u *expr_start;
17856 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857
17858 *alias = NULL; /* default to no alias */
17859
17860 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
17861 && (*arg)[2] == (int)KE_SNR)
17862 {
17863 /* hard coded <SNR>, already translated */
17864 *arg += 3;
17865 return get_id_len(arg) + 3;
17866 }
17867 len = eval_fname_script(*arg);
17868 if (len > 0)
17869 {
17870 /* literal "<SID>", "s:" or "<SNR>" */
17871 *arg += len;
17872 }
17873
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017875 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017877 p = find_name_end(*arg, &expr_start, &expr_end,
17878 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 if (expr_start != NULL)
17880 {
17881 char_u *temp_string;
17882
17883 if (!evaluate)
17884 {
17885 len += (int)(p - *arg);
17886 *arg = skipwhite(p);
17887 return len;
17888 }
17889
17890 /*
17891 * Include any <SID> etc in the expanded string:
17892 * Thus the -len here.
17893 */
17894 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
17895 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017896 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897 *alias = temp_string;
17898 *arg = skipwhite(p);
17899 return (int)STRLEN(temp_string);
17900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901
17902 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017903 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 EMSG2(_(e_invexpr2), *arg);
17905
17906 return len;
17907}
17908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017909/*
17910 * Find the end of a variable or function name, taking care of magic braces.
17911 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17912 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017913 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017914 * Return a pointer to just after the name. Equal to "arg" if there is no
17915 * valid name.
17916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017918find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 char_u *arg;
17920 char_u **expr_start;
17921 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017922 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017924 int mb_nest = 0;
17925 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926 char_u *p;
17927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017928 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017930 *expr_start = NULL;
17931 *expr_end = NULL;
17932 }
17933
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017934 /* Quick check for valid starting character. */
17935 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
17936 return arg;
17937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017938 for (p = arg; *p != NUL
17939 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017940 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017941 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017942 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000017943 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017944 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000017945 if (*p == '\'')
17946 {
17947 /* skip over 'string' to avoid counting [ and ] inside it. */
17948 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
17949 ;
17950 if (*p == NUL)
17951 break;
17952 }
17953 else if (*p == '"')
17954 {
17955 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17956 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
17957 if (*p == '\\' && p[1] != NUL)
17958 ++p;
17959 if (*p == NUL)
17960 break;
17961 }
17962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017963 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017965 if (*p == '[')
17966 ++br_nest;
17967 else if (*p == ']')
17968 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000017970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017971 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017973 if (*p == '{')
17974 {
17975 mb_nest++;
17976 if (expr_start != NULL && *expr_start == NULL)
17977 *expr_start = p;
17978 }
17979 else if (*p == '}')
17980 {
17981 mb_nest--;
17982 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
17983 *expr_end = p;
17984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986 }
17987
17988 return p;
17989}
17990
17991/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017992 * Expands out the 'magic' {}'s in a variable/function name.
17993 * Note that this can call itself recursively, to deal with
17994 * constructs like foo{bar}{baz}{bam}
17995 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17996 * "in_start" ^
17997 * "expr_start" ^
17998 * "expr_end" ^
17999 * "in_end" ^
18000 *
18001 * Returns a new allocated string, which the caller must free.
18002 * Returns NULL for failure.
18003 */
18004 static char_u *
18005make_expanded_name(in_start, expr_start, expr_end, in_end)
18006 char_u *in_start;
18007 char_u *expr_start;
18008 char_u *expr_end;
18009 char_u *in_end;
18010{
18011 char_u c1;
18012 char_u *retval = NULL;
18013 char_u *temp_result;
18014 char_u *nextcmd = NULL;
18015
18016 if (expr_end == NULL || in_end == NULL)
18017 return NULL;
18018 *expr_start = NUL;
18019 *expr_end = NUL;
18020 c1 = *in_end;
18021 *in_end = NUL;
18022
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018023 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018024 if (temp_result != NULL && nextcmd == NULL)
18025 {
18026 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18027 + (in_end - expr_end) + 1));
18028 if (retval != NULL)
18029 {
18030 STRCPY(retval, in_start);
18031 STRCAT(retval, temp_result);
18032 STRCAT(retval, expr_end + 1);
18033 }
18034 }
18035 vim_free(temp_result);
18036
18037 *in_end = c1; /* put char back for error messages */
18038 *expr_start = '{';
18039 *expr_end = '}';
18040
18041 if (retval != NULL)
18042 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018043 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018044 if (expr_start != NULL)
18045 {
18046 /* Further expansion! */
18047 temp_result = make_expanded_name(retval, expr_start,
18048 expr_end, temp_result);
18049 vim_free(retval);
18050 retval = temp_result;
18051 }
18052 }
18053
18054 return retval;
18055}
18056
18057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018059 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 */
18061 static int
18062eval_isnamec(c)
18063 int c;
18064{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018065 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18066}
18067
18068/*
18069 * Return TRUE if character "c" can be used as the first character in a
18070 * variable or function name (excluding '{' and '}').
18071 */
18072 static int
18073eval_isnamec1(c)
18074 int c;
18075{
18076 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077}
18078
18079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 * Set number v: variable to "val".
18081 */
18082 void
18083set_vim_var_nr(idx, val)
18084 int idx;
18085 long val;
18086{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018087 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088}
18089
18090/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018091 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 */
18093 long
18094get_vim_var_nr(idx)
18095 int idx;
18096{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018097 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098}
18099
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018100/*
18101 * Get string v: variable value. Uses a static buffer, can only be used once.
18102 */
18103 char_u *
18104get_vim_var_str(idx)
18105 int idx;
18106{
18107 return get_tv_string(&vimvars[idx].vv_tv);
18108}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018109
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018111 * Get List v: variable value. Caller must take care of reference count when
18112 * needed.
18113 */
18114 list_T *
18115get_vim_var_list(idx)
18116 int idx;
18117{
18118 return vimvars[idx].vv_list;
18119}
18120
18121/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018122 * Set v:char to character "c".
18123 */
18124 void
18125set_vim_var_char(c)
18126 int c;
18127{
18128#ifdef FEAT_MBYTE
18129 char_u buf[MB_MAXBYTES];
18130#else
18131 char_u buf[2];
18132#endif
18133
18134#ifdef FEAT_MBYTE
18135 if (has_mbyte)
18136 buf[(*mb_char2bytes)(c, buf)] = NUL;
18137 else
18138#endif
18139 {
18140 buf[0] = c;
18141 buf[1] = NUL;
18142 }
18143 set_vim_var_string(VV_CHAR, buf, -1);
18144}
18145
18146/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018147 * Set v:count to "count" and v:count1 to "count1".
18148 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 */
18150 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018151set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152 long count;
18153 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018154 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018156 if (set_prevcount)
18157 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018158 vimvars[VV_COUNT].vv_nr = count;
18159 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160}
18161
18162/*
18163 * Set string v: variable to a copy of "val".
18164 */
18165 void
18166set_vim_var_string(idx, val, len)
18167 int idx;
18168 char_u *val;
18169 int len; /* length of "val" to use or -1 (whole string) */
18170{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018171 /* Need to do this (at least) once, since we can't initialize a union.
18172 * Will always be invoked when "v:progname" is set. */
18173 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18174
Bram Moolenaare9a41262005-01-15 22:18:47 +000018175 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018177 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018179 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018181 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182}
18183
18184/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018185 * Set List v: variable to "val".
18186 */
18187 void
18188set_vim_var_list(idx, val)
18189 int idx;
18190 list_T *val;
18191{
18192 list_unref(vimvars[idx].vv_list);
18193 vimvars[idx].vv_list = val;
18194 if (val != NULL)
18195 ++val->lv_refcount;
18196}
18197
18198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 * Set v:register if needed.
18200 */
18201 void
18202set_reg_var(c)
18203 int c;
18204{
18205 char_u regname;
18206
18207 if (c == 0 || c == ' ')
18208 regname = '"';
18209 else
18210 regname = c;
18211 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018212 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213 set_vim_var_string(VV_REG, &regname, 1);
18214}
18215
18216/*
18217 * Get or set v:exception. If "oldval" == NULL, return the current value.
18218 * Otherwise, restore the value to "oldval" and return NULL.
18219 * Must always be called in pairs to save and restore v:exception! Does not
18220 * take care of memory allocations.
18221 */
18222 char_u *
18223v_exception(oldval)
18224 char_u *oldval;
18225{
18226 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018227 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228
Bram Moolenaare9a41262005-01-15 22:18:47 +000018229 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 return NULL;
18231}
18232
18233/*
18234 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18235 * Otherwise, restore the value to "oldval" and return NULL.
18236 * Must always be called in pairs to save and restore v:throwpoint! Does not
18237 * take care of memory allocations.
18238 */
18239 char_u *
18240v_throwpoint(oldval)
18241 char_u *oldval;
18242{
18243 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018244 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245
Bram Moolenaare9a41262005-01-15 22:18:47 +000018246 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 return NULL;
18248}
18249
18250#if defined(FEAT_AUTOCMD) || defined(PROTO)
18251/*
18252 * Set v:cmdarg.
18253 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18254 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18255 * Must always be called in pairs!
18256 */
18257 char_u *
18258set_cmdarg(eap, oldarg)
18259 exarg_T *eap;
18260 char_u *oldarg;
18261{
18262 char_u *oldval;
18263 char_u *newval;
18264 unsigned len;
18265
Bram Moolenaare9a41262005-01-15 22:18:47 +000018266 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018267 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018269 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018270 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018271 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 }
18273
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018274 if (eap->force_bin == FORCE_BIN)
18275 len = 6;
18276 else if (eap->force_bin == FORCE_NOBIN)
18277 len = 8;
18278 else
18279 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018280
18281 if (eap->read_edit)
18282 len += 7;
18283
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018284 if (eap->force_ff != 0)
18285 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18286# ifdef FEAT_MBYTE
18287 if (eap->force_enc != 0)
18288 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018289 if (eap->bad_char != 0)
18290 len += (unsigned)STRLEN(eap->cmd + eap->bad_char) + 7;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018291# endif
18292
18293 newval = alloc(len + 1);
18294 if (newval == NULL)
18295 return NULL;
18296
18297 if (eap->force_bin == FORCE_BIN)
18298 sprintf((char *)newval, " ++bin");
18299 else if (eap->force_bin == FORCE_NOBIN)
18300 sprintf((char *)newval, " ++nobin");
18301 else
18302 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018303
18304 if (eap->read_edit)
18305 STRCAT(newval, " ++edit");
18306
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018307 if (eap->force_ff != 0)
18308 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18309 eap->cmd + eap->force_ff);
18310# ifdef FEAT_MBYTE
18311 if (eap->force_enc != 0)
18312 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18313 eap->cmd + eap->force_enc);
Bram Moolenaarb2c2efa2005-12-13 20:09:08 +000018314 if (eap->bad_char != 0)
18315 sprintf((char *)newval + STRLEN(newval), " ++bad=%s",
18316 eap->cmd + eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018317# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018318 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018319 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320}
18321#endif
18322
18323/*
18324 * Get the value of internal variable "name".
18325 * Return OK or FAIL.
18326 */
18327 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018328get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329 char_u *name;
18330 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018331 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018332 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333{
18334 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018335 typval_T *tv = NULL;
18336 typval_T atv;
18337 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339
18340 /* truncate the name, so that we can use strcmp() */
18341 cc = name[len];
18342 name[len] = NUL;
18343
18344 /*
18345 * Check for "b:changedtick".
18346 */
18347 if (STRCMP(name, "b:changedtick") == 0)
18348 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018349 atv.v_type = VAR_NUMBER;
18350 atv.vval.v_number = curbuf->b_changedtick;
18351 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352 }
18353
18354 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355 * Check for user-defined variables.
18356 */
18357 else
18358 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018359 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018361 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 }
18363
Bram Moolenaare9a41262005-01-15 22:18:47 +000018364 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018366 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018367 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 ret = FAIL;
18369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018370 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018371 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372
18373 name[len] = cc;
18374
18375 return ret;
18376}
18377
18378/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018379 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18380 * Also handle function call with Funcref variable: func(expr)
18381 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18382 */
18383 static int
18384handle_subscript(arg, rettv, evaluate, verbose)
18385 char_u **arg;
18386 typval_T *rettv;
18387 int evaluate; /* do more than finding the end */
18388 int verbose; /* give error messages */
18389{
18390 int ret = OK;
18391 dict_T *selfdict = NULL;
18392 char_u *s;
18393 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018394 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018395
18396 while (ret == OK
18397 && (**arg == '['
18398 || (**arg == '.' && rettv->v_type == VAR_DICT)
18399 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18400 && !vim_iswhite(*(*arg - 1)))
18401 {
18402 if (**arg == '(')
18403 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018404 /* need to copy the funcref so that we can clear rettv */
18405 functv = *rettv;
18406 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018407
18408 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018409 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018410 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018411 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18412 &len, evaluate, selfdict);
18413
18414 /* Clear the funcref afterwards, so that deleting it while
18415 * evaluating the arguments is possible (see test55). */
18416 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018417
18418 /* Stop the expression evaluation when immediately aborting on
18419 * error, or when an interrupt occurred or an exception was thrown
18420 * but not caught. */
18421 if (aborting())
18422 {
18423 if (ret == OK)
18424 clear_tv(rettv);
18425 ret = FAIL;
18426 }
18427 dict_unref(selfdict);
18428 selfdict = NULL;
18429 }
18430 else /* **arg == '[' || **arg == '.' */
18431 {
18432 dict_unref(selfdict);
18433 if (rettv->v_type == VAR_DICT)
18434 {
18435 selfdict = rettv->vval.v_dict;
18436 if (selfdict != NULL)
18437 ++selfdict->dv_refcount;
18438 }
18439 else
18440 selfdict = NULL;
18441 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18442 {
18443 clear_tv(rettv);
18444 ret = FAIL;
18445 }
18446 }
18447 }
18448 dict_unref(selfdict);
18449 return ret;
18450}
18451
18452/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018453 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018454 * value).
18455 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018456 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018457alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018458{
Bram Moolenaar33570922005-01-25 22:26:29 +000018459 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018460}
18461
18462/*
18463 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 * The string "s" must have been allocated, it is consumed.
18465 * Return NULL for out of memory, the variable otherwise.
18466 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018467 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018468alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469 char_u *s;
18470{
Bram Moolenaar33570922005-01-25 22:26:29 +000018471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018473 rettv = alloc_tv();
18474 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018476 rettv->v_type = VAR_STRING;
18477 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 }
18479 else
18480 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018481 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482}
18483
18484/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018485 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018487 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018488free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018489 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490{
18491 if (varp != NULL)
18492 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018493 switch (varp->v_type)
18494 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018495 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018496 func_unref(varp->vval.v_string);
18497 /*FALLTHROUGH*/
18498 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018499 vim_free(varp->vval.v_string);
18500 break;
18501 case VAR_LIST:
18502 list_unref(varp->vval.v_list);
18503 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018504 case VAR_DICT:
18505 dict_unref(varp->vval.v_dict);
18506 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018507 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018508#ifdef FEAT_FLOAT
18509 case VAR_FLOAT:
18510#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018511 case VAR_UNKNOWN:
18512 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018513 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018514 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018515 break;
18516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 vim_free(varp);
18518 }
18519}
18520
18521/*
18522 * Free the memory for a variable value and set the value to NULL or 0.
18523 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018524 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018525clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018526 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527{
18528 if (varp != NULL)
18529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018530 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018532 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018533 func_unref(varp->vval.v_string);
18534 /*FALLTHROUGH*/
18535 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018536 vim_free(varp->vval.v_string);
18537 varp->vval.v_string = NULL;
18538 break;
18539 case VAR_LIST:
18540 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018541 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018542 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018543 case VAR_DICT:
18544 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018545 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018546 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018547 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018548 varp->vval.v_number = 0;
18549 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018550#ifdef FEAT_FLOAT
18551 case VAR_FLOAT:
18552 varp->vval.v_float = 0.0;
18553 break;
18554#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018555 case VAR_UNKNOWN:
18556 break;
18557 default:
18558 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018560 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 }
18562}
18563
18564/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018565 * Set the value of a variable to NULL without freeing items.
18566 */
18567 static void
18568init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018569 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018570{
18571 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018572 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018573}
18574
18575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 * Get the number value of a variable.
18577 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018578 * For incompatible types, return 0.
18579 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18580 * caller of incompatible types: it sets *denote to TRUE if "denote"
18581 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 */
18583 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018584get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018585 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018587 int error = FALSE;
18588
18589 return get_tv_number_chk(varp, &error); /* return 0L on error */
18590}
18591
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018592 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018593get_tv_number_chk(varp, denote)
18594 typval_T *varp;
18595 int *denote;
18596{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018597 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018599 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018601 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018602 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018603#ifdef FEAT_FLOAT
18604 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018605 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018606 break;
18607#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018608 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018609 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018610 break;
18611 case VAR_STRING:
18612 if (varp->vval.v_string != NULL)
18613 vim_str2nr(varp->vval.v_string, NULL, NULL,
18614 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018615 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018616 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018617 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018618 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018619 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000018620 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018621 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018622 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018623 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018624 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018626 if (denote == NULL) /* useful for values that must be unsigned */
18627 n = -1;
18628 else
18629 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018630 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631}
18632
18633/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018634 * Get the lnum from the first argument.
18635 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018636 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637 */
18638 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018639get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000018640 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641{
Bram Moolenaar33570922005-01-25 22:26:29 +000018642 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 linenr_T lnum;
18644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018645 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 if (lnum == 0) /* no valid number, try using line() */
18647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018648 rettv.v_type = VAR_NUMBER;
18649 f_line(argvars, &rettv);
18650 lnum = rettv.vval.v_number;
18651 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 }
18653 return lnum;
18654}
18655
18656/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000018657 * Get the lnum from the first argument.
18658 * Also accepts "$", then "buf" is used.
18659 * Returns 0 on error.
18660 */
18661 static linenr_T
18662get_tv_lnum_buf(argvars, buf)
18663 typval_T *argvars;
18664 buf_T *buf;
18665{
18666 if (argvars[0].v_type == VAR_STRING
18667 && argvars[0].vval.v_string != NULL
18668 && argvars[0].vval.v_string[0] == '$'
18669 && buf != NULL)
18670 return buf->b_ml.ml_line_count;
18671 return get_tv_number_chk(&argvars[0], NULL);
18672}
18673
18674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 * Get the string value of a variable.
18676 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000018677 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18678 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 * If the String variable has never been set, return an empty string.
18680 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018681 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18682 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683 */
18684 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018685get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018686 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687{
18688 static char_u mybuf[NUMBUFLEN];
18689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018690 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691}
18692
18693 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018694get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000018695 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018696 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018698 char_u *res = get_tv_string_buf_chk(varp, buf);
18699
18700 return res != NULL ? res : (char_u *)"";
18701}
18702
Bram Moolenaar4be06f92005-07-29 22:36:03 +000018703 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018704get_tv_string_chk(varp)
18705 typval_T *varp;
18706{
18707 static char_u mybuf[NUMBUFLEN];
18708
18709 return get_tv_string_buf_chk(varp, mybuf);
18710}
18711
18712 static char_u *
18713get_tv_string_buf_chk(varp, buf)
18714 typval_T *varp;
18715 char_u *buf;
18716{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018717 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018719 case VAR_NUMBER:
18720 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
18721 return buf;
18722 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018723 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018724 break;
18725 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018726 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000018727 break;
18728 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018729 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018730 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018731#ifdef FEAT_FLOAT
18732 case VAR_FLOAT:
18733 EMSG(_("E806: using Float as a String"));
18734 break;
18735#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018736 case VAR_STRING:
18737 if (varp->vval.v_string != NULL)
18738 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018739 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018740 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018741 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018742 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018744 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745}
18746
18747/*
18748 * Find variable "name" in the list of variables.
18749 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018750 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018751 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000018752 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018754 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018755find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018757 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018760 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761
Bram Moolenaara7043832005-01-21 11:56:39 +000018762 ht = find_var_ht(name, &varname);
18763 if (htp != NULL)
18764 *htp = ht;
18765 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018767 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768}
18769
18770/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018771 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000018772 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018774 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018775find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000018776 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000018777 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018778 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000018779{
Bram Moolenaar33570922005-01-25 22:26:29 +000018780 hashitem_T *hi;
18781
18782 if (*varname == NUL)
18783 {
18784 /* Must be something like "s:", otherwise "ht" would be NULL. */
18785 switch (varname[-2])
18786 {
18787 case 's': return &SCRIPT_SV(current_SID).sv_var;
18788 case 'g': return &globvars_var;
18789 case 'v': return &vimvars_var;
18790 case 'b': return &curbuf->b_bufvar;
18791 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018792#ifdef FEAT_WINDOWS
18793 case 't': return &curtab->tp_winvar;
18794#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018795 case 'l': return current_funccal == NULL
18796 ? NULL : &current_funccal->l_vars_var;
18797 case 'a': return current_funccal == NULL
18798 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000018799 }
18800 return NULL;
18801 }
Bram Moolenaara7043832005-01-21 11:56:39 +000018802
18803 hi = hash_find(ht, varname);
18804 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018805 {
18806 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018807 * worked find the variable again. Don't auto-load a script if it was
18808 * loaded already, otherwise it would be loaded every time when
18809 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018810 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018811 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018812 hi = hash_find(ht, varname);
18813 if (HASHITEM_EMPTY(hi))
18814 return NULL;
18815 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018816 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000018817}
18818
18819/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018820 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000018821 * Set "varname" to the start of name without ':'.
18822 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018823 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000018824find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 char_u *name;
18826 char_u **varname;
18827{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018828 hashitem_T *hi;
18829
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830 if (name[1] != ':')
18831 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018832 /* The name must not start with a colon or #. */
18833 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834 return NULL;
18835 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018836
18837 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018838 hi = hash_find(&compat_hashtab, name);
18839 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000018840 return &compat_hashtab;
18841
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018843 return &globvarht; /* global variable */
18844 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 }
18846 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018847 if (*name == 'g') /* global variable */
18848 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018849 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18850 */
18851 if (vim_strchr(name + 2, ':') != NULL
18852 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018853 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018855 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000018857 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018858#ifdef FEAT_WINDOWS
18859 if (*name == 't') /* tab page variable */
18860 return &curtab->tp_vars.dv_hashtab;
18861#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000018862 if (*name == 'v') /* v: variable */
18863 return &vimvarht;
18864 if (*name == 'a' && current_funccal != NULL) /* function argument */
18865 return &current_funccal->l_avars.dv_hashtab;
18866 if (*name == 'l' && current_funccal != NULL) /* local function variable */
18867 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868 if (*name == 's' /* script variable */
18869 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
18870 return &SCRIPT_VARS(current_SID);
18871 return NULL;
18872}
18873
18874/*
18875 * Get the string value of a (global/local) variable.
18876 * Returns NULL when it doesn't exist.
18877 */
18878 char_u *
18879get_var_value(name)
18880 char_u *name;
18881{
Bram Moolenaar33570922005-01-25 22:26:29 +000018882 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883
Bram Moolenaara7043832005-01-21 11:56:39 +000018884 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885 if (v == NULL)
18886 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000018887 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888}
18889
18890/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018891 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 * sourcing this script and when executing functions defined in the script.
18893 */
18894 void
18895new_script_vars(id)
18896 scid_T id;
18897{
Bram Moolenaara7043832005-01-21 11:56:39 +000018898 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000018899 hashtab_T *ht;
18900 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000018901
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
18903 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018904 /* Re-allocating ga_data means that an ht_array pointing to
18905 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000018906 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000018907 for (i = 1; i <= ga_scripts.ga_len; ++i)
18908 {
18909 ht = &SCRIPT_VARS(i);
18910 if (ht->ht_mask == HT_INIT_SIZE - 1)
18911 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000018912 sv = &SCRIPT_SV(i);
18913 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000018914 }
18915
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 while (ga_scripts.ga_len < id)
18917 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018918 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
18919 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 }
18922 }
18923}
18924
18925/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018926 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18927 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 */
18929 void
Bram Moolenaar33570922005-01-25 22:26:29 +000018930init_var_dict(dict, dict_var)
18931 dict_T *dict;
18932 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933{
Bram Moolenaar33570922005-01-25 22:26:29 +000018934 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000018935 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000018936 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018937 dict_var->di_tv.vval.v_dict = dict;
18938 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018939 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018940 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18941 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942}
18943
18944/*
18945 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000018946 * Frees all allocated variables and the value they contain.
18947 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 */
18949 void
Bram Moolenaara7043832005-01-21 11:56:39 +000018950vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000018951 hashtab_T *ht;
18952{
18953 vars_clear_ext(ht, TRUE);
18954}
18955
18956/*
18957 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18958 */
18959 static void
18960vars_clear_ext(ht, free_val)
18961 hashtab_T *ht;
18962 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963{
Bram Moolenaara7043832005-01-21 11:56:39 +000018964 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018965 hashitem_T *hi;
18966 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967
Bram Moolenaar33570922005-01-25 22:26:29 +000018968 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018969 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000018970 for (hi = ht->ht_array; todo > 0; ++hi)
18971 {
18972 if (!HASHITEM_EMPTY(hi))
18973 {
18974 --todo;
18975
Bram Moolenaar33570922005-01-25 22:26:29 +000018976 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000018977 * ht_array might change then. hash_clear() takes care of it
18978 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018979 v = HI2DI(hi);
18980 if (free_val)
18981 clear_tv(&v->di_tv);
18982 if ((v->di_flags & DI_FLAGS_FIX) == 0)
18983 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000018984 }
18985 }
18986 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018987 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988}
18989
Bram Moolenaara7043832005-01-21 11:56:39 +000018990/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018991 * Delete a variable from hashtab "ht" at item "hi".
18992 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000018993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000018995delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000018996 hashtab_T *ht;
18997 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998{
Bram Moolenaar33570922005-01-25 22:26:29 +000018999 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019000
19001 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019002 clear_tv(&di->di_tv);
19003 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004}
19005
19006/*
19007 * List the value of one internal variable.
19008 */
19009 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019010list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019011 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019013 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019015 char_u *tofree;
19016 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019017 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019018
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019019 current_copyID += COPYID_INC;
19020 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019021 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019022 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019023 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024}
19025
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019027list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 char_u *prefix;
19029 char_u *name;
19030 int type;
19031 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019032 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033{
Bram Moolenaar31859182007-08-14 20:41:13 +000019034 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19035 msg_start();
19036 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037 if (name != NULL) /* "a:" vars don't have a name stored */
19038 msg_puts(name);
19039 msg_putchar(' ');
19040 msg_advance(22);
19041 if (type == VAR_NUMBER)
19042 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019043 else if (type == VAR_FUNC)
19044 msg_putchar('*');
19045 else if (type == VAR_LIST)
19046 {
19047 msg_putchar('[');
19048 if (*string == '[')
19049 ++string;
19050 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019051 else if (type == VAR_DICT)
19052 {
19053 msg_putchar('{');
19054 if (*string == '{')
19055 ++string;
19056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 else
19058 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019059
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019061
19062 if (type == VAR_FUNC)
19063 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019064 if (*first)
19065 {
19066 msg_clr_eos();
19067 *first = FALSE;
19068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069}
19070
19071/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019072 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 * If the variable already exists, the value is updated.
19074 * Otherwise the variable is created.
19075 */
19076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019077set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019079 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019080 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081{
Bram Moolenaar33570922005-01-25 22:26:29 +000019082 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019084 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019085 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019087 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019088 {
19089 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19090 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19091 ? name[2] : name[0]))
19092 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019093 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019094 return;
19095 }
19096 if (function_exists(name))
19097 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019098 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019099 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019100 return;
19101 }
19102 }
19103
Bram Moolenaara7043832005-01-21 11:56:39 +000019104 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019105 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000019106 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000019107 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000019108 return;
19109 }
19110
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019111 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000019112 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019114 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019115 if (var_check_ro(v->di_flags, name)
19116 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019117 return;
19118 if (v->di_tv.v_type != tv->v_type
19119 && !((v->di_tv.v_type == VAR_STRING
19120 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019121 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019122 || tv->v_type == VAR_NUMBER))
19123#ifdef FEAT_FLOAT
19124 && !((v->di_tv.v_type == VAR_NUMBER
19125 || v->di_tv.v_type == VAR_FLOAT)
19126 && (tv->v_type == VAR_NUMBER
19127 || tv->v_type == VAR_FLOAT))
19128#endif
19129 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019130 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019131 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019132 return;
19133 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019134
19135 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019136 * Handle setting internal v: variables separately: we don't change
19137 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019138 */
19139 if (ht == &vimvarht)
19140 {
19141 if (v->di_tv.v_type == VAR_STRING)
19142 {
19143 vim_free(v->di_tv.vval.v_string);
19144 if (copy || tv->v_type != VAR_STRING)
19145 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19146 else
19147 {
19148 /* Take over the string to avoid an extra alloc/free. */
19149 v->di_tv.vval.v_string = tv->vval.v_string;
19150 tv->vval.v_string = NULL;
19151 }
19152 }
19153 else if (v->di_tv.v_type != VAR_NUMBER)
19154 EMSG2(_(e_intern2), "set_var()");
19155 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019156 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019157 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019158 if (STRCMP(varname, "searchforward") == 0)
19159 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19160 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019161 return;
19162 }
19163
19164 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165 }
19166 else /* add a new variable */
19167 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019168 /* Can't add "v:" variable. */
19169 if (ht == &vimvarht)
19170 {
19171 EMSG2(_(e_illvar), name);
19172 return;
19173 }
19174
Bram Moolenaar92124a32005-06-17 22:03:40 +000019175 /* Make sure the variable name is valid. */
19176 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019177 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19178 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019179 {
19180 EMSG2(_(e_illvar), varname);
19181 return;
19182 }
19183
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019184 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19185 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019186 if (v == NULL)
19187 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019188 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019189 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019191 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192 return;
19193 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019194 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019196
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019197 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019198 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019199 else
19200 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019201 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019202 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019203 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205}
19206
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019207/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019208 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019209 * Also give an error message.
19210 */
19211 static int
19212var_check_ro(flags, name)
19213 int flags;
19214 char_u *name;
19215{
19216 if (flags & DI_FLAGS_RO)
19217 {
19218 EMSG2(_(e_readonlyvar), name);
19219 return TRUE;
19220 }
19221 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19222 {
19223 EMSG2(_(e_readonlysbx), name);
19224 return TRUE;
19225 }
19226 return FALSE;
19227}
19228
19229/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019230 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19231 * Also give an error message.
19232 */
19233 static int
19234var_check_fixed(flags, name)
19235 int flags;
19236 char_u *name;
19237{
19238 if (flags & DI_FLAGS_FIX)
19239 {
19240 EMSG2(_("E795: Cannot delete variable %s"), name);
19241 return TRUE;
19242 }
19243 return FALSE;
19244}
19245
19246/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019247 * Return TRUE if typeval "tv" is set to be locked (immutable).
19248 * Also give an error message, using "name".
19249 */
19250 static int
19251tv_check_lock(lock, name)
19252 int lock;
19253 char_u *name;
19254{
19255 if (lock & VAR_LOCKED)
19256 {
19257 EMSG2(_("E741: Value is locked: %s"),
19258 name == NULL ? (char_u *)_("Unknown") : name);
19259 return TRUE;
19260 }
19261 if (lock & VAR_FIXED)
19262 {
19263 EMSG2(_("E742: Cannot change value of %s"),
19264 name == NULL ? (char_u *)_("Unknown") : name);
19265 return TRUE;
19266 }
19267 return FALSE;
19268}
19269
19270/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019271 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019272 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019273 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019274 * It is OK for "from" and "to" to point to the same item. This is used to
19275 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019278copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019279 typval_T *from;
19280 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019282 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019283 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019284 switch (from->v_type)
19285 {
19286 case VAR_NUMBER:
19287 to->vval.v_number = from->vval.v_number;
19288 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019289#ifdef FEAT_FLOAT
19290 case VAR_FLOAT:
19291 to->vval.v_float = from->vval.v_float;
19292 break;
19293#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019294 case VAR_STRING:
19295 case VAR_FUNC:
19296 if (from->vval.v_string == NULL)
19297 to->vval.v_string = NULL;
19298 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019299 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019300 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019301 if (from->v_type == VAR_FUNC)
19302 func_ref(to->vval.v_string);
19303 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019304 break;
19305 case VAR_LIST:
19306 if (from->vval.v_list == NULL)
19307 to->vval.v_list = NULL;
19308 else
19309 {
19310 to->vval.v_list = from->vval.v_list;
19311 ++to->vval.v_list->lv_refcount;
19312 }
19313 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019314 case VAR_DICT:
19315 if (from->vval.v_dict == NULL)
19316 to->vval.v_dict = NULL;
19317 else
19318 {
19319 to->vval.v_dict = from->vval.v_dict;
19320 ++to->vval.v_dict->dv_refcount;
19321 }
19322 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019323 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019324 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019325 break;
19326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327}
19328
19329/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019330 * Make a copy of an item.
19331 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019332 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19333 * reference to an already copied list/dict can be used.
19334 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019335 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019336 static int
19337item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019338 typval_T *from;
19339 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019340 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019341 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019342{
19343 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019344 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019345
Bram Moolenaar33570922005-01-25 22:26:29 +000019346 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019347 {
19348 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019349 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019350 }
19351 ++recurse;
19352
19353 switch (from->v_type)
19354 {
19355 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019356#ifdef FEAT_FLOAT
19357 case VAR_FLOAT:
19358#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019359 case VAR_STRING:
19360 case VAR_FUNC:
19361 copy_tv(from, to);
19362 break;
19363 case VAR_LIST:
19364 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019365 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019366 if (from->vval.v_list == NULL)
19367 to->vval.v_list = NULL;
19368 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19369 {
19370 /* use the copy made earlier */
19371 to->vval.v_list = from->vval.v_list->lv_copylist;
19372 ++to->vval.v_list->lv_refcount;
19373 }
19374 else
19375 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19376 if (to->vval.v_list == NULL)
19377 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019378 break;
19379 case VAR_DICT:
19380 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019381 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019382 if (from->vval.v_dict == NULL)
19383 to->vval.v_dict = NULL;
19384 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19385 {
19386 /* use the copy made earlier */
19387 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19388 ++to->vval.v_dict->dv_refcount;
19389 }
19390 else
19391 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19392 if (to->vval.v_dict == NULL)
19393 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019394 break;
19395 default:
19396 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019397 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019398 }
19399 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019400 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019401}
19402
19403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 * ":echo expr1 ..." print each argument separated with a space, add a
19405 * newline at the end.
19406 * ":echon expr1 ..." print each argument plain.
19407 */
19408 void
19409ex_echo(eap)
19410 exarg_T *eap;
19411{
19412 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019413 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019414 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415 char_u *p;
19416 int needclr = TRUE;
19417 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019418 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419
19420 if (eap->skip)
19421 ++emsg_skip;
19422 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19423 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019424 /* If eval1() causes an error message the text from the command may
19425 * still need to be cleared. E.g., "echo 22,44". */
19426 need_clr_eos = needclr;
19427
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019429 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 {
19431 /*
19432 * Report the invalid expression unless the expression evaluation
19433 * has been cancelled due to an aborting error, an interrupt, or an
19434 * exception.
19435 */
19436 if (!aborting())
19437 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019438 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 break;
19440 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019441 need_clr_eos = FALSE;
19442
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 if (!eap->skip)
19444 {
19445 if (atstart)
19446 {
19447 atstart = FALSE;
19448 /* Call msg_start() after eval1(), evaluating the expression
19449 * may cause a message to appear. */
19450 if (eap->cmdidx == CMD_echo)
19451 msg_start();
19452 }
19453 else if (eap->cmdidx == CMD_echo)
19454 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019455 current_copyID += COPYID_INC;
19456 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019457 if (p != NULL)
19458 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019460 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019462 if (*p != TAB && needclr)
19463 {
19464 /* remove any text still there from the command */
19465 msg_clr_eos();
19466 needclr = FALSE;
19467 }
19468 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 }
19470 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019471 {
19472#ifdef FEAT_MBYTE
19473 if (has_mbyte)
19474 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019475 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019476
19477 (void)msg_outtrans_len_attr(p, i, echo_attr);
19478 p += i - 1;
19479 }
19480 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019482 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019485 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019487 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 arg = skipwhite(arg);
19489 }
19490 eap->nextcmd = check_nextcmd(arg);
19491
19492 if (eap->skip)
19493 --emsg_skip;
19494 else
19495 {
19496 /* remove text that may still be there from the command */
19497 if (needclr)
19498 msg_clr_eos();
19499 if (eap->cmdidx == CMD_echo)
19500 msg_end();
19501 }
19502}
19503
19504/*
19505 * ":echohl {name}".
19506 */
19507 void
19508ex_echohl(eap)
19509 exarg_T *eap;
19510{
19511 int id;
19512
19513 id = syn_name2id(eap->arg);
19514 if (id == 0)
19515 echo_attr = 0;
19516 else
19517 echo_attr = syn_id2attr(id);
19518}
19519
19520/*
19521 * ":execute expr1 ..." execute the result of an expression.
19522 * ":echomsg expr1 ..." Print a message
19523 * ":echoerr expr1 ..." Print an error
19524 * Each gets spaces around each argument and a newline at the end for
19525 * echo commands
19526 */
19527 void
19528ex_execute(eap)
19529 exarg_T *eap;
19530{
19531 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019532 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 int ret = OK;
19534 char_u *p;
19535 garray_T ga;
19536 int len;
19537 int save_did_emsg;
19538
19539 ga_init2(&ga, 1, 80);
19540
19541 if (eap->skip)
19542 ++emsg_skip;
19543 while (*arg != NUL && *arg != '|' && *arg != '\n')
19544 {
19545 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019546 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547 {
19548 /*
19549 * Report the invalid expression unless the expression evaluation
19550 * has been cancelled due to an aborting error, an interrupt, or an
19551 * exception.
19552 */
19553 if (!aborting())
19554 EMSG2(_(e_invexpr2), p);
19555 ret = FAIL;
19556 break;
19557 }
19558
19559 if (!eap->skip)
19560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019561 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562 len = (int)STRLEN(p);
19563 if (ga_grow(&ga, len + 2) == FAIL)
19564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019565 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566 ret = FAIL;
19567 break;
19568 }
19569 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 ga.ga_len += len;
19573 }
19574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019575 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 arg = skipwhite(arg);
19577 }
19578
19579 if (ret != FAIL && ga.ga_data != NULL)
19580 {
19581 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000019582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000019584 out_flush();
19585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 else if (eap->cmdidx == CMD_echoerr)
19587 {
19588 /* We don't want to abort following commands, restore did_emsg. */
19589 save_did_emsg = did_emsg;
19590 EMSG((char_u *)ga.ga_data);
19591 if (!force_abort)
19592 did_emsg = save_did_emsg;
19593 }
19594 else if (eap->cmdidx == CMD_execute)
19595 do_cmdline((char_u *)ga.ga_data,
19596 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
19597 }
19598
19599 ga_clear(&ga);
19600
19601 if (eap->skip)
19602 --emsg_skip;
19603
19604 eap->nextcmd = check_nextcmd(arg);
19605}
19606
19607/*
19608 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19609 * "arg" points to the "&" or '+' when called, to "option" when returning.
19610 * Returns NULL when no option name found. Otherwise pointer to the char
19611 * after the option name.
19612 */
19613 static char_u *
19614find_option_end(arg, opt_flags)
19615 char_u **arg;
19616 int *opt_flags;
19617{
19618 char_u *p = *arg;
19619
19620 ++p;
19621 if (*p == 'g' && p[1] == ':')
19622 {
19623 *opt_flags = OPT_GLOBAL;
19624 p += 2;
19625 }
19626 else if (*p == 'l' && p[1] == ':')
19627 {
19628 *opt_flags = OPT_LOCAL;
19629 p += 2;
19630 }
19631 else
19632 *opt_flags = 0;
19633
19634 if (!ASCII_ISALPHA(*p))
19635 return NULL;
19636 *arg = p;
19637
19638 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
19639 p += 4; /* termcap option */
19640 else
19641 while (ASCII_ISALPHA(*p))
19642 ++p;
19643 return p;
19644}
19645
19646/*
19647 * ":function"
19648 */
19649 void
19650ex_function(eap)
19651 exarg_T *eap;
19652{
19653 char_u *theline;
19654 int j;
19655 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657 char_u *name = NULL;
19658 char_u *p;
19659 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019660 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661 garray_T newargs;
19662 garray_T newlines;
19663 int varargs = FALSE;
19664 int mustend = FALSE;
19665 int flags = 0;
19666 ufunc_T *fp;
19667 int indent;
19668 int nesting;
19669 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 dictitem_T *v;
19671 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019672 static int func_nr = 0; /* number for nameless function */
19673 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019674 hashtab_T *ht;
19675 int todo;
19676 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019677 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678
19679 /*
19680 * ":function" without argument: list functions.
19681 */
19682 if (ends_excmd(*eap->arg))
19683 {
19684 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019685 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019686 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000019687 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019688 {
19689 if (!HASHITEM_EMPTY(hi))
19690 {
19691 --todo;
19692 fp = HI2UF(hi);
19693 if (!isdigit(*fp->uf_name))
19694 list_func_head(fp, FALSE);
19695 }
19696 }
19697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 eap->nextcmd = check_nextcmd(eap->arg);
19699 return;
19700 }
19701
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019702 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019703 * ":function /pat": list functions matching pattern.
19704 */
19705 if (*eap->arg == '/')
19706 {
19707 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
19708 if (!eap->skip)
19709 {
19710 regmatch_T regmatch;
19711
19712 c = *p;
19713 *p = NUL;
19714 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
19715 *p = c;
19716 if (regmatch.regprog != NULL)
19717 {
19718 regmatch.rm_ic = p_ic;
19719
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019720 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019721 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
19722 {
19723 if (!HASHITEM_EMPTY(hi))
19724 {
19725 --todo;
19726 fp = HI2UF(hi);
19727 if (!isdigit(*fp->uf_name)
19728 && vim_regexec(&regmatch, fp->uf_name, 0))
19729 list_func_head(fp, FALSE);
19730 }
19731 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000019732 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000019733 }
19734 }
19735 if (*p == '/')
19736 ++p;
19737 eap->nextcmd = check_nextcmd(p);
19738 return;
19739 }
19740
19741 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019742 * Get the function name. There are these situations:
19743 * func normal function name
19744 * "name" == func, "fudi.fd_dict" == NULL
19745 * dict.func new dictionary entry
19746 * "name" == NULL, "fudi.fd_dict" set,
19747 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19748 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019749 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019750 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19751 * dict.func existing dict entry that's not a Funcref
19752 * "name" == NULL, "fudi.fd_dict" set,
19753 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019756 name = trans_function_name(&p, eap->skip, 0, &fudi);
19757 paren = (vim_strchr(p, '(') != NULL);
19758 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 {
19760 /*
19761 * Return on an invalid expression in braces, unless the expression
19762 * evaluation has been cancelled due to an aborting error, an
19763 * interrupt, or an exception.
19764 */
19765 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019766 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019767 if (!eap->skip && fudi.fd_newkey != NULL)
19768 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019769 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 else
19773 eap->skip = TRUE;
19774 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000019775
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 /* An error in a function call during evaluation of an expression in magic
19777 * braces should not cause the function not to be defined. */
19778 saved_did_emsg = did_emsg;
19779 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780
19781 /*
19782 * ":function func" with only function name: list function.
19783 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019784 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785 {
19786 if (!ends_excmd(*skipwhite(p)))
19787 {
19788 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019789 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 }
19791 eap->nextcmd = check_nextcmd(p);
19792 if (eap->nextcmd != NULL)
19793 *p = NUL;
19794 if (!eap->skip && !got_int)
19795 {
19796 fp = find_func(name);
19797 if (fp != NULL)
19798 {
19799 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019800 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019802 if (FUNCLINE(fp, j) == NULL)
19803 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 msg_putchar('\n');
19805 msg_outnum((long)(j + 1));
19806 if (j < 9)
19807 msg_putchar(' ');
19808 if (j < 99)
19809 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019810 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811 out_flush(); /* show a line at a time */
19812 ui_breakcheck();
19813 }
19814 if (!got_int)
19815 {
19816 msg_putchar('\n');
19817 msg_puts((char_u *)" endfunction");
19818 }
19819 }
19820 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000019821 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019823 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824 }
19825
19826 /*
19827 * ":function name(arg1, arg2)" Define function.
19828 */
19829 p = skipwhite(p);
19830 if (*p != '(')
19831 {
19832 if (!eap->skip)
19833 {
19834 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019835 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 }
19837 /* attempt to continue by skipping some text */
19838 if (vim_strchr(p, '(') != NULL)
19839 p = vim_strchr(p, '(');
19840 }
19841 p = skipwhite(p + 1);
19842
19843 ga_init2(&newargs, (int)sizeof(char_u *), 3);
19844 ga_init2(&newlines, (int)sizeof(char_u *), 3);
19845
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019846 if (!eap->skip)
19847 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019848 /* Check the name of the function. Unless it's a dictionary function
19849 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019850 if (name != NULL)
19851 arg = name;
19852 else
19853 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000019854 if (arg != NULL && (fudi.fd_di == NULL
19855 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019856 {
19857 if (*arg == K_SPECIAL)
19858 j = 3;
19859 else
19860 j = 0;
19861 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
19862 : eval_isnamec(arg[j])))
19863 ++j;
19864 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000019865 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019866 }
19867 }
19868
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869 /*
19870 * Isolate the arguments: "arg1, arg2, ...)"
19871 */
19872 while (*p != ')')
19873 {
19874 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
19875 {
19876 varargs = TRUE;
19877 p += 3;
19878 mustend = TRUE;
19879 }
19880 else
19881 {
19882 arg = p;
19883 while (ASCII_ISALNUM(*p) || *p == '_')
19884 ++p;
19885 if (arg == p || isdigit(*arg)
19886 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
19887 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
19888 {
19889 if (!eap->skip)
19890 EMSG2(_("E125: Illegal argument: %s"), arg);
19891 break;
19892 }
19893 if (ga_grow(&newargs, 1) == FAIL)
19894 goto erret;
19895 c = *p;
19896 *p = NUL;
19897 arg = vim_strsave(arg);
19898 if (arg == NULL)
19899 goto erret;
19900 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
19901 *p = c;
19902 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903 if (*p == ',')
19904 ++p;
19905 else
19906 mustend = TRUE;
19907 }
19908 p = skipwhite(p);
19909 if (mustend && *p != ')')
19910 {
19911 if (!eap->skip)
19912 EMSG2(_(e_invarg2), eap->arg);
19913 break;
19914 }
19915 }
19916 ++p; /* skip the ')' */
19917
Bram Moolenaare9a41262005-01-15 22:18:47 +000019918 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 for (;;)
19920 {
19921 p = skipwhite(p);
19922 if (STRNCMP(p, "range", 5) == 0)
19923 {
19924 flags |= FC_RANGE;
19925 p += 5;
19926 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000019927 else if (STRNCMP(p, "dict", 4) == 0)
19928 {
19929 flags |= FC_DICT;
19930 p += 4;
19931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 else if (STRNCMP(p, "abort", 5) == 0)
19933 {
19934 flags |= FC_ABORT;
19935 p += 5;
19936 }
19937 else
19938 break;
19939 }
19940
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019941 /* When there is a line break use what follows for the function body.
19942 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19943 if (*p == '\n')
19944 line_arg = p + 1;
19945 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 EMSG(_(e_trailing));
19947
19948 /*
19949 * Read the body of the function, until ":endfunction" is found.
19950 */
19951 if (KeyTyped)
19952 {
19953 /* Check if the function already exists, don't let the user type the
19954 * whole function before telling him it doesn't work! For a script we
19955 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019956 if (!eap->skip && !eap->forceit)
19957 {
19958 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
19959 EMSG(_(e_funcdict));
19960 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019961 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019964 if (!eap->skip && did_emsg)
19965 goto erret;
19966
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 msg_putchar('\n'); /* don't overwrite the function name */
19968 cmdline_row = msg_row;
19969 }
19970
19971 indent = 2;
19972 nesting = 0;
19973 for (;;)
19974 {
19975 msg_scroll = TRUE;
19976 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000019977 sourcing_lnum_off = sourcing_lnum;
19978
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000019979 if (line_arg != NULL)
19980 {
19981 /* Use eap->arg, split up in parts by line breaks. */
19982 theline = line_arg;
19983 p = vim_strchr(theline, '\n');
19984 if (p == NULL)
19985 line_arg += STRLEN(line_arg);
19986 else
19987 {
19988 *p = NUL;
19989 line_arg = p + 1;
19990 }
19991 }
19992 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 theline = getcmdline(':', 0L, indent);
19994 else
19995 theline = eap->getline(':', eap->cookie, indent);
19996 if (KeyTyped)
19997 lines_left = Rows - 1;
19998 if (theline == NULL)
19999 {
20000 EMSG(_("E126: Missing :endfunction"));
20001 goto erret;
20002 }
20003
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020004 /* Detect line continuation: sourcing_lnum increased more than one. */
20005 if (sourcing_lnum > sourcing_lnum_off + 1)
20006 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20007 else
20008 sourcing_lnum_off = 0;
20009
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010 if (skip_until != NULL)
20011 {
20012 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20013 * don't check for ":endfunc". */
20014 if (STRCMP(theline, skip_until) == 0)
20015 {
20016 vim_free(skip_until);
20017 skip_until = NULL;
20018 }
20019 }
20020 else
20021 {
20022 /* skip ':' and blanks*/
20023 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20024 ;
20025
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020026 /* Check for "endfunction". */
20027 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020029 if (line_arg == NULL)
20030 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 break;
20032 }
20033
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020034 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 * at "end". */
20036 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20037 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020038 else if (STRNCMP(p, "if", 2) == 0
20039 || STRNCMP(p, "wh", 2) == 0
20040 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 || STRNCMP(p, "try", 3) == 0)
20042 indent += 2;
20043
20044 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020045 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020047 if (*p == '!')
20048 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 p += eval_fname_script(p);
20050 if (ASCII_ISALPHA(*p))
20051 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020052 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053 if (*skipwhite(p) == '(')
20054 {
20055 ++nesting;
20056 indent += 2;
20057 }
20058 }
20059 }
20060
20061 /* Check for ":append" or ":insert". */
20062 p = skip_range(p, NULL);
20063 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20064 || (p[0] == 'i'
20065 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20066 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20067 skip_until = vim_strsave((char_u *)".");
20068
20069 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20070 arg = skipwhite(skiptowhite(p));
20071 if (arg[0] == '<' && arg[1] =='<'
20072 && ((p[0] == 'p' && p[1] == 'y'
20073 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20074 || (p[0] == 'p' && p[1] == 'e'
20075 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20076 || (p[0] == 't' && p[1] == 'c'
20077 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20078 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20079 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020080 || (p[0] == 'm' && p[1] == 'z'
20081 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 ))
20083 {
20084 /* ":python <<" continues until a dot, like ":append" */
20085 p = skipwhite(arg + 2);
20086 if (*p == NUL)
20087 skip_until = vim_strsave((char_u *)".");
20088 else
20089 skip_until = vim_strsave(p);
20090 }
20091 }
20092
20093 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020094 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020095 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020096 if (line_arg == NULL)
20097 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020099 }
20100
20101 /* Copy the line to newly allocated memory. get_one_sourceline()
20102 * allocates 250 bytes per line, this saves 80% on average. The cost
20103 * is an extra alloc/free. */
20104 p = vim_strsave(theline);
20105 if (p != NULL)
20106 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020107 if (line_arg == NULL)
20108 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020109 theline = p;
20110 }
20111
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020112 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20113
20114 /* Add NULL lines for continuation lines, so that the line count is
20115 * equal to the index in the growarray. */
20116 while (sourcing_lnum_off-- > 0)
20117 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020118
20119 /* Check for end of eap->arg. */
20120 if (line_arg != NULL && *line_arg == NUL)
20121 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 }
20123
20124 /* Don't define the function when skipping commands or when an error was
20125 * detected. */
20126 if (eap->skip || did_emsg)
20127 goto erret;
20128
20129 /*
20130 * If there are no errors, add the function
20131 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020132 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020133 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020134 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020135 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020136 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020137 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020138 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020139 goto erret;
20140 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020141
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020142 fp = find_func(name);
20143 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020145 if (!eap->forceit)
20146 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020147 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020148 goto erret;
20149 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020150 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020151 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020152 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020153 name);
20154 goto erret;
20155 }
20156 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020157 ga_clear_strings(&(fp->uf_args));
20158 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020159 vim_free(name);
20160 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 }
20163 else
20164 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020165 char numbuf[20];
20166
20167 fp = NULL;
20168 if (fudi.fd_newkey == NULL && !eap->forceit)
20169 {
20170 EMSG(_(e_funcdict));
20171 goto erret;
20172 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020173 if (fudi.fd_di == NULL)
20174 {
20175 /* Can't add a function to a locked dictionary */
20176 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20177 goto erret;
20178 }
20179 /* Can't change an existing function if it is locked */
20180 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20181 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020182
20183 /* Give the function a sequential number. Can only be used with a
20184 * Funcref! */
20185 vim_free(name);
20186 sprintf(numbuf, "%d", ++func_nr);
20187 name = vim_strsave((char_u *)numbuf);
20188 if (name == NULL)
20189 goto erret;
20190 }
20191
20192 if (fp == NULL)
20193 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020194 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020195 {
20196 int slen, plen;
20197 char_u *scriptname;
20198
20199 /* Check that the autoload name matches the script name. */
20200 j = FAIL;
20201 if (sourcing_name != NULL)
20202 {
20203 scriptname = autoload_name(name);
20204 if (scriptname != NULL)
20205 {
20206 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020207 plen = (int)STRLEN(p);
20208 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020209 if (slen > plen && fnamecmp(p,
20210 sourcing_name + slen - plen) == 0)
20211 j = OK;
20212 vim_free(scriptname);
20213 }
20214 }
20215 if (j == FAIL)
20216 {
20217 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20218 goto erret;
20219 }
20220 }
20221
20222 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 if (fp == NULL)
20224 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020225
20226 if (fudi.fd_dict != NULL)
20227 {
20228 if (fudi.fd_di == NULL)
20229 {
20230 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020231 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020232 if (fudi.fd_di == NULL)
20233 {
20234 vim_free(fp);
20235 goto erret;
20236 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020237 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20238 {
20239 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020240 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020241 goto erret;
20242 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020243 }
20244 else
20245 /* overwrite existing dict entry */
20246 clear_tv(&fudi.fd_di->di_tv);
20247 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020248 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020249 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020250 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020251
20252 /* behave like "dict" was used */
20253 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020254 }
20255
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020257 STRCPY(fp->uf_name, name);
20258 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020260 fp->uf_args = newargs;
20261 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020262#ifdef FEAT_PROFILE
20263 fp->uf_tml_count = NULL;
20264 fp->uf_tml_total = NULL;
20265 fp->uf_tml_self = NULL;
20266 fp->uf_profiling = FALSE;
20267 if (prof_def_func())
20268 func_do_profile(fp);
20269#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020270 fp->uf_varargs = varargs;
20271 fp->uf_flags = flags;
20272 fp->uf_calls = 0;
20273 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020274 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275
20276erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 ga_clear_strings(&newargs);
20278 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020279ret_free:
20280 vim_free(skip_until);
20281 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284}
20285
20286/*
20287 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020288 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020290 * flags:
20291 * TFN_INT: internal function name OK
20292 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 * Advances "pp" to just after the function name (if no error).
20294 */
20295 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020296trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 char_u **pp;
20298 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020299 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020300 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020302 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 char_u *start;
20304 char_u *end;
20305 int lead;
20306 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020308 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020309
20310 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020311 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020313
20314 /* Check for hard coded <SNR>: already translated function ID (from a user
20315 * command). */
20316 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20317 && (*pp)[2] == (int)KE_SNR)
20318 {
20319 *pp += 3;
20320 len = get_id_len(pp) + 3;
20321 return vim_strnsave(start, len);
20322 }
20323
20324 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20325 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020327 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020329
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020330 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20331 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020332 if (end == start)
20333 {
20334 if (!skip)
20335 EMSG(_("E129: Function name required"));
20336 goto theend;
20337 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020338 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020339 {
20340 /*
20341 * Report an invalid expression in braces, unless the expression
20342 * evaluation has been cancelled due to an aborting error, an
20343 * interrupt, or an exception.
20344 */
20345 if (!aborting())
20346 {
20347 if (end != NULL)
20348 EMSG2(_(e_invarg2), start);
20349 }
20350 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020351 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020352 goto theend;
20353 }
20354
20355 if (lv.ll_tv != NULL)
20356 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020357 if (fdp != NULL)
20358 {
20359 fdp->fd_dict = lv.ll_dict;
20360 fdp->fd_newkey = lv.ll_newkey;
20361 lv.ll_newkey = NULL;
20362 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020363 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020364 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20365 {
20366 name = vim_strsave(lv.ll_tv->vval.v_string);
20367 *pp = end;
20368 }
20369 else
20370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020371 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20372 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020373 EMSG(_(e_funcref));
20374 else
20375 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020376 name = NULL;
20377 }
20378 goto theend;
20379 }
20380
20381 if (lv.ll_name == NULL)
20382 {
20383 /* Error found, but continue after the function name. */
20384 *pp = end;
20385 goto theend;
20386 }
20387
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020388 /* Check if the name is a Funcref. If so, use the value. */
20389 if (lv.ll_exp_name != NULL)
20390 {
20391 len = (int)STRLEN(lv.ll_exp_name);
20392 name = deref_func_name(lv.ll_exp_name, &len);
20393 if (name == lv.ll_exp_name)
20394 name = NULL;
20395 }
20396 else
20397 {
20398 len = (int)(end - *pp);
20399 name = deref_func_name(*pp, &len);
20400 if (name == *pp)
20401 name = NULL;
20402 }
20403 if (name != NULL)
20404 {
20405 name = vim_strsave(name);
20406 *pp = end;
20407 goto theend;
20408 }
20409
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020410 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020411 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020412 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020413 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20414 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20415 {
20416 /* When there was "s:" already or the name expanded to get a
20417 * leading "s:" then remove it. */
20418 lv.ll_name += 2;
20419 len -= 2;
20420 lead = 2;
20421 }
20422 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020423 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020424 {
20425 if (lead == 2) /* skip over "s:" */
20426 lv.ll_name += 2;
20427 len = (int)(end - lv.ll_name);
20428 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020429
20430 /*
20431 * Copy the function name to allocated memory.
20432 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20433 * Accept <SNR>123_name() outside a script.
20434 */
20435 if (skip)
20436 lead = 0; /* do nothing */
20437 else if (lead > 0)
20438 {
20439 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020440 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20441 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020442 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020443 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020444 if (current_SID <= 0)
20445 {
20446 EMSG(_(e_usingsid));
20447 goto theend;
20448 }
20449 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20450 lead += (int)STRLEN(sid_buf);
20451 }
20452 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020453 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020454 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020455 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020456 goto theend;
20457 }
20458 name = alloc((unsigned)(len + lead + 1));
20459 if (name != NULL)
20460 {
20461 if (lead > 0)
20462 {
20463 name[0] = K_SPECIAL;
20464 name[1] = KS_EXTRA;
20465 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020466 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020467 STRCPY(name + 3, sid_buf);
20468 }
20469 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20470 name[len + lead] = NUL;
20471 }
20472 *pp = end;
20473
20474theend:
20475 clear_lval(&lv);
20476 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477}
20478
20479/*
20480 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20481 * Return 2 if "p" starts with "s:".
20482 * Return 0 otherwise.
20483 */
20484 static int
20485eval_fname_script(p)
20486 char_u *p;
20487{
20488 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20489 || STRNICMP(p + 1, "SNR>", 4) == 0))
20490 return 5;
20491 if (p[0] == 's' && p[1] == ':')
20492 return 2;
20493 return 0;
20494}
20495
20496/*
20497 * Return TRUE if "p" starts with "<SID>" or "s:".
20498 * Only works if eval_fname_script() returned non-zero for "p"!
20499 */
20500 static int
20501eval_fname_sid(p)
20502 char_u *p;
20503{
20504 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20505}
20506
20507/*
20508 * List the head of the function: "name(arg1, arg2)".
20509 */
20510 static void
20511list_func_head(fp, indent)
20512 ufunc_T *fp;
20513 int indent;
20514{
20515 int j;
20516
20517 msg_start();
20518 if (indent)
20519 MSG_PUTS(" ");
20520 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020521 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 {
20523 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020524 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 }
20526 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020527 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020529 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530 {
20531 if (j)
20532 MSG_PUTS(", ");
20533 msg_puts(FUNCARG(fp, j));
20534 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020535 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 {
20537 if (j)
20538 MSG_PUTS(", ");
20539 MSG_PUTS("...");
20540 }
20541 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000020542 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000020543 if (p_verbose > 0)
20544 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545}
20546
20547/*
20548 * Find a function by name, return pointer to it in ufuncs.
20549 * Return NULL for unknown function.
20550 */
20551 static ufunc_T *
20552find_func(name)
20553 char_u *name;
20554{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020555 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020557 hi = hash_find(&func_hashtab, name);
20558 if (!HASHITEM_EMPTY(hi))
20559 return HI2UF(hi);
20560 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561}
20562
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020563#if defined(EXITFREE) || defined(PROTO)
20564 void
20565free_all_functions()
20566{
20567 hashitem_T *hi;
20568
20569 /* Need to start all over every time, because func_free() may change the
20570 * hash table. */
20571 while (func_hashtab.ht_used > 0)
20572 for (hi = func_hashtab.ht_array; ; ++hi)
20573 if (!HASHITEM_EMPTY(hi))
20574 {
20575 func_free(HI2UF(hi));
20576 break;
20577 }
20578}
20579#endif
20580
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020581/*
20582 * Return TRUE if a function "name" exists.
20583 */
20584 static int
20585function_exists(name)
20586 char_u *name;
20587{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020588 char_u *nm = name;
20589 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020590 int n = FALSE;
20591
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000020592 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000020593 nm = skipwhite(nm);
20594
20595 /* Only accept "funcname", "funcname ", "funcname (..." and
20596 * "funcname(...", not "funcname!...". */
20597 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020598 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020599 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020600 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020601 else
20602 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020603 }
Bram Moolenaar79783442006-05-05 21:18:03 +000020604 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020605 return n;
20606}
20607
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020608/*
20609 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020610 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020611 */
20612 static int
20613builtin_function(name)
20614 char_u *name;
20615{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020616 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
20617 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020618}
20619
Bram Moolenaar05159a02005-02-26 23:04:13 +000020620#if defined(FEAT_PROFILE) || defined(PROTO)
20621/*
20622 * Start profiling function "fp".
20623 */
20624 static void
20625func_do_profile(fp)
20626 ufunc_T *fp;
20627{
20628 fp->uf_tm_count = 0;
20629 profile_zero(&fp->uf_tm_self);
20630 profile_zero(&fp->uf_tm_total);
20631 if (fp->uf_tml_count == NULL)
20632 fp->uf_tml_count = (int *)alloc_clear((unsigned)
20633 (sizeof(int) * fp->uf_lines.ga_len));
20634 if (fp->uf_tml_total == NULL)
20635 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
20636 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20637 if (fp->uf_tml_self == NULL)
20638 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
20639 (sizeof(proftime_T) * fp->uf_lines.ga_len));
20640 fp->uf_tml_idx = -1;
20641 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
20642 || fp->uf_tml_self == NULL)
20643 return; /* out of memory */
20644
20645 fp->uf_profiling = TRUE;
20646}
20647
20648/*
20649 * Dump the profiling results for all functions in file "fd".
20650 */
20651 void
20652func_dump_profile(fd)
20653 FILE *fd;
20654{
20655 hashitem_T *hi;
20656 int todo;
20657 ufunc_T *fp;
20658 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000020659 ufunc_T **sorttab;
20660 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020661
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020662 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020663 if (todo == 0)
20664 return; /* nothing to dump */
20665
Bram Moolenaar73830342005-02-28 22:48:19 +000020666 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
20667
Bram Moolenaar05159a02005-02-26 23:04:13 +000020668 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
20669 {
20670 if (!HASHITEM_EMPTY(hi))
20671 {
20672 --todo;
20673 fp = HI2UF(hi);
20674 if (fp->uf_profiling)
20675 {
Bram Moolenaar73830342005-02-28 22:48:19 +000020676 if (sorttab != NULL)
20677 sorttab[st_len++] = fp;
20678
Bram Moolenaar05159a02005-02-26 23:04:13 +000020679 if (fp->uf_name[0] == K_SPECIAL)
20680 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
20681 else
20682 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
20683 if (fp->uf_tm_count == 1)
20684 fprintf(fd, "Called 1 time\n");
20685 else
20686 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
20687 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
20688 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
20689 fprintf(fd, "\n");
20690 fprintf(fd, "count total (s) self (s)\n");
20691
20692 for (i = 0; i < fp->uf_lines.ga_len; ++i)
20693 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020694 if (FUNCLINE(fp, i) == NULL)
20695 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000020696 prof_func_line(fd, fp->uf_tml_count[i],
20697 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020698 fprintf(fd, "%s\n", FUNCLINE(fp, i));
20699 }
20700 fprintf(fd, "\n");
20701 }
20702 }
20703 }
Bram Moolenaar73830342005-02-28 22:48:19 +000020704
20705 if (sorttab != NULL && st_len > 0)
20706 {
20707 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20708 prof_total_cmp);
20709 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
20710 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
20711 prof_self_cmp);
20712 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
20713 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000020714
20715 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020716}
Bram Moolenaar73830342005-02-28 22:48:19 +000020717
20718 static void
20719prof_sort_list(fd, sorttab, st_len, title, prefer_self)
20720 FILE *fd;
20721 ufunc_T **sorttab;
20722 int st_len;
20723 char *title;
20724 int prefer_self; /* when equal print only self time */
20725{
20726 int i;
20727 ufunc_T *fp;
20728
20729 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
20730 fprintf(fd, "count total (s) self (s) function\n");
20731 for (i = 0; i < 20 && i < st_len; ++i)
20732 {
20733 fp = sorttab[i];
20734 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
20735 prefer_self);
20736 if (fp->uf_name[0] == K_SPECIAL)
20737 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
20738 else
20739 fprintf(fd, " %s()\n", fp->uf_name);
20740 }
20741 fprintf(fd, "\n");
20742}
20743
20744/*
20745 * Print the count and times for one function or function line.
20746 */
20747 static void
20748prof_func_line(fd, count, total, self, prefer_self)
20749 FILE *fd;
20750 int count;
20751 proftime_T *total;
20752 proftime_T *self;
20753 int prefer_self; /* when equal print only self time */
20754{
20755 if (count > 0)
20756 {
20757 fprintf(fd, "%5d ", count);
20758 if (prefer_self && profile_equal(total, self))
20759 fprintf(fd, " ");
20760 else
20761 fprintf(fd, "%s ", profile_msg(total));
20762 if (!prefer_self && profile_equal(total, self))
20763 fprintf(fd, " ");
20764 else
20765 fprintf(fd, "%s ", profile_msg(self));
20766 }
20767 else
20768 fprintf(fd, " ");
20769}
20770
20771/*
20772 * Compare function for total time sorting.
20773 */
20774 static int
20775#ifdef __BORLANDC__
20776_RTLENTRYF
20777#endif
20778prof_total_cmp(s1, s2)
20779 const void *s1;
20780 const void *s2;
20781{
20782 ufunc_T *p1, *p2;
20783
20784 p1 = *(ufunc_T **)s1;
20785 p2 = *(ufunc_T **)s2;
20786 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
20787}
20788
20789/*
20790 * Compare function for self time sorting.
20791 */
20792 static int
20793#ifdef __BORLANDC__
20794_RTLENTRYF
20795#endif
20796prof_self_cmp(s1, s2)
20797 const void *s1;
20798 const void *s2;
20799{
20800 ufunc_T *p1, *p2;
20801
20802 p1 = *(ufunc_T **)s1;
20803 p2 = *(ufunc_T **)s2;
20804 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
20805}
20806
Bram Moolenaar05159a02005-02-26 23:04:13 +000020807#endif
20808
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020809/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020810 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020811 * Return TRUE if a package was loaded.
20812 */
20813 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020814script_autoload(name, reload)
20815 char_u *name;
20816 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020817{
20818 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020819 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020820 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020821 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020822
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020823 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020824 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020825 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020826 return FALSE;
20827
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020828 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020829
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020830 /* Find the name in the list of previously loaded package names. Skip
20831 * "autoload/", it's always the same. */
20832 for (i = 0; i < ga_loaded.ga_len; ++i)
20833 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
20834 break;
20835 if (!reload && i < ga_loaded.ga_len)
20836 ret = FALSE; /* was loaded already */
20837 else
20838 {
20839 /* Remember the name if it wasn't loaded already. */
20840 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
20841 {
20842 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
20843 tofree = NULL;
20844 }
20845
20846 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000020847 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020848 ret = TRUE;
20849 }
20850
20851 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020852 return ret;
20853}
20854
20855/*
20856 * Return the autoload script name for a function or variable name.
20857 * Returns NULL when out of memory.
20858 */
20859 static char_u *
20860autoload_name(name)
20861 char_u *name;
20862{
20863 char_u *p;
20864 char_u *scriptname;
20865
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020866 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020867 scriptname = alloc((unsigned)(STRLEN(name) + 14));
20868 if (scriptname == NULL)
20869 return FALSE;
20870 STRCPY(scriptname, "autoload/");
20871 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020872 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020873 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020874 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020875 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020876 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020877}
20878
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20880
20881/*
20882 * Function given to ExpandGeneric() to obtain the list of user defined
20883 * function names.
20884 */
20885 char_u *
20886get_user_func_name(xp, idx)
20887 expand_T *xp;
20888 int idx;
20889{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020890 static long_u done;
20891 static hashitem_T *hi;
20892 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893
20894 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020896 done = 0;
20897 hi = func_hashtab.ht_array;
20898 }
20899 if (done < func_hashtab.ht_used)
20900 {
20901 if (done++ > 0)
20902 ++hi;
20903 while (HASHITEM_EMPTY(hi))
20904 ++hi;
20905 fp = HI2UF(hi);
20906
20907 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
20908 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909
20910 cat_func_name(IObuff, fp);
20911 if (xp->xp_context != EXPAND_USER_FUNC)
20912 {
20913 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020914 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 STRCAT(IObuff, ")");
20916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 return IObuff;
20918 }
20919 return NULL;
20920}
20921
20922#endif /* FEAT_CMDL_COMPL */
20923
20924/*
20925 * Copy the function name of "fp" to buffer "buf".
20926 * "buf" must be able to hold the function name plus three bytes.
20927 * Takes care of script-local function names.
20928 */
20929 static void
20930cat_func_name(buf, fp)
20931 char_u *buf;
20932 ufunc_T *fp;
20933{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020934 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935 {
20936 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020937 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 }
20939 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020940 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941}
20942
20943/*
20944 * ":delfunction {name}"
20945 */
20946 void
20947ex_delfunction(eap)
20948 exarg_T *eap;
20949{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020950 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951 char_u *p;
20952 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020953 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954
20955 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020956 name = trans_function_name(&p, eap->skip, 0, &fudi);
20957 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020959 {
20960 if (fudi.fd_dict != NULL && !eap->skip)
20961 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 if (!ends_excmd(*skipwhite(p)))
20965 {
20966 vim_free(name);
20967 EMSG(_(e_trailing));
20968 return;
20969 }
20970 eap->nextcmd = check_nextcmd(p);
20971 if (eap->nextcmd != NULL)
20972 *p = NUL;
20973
20974 if (!eap->skip)
20975 fp = find_func(name);
20976 vim_free(name);
20977
20978 if (!eap->skip)
20979 {
20980 if (fp == NULL)
20981 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000020982 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 return;
20984 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020985 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 {
20987 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
20988 return;
20989 }
20990
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020991 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020993 /* Delete the dict item that refers to the function, it will
20994 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020995 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020997 else
20998 func_free(fp);
20999 }
21000}
21001
21002/*
21003 * Free a function and remove it from the list of functions.
21004 */
21005 static void
21006func_free(fp)
21007 ufunc_T *fp;
21008{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021009 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021010
21011 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021012 ga_clear_strings(&(fp->uf_args));
21013 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021014#ifdef FEAT_PROFILE
21015 vim_free(fp->uf_tml_count);
21016 vim_free(fp->uf_tml_total);
21017 vim_free(fp->uf_tml_self);
21018#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021019
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021020 /* remove the function from the function hashtable */
21021 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21022 if (HASHITEM_EMPTY(hi))
21023 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021024 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021025 hash_remove(&func_hashtab, hi);
21026
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021027 vim_free(fp);
21028}
21029
21030/*
21031 * Unreference a Function: decrement the reference count and free it when it
21032 * becomes zero. Only for numbered functions.
21033 */
21034 static void
21035func_unref(name)
21036 char_u *name;
21037{
21038 ufunc_T *fp;
21039
21040 if (name != NULL && isdigit(*name))
21041 {
21042 fp = find_func(name);
21043 if (fp == NULL)
21044 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021045 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021046 {
21047 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021048 * when "uf_calls" becomes zero. */
21049 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021050 func_free(fp);
21051 }
21052 }
21053}
21054
21055/*
21056 * Count a reference to a Function.
21057 */
21058 static void
21059func_ref(name)
21060 char_u *name;
21061{
21062 ufunc_T *fp;
21063
21064 if (name != NULL && isdigit(*name))
21065 {
21066 fp = find_func(name);
21067 if (fp == NULL)
21068 EMSG2(_(e_intern2), "func_ref()");
21069 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021070 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 }
21072}
21073
21074/*
21075 * Call a user function.
21076 */
21077 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021078call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079 ufunc_T *fp; /* pointer to function */
21080 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021081 typval_T *argvars; /* arguments */
21082 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 linenr_T firstline; /* first line of range */
21084 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021085 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086{
Bram Moolenaar33570922005-01-25 22:26:29 +000021087 char_u *save_sourcing_name;
21088 linenr_T save_sourcing_lnum;
21089 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021090 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021091 int save_did_emsg;
21092 static int depth = 0;
21093 dictitem_T *v;
21094 int fixvar_idx = 0; /* index in fixvar[] */
21095 int i;
21096 int ai;
21097 char_u numbuf[NUMBUFLEN];
21098 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021099#ifdef FEAT_PROFILE
21100 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021101 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103
21104 /* If depth of calling is getting too high, don't execute the function */
21105 if (depth >= p_mfd)
21106 {
21107 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021108 rettv->v_type = VAR_NUMBER;
21109 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 return;
21111 }
21112 ++depth;
21113
21114 line_breakcheck(); /* check for CTRL-C hit */
21115
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021116 fc = (funccall_T *)alloc(sizeof(funccall_T));
21117 fc->caller = current_funccal;
21118 current_funccal = fc;
21119 fc->func = fp;
21120 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021121 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021122 fc->linenr = 0;
21123 fc->returned = FALSE;
21124 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021126 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21127 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128
Bram Moolenaar33570922005-01-25 22:26:29 +000021129 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021130 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021131 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21132 * each argument variable and saves a lot of time.
21133 */
21134 /*
21135 * Init l: variables.
21136 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021137 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021138 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021139 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021140 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21141 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021142 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021143 name = v->di_key;
21144 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021145 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021146 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021147 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021148 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021149 v->di_tv.vval.v_dict = selfdict;
21150 ++selfdict->dv_refcount;
21151 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021152
Bram Moolenaar33570922005-01-25 22:26:29 +000021153 /*
21154 * Init a: variables.
21155 * Set a:0 to "argcount".
21156 * Set a:000 to a list with room for the "..." arguments.
21157 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021158 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21159 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021160 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021161 /* Use "name" to avoid a warning from some compiler that checks the
21162 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021163 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021164 name = v->di_key;
21165 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021166 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021167 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021168 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021169 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021170 v->di_tv.vval.v_list = &fc->l_varlist;
21171 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21172 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21173 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021174
21175 /*
21176 * Set a:firstline to "firstline" and a:lastline to "lastline".
21177 * Set a:name to named arguments.
21178 * Set a:N to the "..." arguments.
21179 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021180 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021181 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021182 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021183 (varnumber_T)lastline);
21184 for (i = 0; i < argcount; ++i)
21185 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021186 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021187 if (ai < 0)
21188 /* named argument a:name */
21189 name = FUNCARG(fp, i);
21190 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021191 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021192 /* "..." argument a:1, a:2, etc. */
21193 sprintf((char *)numbuf, "%d", ai + 1);
21194 name = numbuf;
21195 }
21196 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21197 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021198 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021199 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21200 }
21201 else
21202 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021203 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21204 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021205 if (v == NULL)
21206 break;
21207 v->di_flags = DI_FLAGS_RO;
21208 }
21209 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021210 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021211
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021212 /* Note: the values are copied directly to avoid alloc/free.
21213 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021214 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021215 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021216
21217 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21218 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021219 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21220 fc->l_listitems[ai].li_tv = argvars[i];
21221 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021222 }
21223 }
21224
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225 /* Don't redraw while executing the function. */
21226 ++RedrawingDisabled;
21227 save_sourcing_name = sourcing_name;
21228 save_sourcing_lnum = sourcing_lnum;
21229 sourcing_lnum = 1;
21230 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021231 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 if (sourcing_name != NULL)
21233 {
21234 if (save_sourcing_name != NULL
21235 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21236 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21237 else
21238 STRCPY(sourcing_name, "function ");
21239 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21240
21241 if (p_verbose >= 12)
21242 {
21243 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021244 verbose_enter_scroll();
21245
Bram Moolenaar555b2802005-05-19 21:08:39 +000021246 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247 if (p_verbose >= 14)
21248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021250 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021251 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021252 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253
21254 msg_puts((char_u *)"(");
21255 for (i = 0; i < argcount; ++i)
21256 {
21257 if (i > 0)
21258 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021259 if (argvars[i].v_type == VAR_NUMBER)
21260 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 else
21262 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021263 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21264 if (s != NULL)
21265 {
21266 trunc_string(s, buf, MSG_BUF_CLEN);
21267 msg_puts(buf);
21268 vim_free(tofree);
21269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 }
21271 }
21272 msg_puts((char_u *)")");
21273 }
21274 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021275
21276 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277 --no_wait_return;
21278 }
21279 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021280#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021281 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021282 {
21283 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21284 func_do_profile(fp);
21285 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021286 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021287 {
21288 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021289 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021290 profile_zero(&fp->uf_tm_children);
21291 }
21292 script_prof_save(&wait_start);
21293 }
21294#endif
21295
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021297 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 save_did_emsg = did_emsg;
21299 did_emsg = FALSE;
21300
21301 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021302 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21304
21305 --RedrawingDisabled;
21306
21307 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021308 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021310 clear_tv(rettv);
21311 rettv->v_type = VAR_NUMBER;
21312 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 }
21314
Bram Moolenaar05159a02005-02-26 23:04:13 +000021315#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021316 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021317 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021318 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021319 profile_end(&call_start);
21320 profile_sub_wait(&wait_start, &call_start);
21321 profile_add(&fp->uf_tm_total, &call_start);
21322 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021323 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021324 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021325 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21326 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021327 }
21328 }
21329#endif
21330
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 /* when being verbose, mention the return value */
21332 if (p_verbose >= 12)
21333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021335 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021338 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021339 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021340 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021341 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021342 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021344 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021345 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021346 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021347 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021348
Bram Moolenaar555b2802005-05-19 21:08:39 +000021349 /* The value may be very long. Skip the middle part, so that we
21350 * have some idea how it starts and ends. smsg() would always
21351 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021352 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021353 if (s != NULL)
21354 {
21355 trunc_string(s, buf, MSG_BUF_CLEN);
21356 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21357 vim_free(tofree);
21358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 }
21360 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021361
21362 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 --no_wait_return;
21364 }
21365
21366 vim_free(sourcing_name);
21367 sourcing_name = save_sourcing_name;
21368 sourcing_lnum = save_sourcing_lnum;
21369 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021370#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021371 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021372 script_prof_restore(&wait_start);
21373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374
21375 if (p_verbose >= 12 && sourcing_name != NULL)
21376 {
21377 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021378 verbose_enter_scroll();
21379
Bram Moolenaar555b2802005-05-19 21:08:39 +000021380 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021382
21383 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 --no_wait_return;
21385 }
21386
21387 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021388 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021390
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021391 /* If the a:000 list and the l: and a: dicts are not referenced we can
21392 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021393 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21394 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21395 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21396 {
21397 free_funccal(fc, FALSE);
21398 }
21399 else
21400 {
21401 hashitem_T *hi;
21402 listitem_T *li;
21403 int todo;
21404
21405 /* "fc" is still in use. This can happen when returning "a:000" or
21406 * assigning "l:" to a global variable.
21407 * Link "fc" in the list for garbage collection later. */
21408 fc->caller = previous_funccal;
21409 previous_funccal = fc;
21410
21411 /* Make a copy of the a: variables, since we didn't do that above. */
21412 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21413 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21414 {
21415 if (!HASHITEM_EMPTY(hi))
21416 {
21417 --todo;
21418 v = HI2DI(hi);
21419 copy_tv(&v->di_tv, &v->di_tv);
21420 }
21421 }
21422
21423 /* Make a copy of the a:000 items, since we didn't do that above. */
21424 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21425 copy_tv(&li->li_tv, &li->li_tv);
21426 }
21427}
21428
21429/*
21430 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021431 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021432 */
21433 static int
21434can_free_funccal(fc, copyID)
21435 funccall_T *fc;
21436 int copyID;
21437{
21438 return (fc->l_varlist.lv_copyID != copyID
21439 && fc->l_vars.dv_copyID != copyID
21440 && fc->l_avars.dv_copyID != copyID);
21441}
21442
21443/*
21444 * Free "fc" and what it contains.
21445 */
21446 static void
21447free_funccal(fc, free_val)
21448 funccall_T *fc;
21449 int free_val; /* a: vars were allocated */
21450{
21451 listitem_T *li;
21452
21453 /* The a: variables typevals may not have been allocated, only free the
21454 * allocated variables. */
21455 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21456
21457 /* free all l: variables */
21458 vars_clear(&fc->l_vars.dv_hashtab);
21459
21460 /* Free the a:000 variables if they were allocated. */
21461 if (free_val)
21462 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21463 clear_tv(&li->li_tv);
21464
21465 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466}
21467
21468/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021469 * Add a number variable "name" to dict "dp" with value "nr".
21470 */
21471 static void
21472add_nr_var(dp, v, name, nr)
21473 dict_T *dp;
21474 dictitem_T *v;
21475 char *name;
21476 varnumber_T nr;
21477{
21478 STRCPY(v->di_key, name);
21479 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21480 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21481 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021482 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021483 v->di_tv.vval.v_number = nr;
21484}
21485
21486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487 * ":return [expr]"
21488 */
21489 void
21490ex_return(eap)
21491 exarg_T *eap;
21492{
21493 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021494 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 int returning = FALSE;
21496
21497 if (current_funccal == NULL)
21498 {
21499 EMSG(_("E133: :return not inside a function"));
21500 return;
21501 }
21502
21503 if (eap->skip)
21504 ++emsg_skip;
21505
21506 eap->nextcmd = NULL;
21507 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021508 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509 {
21510 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021511 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021513 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 }
21515 /* It's safer to return also on error. */
21516 else if (!eap->skip)
21517 {
21518 /*
21519 * Return unless the expression evaluation has been cancelled due to an
21520 * aborting error, an interrupt, or an exception.
21521 */
21522 if (!aborting())
21523 returning = do_return(eap, FALSE, TRUE, NULL);
21524 }
21525
21526 /* When skipping or the return gets pending, advance to the next command
21527 * in this line (!returning). Otherwise, ignore the rest of the line.
21528 * Following lines will be ignored by get_func_line(). */
21529 if (returning)
21530 eap->nextcmd = NULL;
21531 else if (eap->nextcmd == NULL) /* no argument */
21532 eap->nextcmd = check_nextcmd(arg);
21533
21534 if (eap->skip)
21535 --emsg_skip;
21536}
21537
21538/*
21539 * Return from a function. Possibly makes the return pending. Also called
21540 * for a pending return at the ":endtry" or after returning from an extra
21541 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000021542 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021543 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 * FALSE when the return gets pending.
21545 */
21546 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021547do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548 exarg_T *eap;
21549 int reanimate;
21550 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021551 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552{
21553 int idx;
21554 struct condstack *cstack = eap->cstack;
21555
21556 if (reanimate)
21557 /* Undo the return. */
21558 current_funccal->returned = FALSE;
21559
21560 /*
21561 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21562 * not in its finally clause (which then is to be executed next) is found.
21563 * In this case, make the ":return" pending for execution at the ":endtry".
21564 * Otherwise, return normally.
21565 */
21566 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
21567 if (idx >= 0)
21568 {
21569 cstack->cs_pending[idx] = CSTP_RETURN;
21570
21571 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021572 /* A pending return again gets pending. "rettv" points to an
21573 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021575 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 else
21577 {
21578 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021579 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021581 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021583 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584 {
21585 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021586 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021587 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 else
21589 EMSG(_(e_outofmem));
21590 }
21591 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021592 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593
21594 if (reanimate)
21595 {
21596 /* The pending return value could be overwritten by a ":return"
21597 * without argument in a finally clause; reset the default
21598 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021599 current_funccal->rettv->v_type = VAR_NUMBER;
21600 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 }
21602 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021603 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 }
21605 else
21606 {
21607 current_funccal->returned = TRUE;
21608
21609 /* If the return is carried out now, store the return value. For
21610 * a return immediately after reanimation, the value is already
21611 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021612 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021614 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000021615 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021617 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 }
21619 }
21620
21621 return idx < 0;
21622}
21623
21624/*
21625 * Free the variable with a pending return value.
21626 */
21627 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021628discard_pending_return(rettv)
21629 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630{
Bram Moolenaar33570922005-01-25 22:26:29 +000021631 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632}
21633
21634/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021635 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 * is an allocated string. Used by report_pending() for verbose messages.
21637 */
21638 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021639get_return_cmd(rettv)
21640 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021642 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021643 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021644 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021646 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021647 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021648 if (s == NULL)
21649 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021650
21651 STRCPY(IObuff, ":return ");
21652 STRNCPY(IObuff + 8, s, IOSIZE - 8);
21653 if (STRLEN(s) + 8 >= IOSIZE)
21654 STRCPY(IObuff + IOSIZE - 4, "...");
21655 vim_free(tofree);
21656 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657}
21658
21659/*
21660 * Get next function line.
21661 * Called by do_cmdline() to get the next line.
21662 * Returns allocated string, or NULL for end of function.
21663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664 char_u *
21665get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021666 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000021668 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669{
Bram Moolenaar33570922005-01-25 22:26:29 +000021670 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021671 ufunc_T *fp = fcp->func;
21672 char_u *retval;
21673 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674
21675 /* If breakpoints have been added/deleted need to check for it. */
21676 if (fcp->dbg_tick != debug_tick)
21677 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021678 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 sourcing_lnum);
21680 fcp->dbg_tick = debug_tick;
21681 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021682#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021683 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021684 func_line_end(cookie);
21685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686
Bram Moolenaar05159a02005-02-26 23:04:13 +000021687 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021688 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
21689 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 retval = NULL;
21691 else
21692 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021693 /* Skip NULL lines (continuation lines). */
21694 while (fcp->linenr < gap->ga_len
21695 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
21696 ++fcp->linenr;
21697 if (fcp->linenr >= gap->ga_len)
21698 retval = NULL;
21699 else
21700 {
21701 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
21702 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021703#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021704 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021705 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021706#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 }
21709
21710 /* Did we encounter a breakpoint? */
21711 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
21712 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021713 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021715 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 sourcing_lnum);
21717 fcp->dbg_tick = debug_tick;
21718 }
21719
21720 return retval;
21721}
21722
Bram Moolenaar05159a02005-02-26 23:04:13 +000021723#if defined(FEAT_PROFILE) || defined(PROTO)
21724/*
21725 * Called when starting to read a function line.
21726 * "sourcing_lnum" must be correct!
21727 * When skipping lines it may not actually be executed, but we won't find out
21728 * until later and we need to store the time now.
21729 */
21730 void
21731func_line_start(cookie)
21732 void *cookie;
21733{
21734 funccall_T *fcp = (funccall_T *)cookie;
21735 ufunc_T *fp = fcp->func;
21736
21737 if (fp->uf_profiling && sourcing_lnum >= 1
21738 && sourcing_lnum <= fp->uf_lines.ga_len)
21739 {
21740 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021741 /* Skip continuation lines. */
21742 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
21743 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021744 fp->uf_tml_execed = FALSE;
21745 profile_start(&fp->uf_tml_start);
21746 profile_zero(&fp->uf_tml_children);
21747 profile_get_wait(&fp->uf_tml_wait);
21748 }
21749}
21750
21751/*
21752 * Called when actually executing a function line.
21753 */
21754 void
21755func_line_exec(cookie)
21756 void *cookie;
21757{
21758 funccall_T *fcp = (funccall_T *)cookie;
21759 ufunc_T *fp = fcp->func;
21760
21761 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21762 fp->uf_tml_execed = TRUE;
21763}
21764
21765/*
21766 * Called when done with a function line.
21767 */
21768 void
21769func_line_end(cookie)
21770 void *cookie;
21771{
21772 funccall_T *fcp = (funccall_T *)cookie;
21773 ufunc_T *fp = fcp->func;
21774
21775 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
21776 {
21777 if (fp->uf_tml_execed)
21778 {
21779 ++fp->uf_tml_count[fp->uf_tml_idx];
21780 profile_end(&fp->uf_tml_start);
21781 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021782 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000021783 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
21784 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021785 }
21786 fp->uf_tml_idx = -1;
21787 }
21788}
21789#endif
21790
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791/*
21792 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021793 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 */
21795 int
21796func_has_ended(cookie)
21797 void *cookie;
21798{
Bram Moolenaar33570922005-01-25 22:26:29 +000021799 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800
21801 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21802 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021803 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 || fcp->returned);
21805}
21806
21807/*
21808 * return TRUE if cookie indicates a function which "abort"s on errors.
21809 */
21810 int
21811func_has_abort(cookie)
21812 void *cookie;
21813{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021814 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815}
21816
21817#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21818typedef enum
21819{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021820 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
21821 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
21822 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823} var_flavour_T;
21824
21825static var_flavour_T var_flavour __ARGS((char_u *varname));
21826
21827 static var_flavour_T
21828var_flavour(varname)
21829 char_u *varname;
21830{
21831 char_u *p = varname;
21832
21833 if (ASCII_ISUPPER(*p))
21834 {
21835 while (*(++p))
21836 if (ASCII_ISLOWER(*p))
21837 return VAR_FLAVOUR_SESSION;
21838 return VAR_FLAVOUR_VIMINFO;
21839 }
21840 else
21841 return VAR_FLAVOUR_DEFAULT;
21842}
21843#endif
21844
21845#if defined(FEAT_VIMINFO) || defined(PROTO)
21846/*
21847 * Restore global vars that start with a capital from the viminfo file
21848 */
21849 int
21850read_viminfo_varlist(virp, writing)
21851 vir_T *virp;
21852 int writing;
21853{
21854 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021855 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000021856 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857
21858 if (!writing && (find_viminfo_parameter('!') != NULL))
21859 {
21860 tab = vim_strchr(virp->vir_line + 1, '\t');
21861 if (tab != NULL)
21862 {
21863 *tab++ = '\0'; /* isolate the variable name */
21864 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021865 type = VAR_STRING;
21866#ifdef FEAT_FLOAT
21867 else if (*tab == 'F')
21868 type = VAR_FLOAT;
21869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870
21871 tab = vim_strchr(tab, '\t');
21872 if (tab != NULL)
21873 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021874 tv.v_type = type;
21875 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021876 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021878#ifdef FEAT_FLOAT
21879 else if (type == VAR_FLOAT)
21880 (void)string2float(tab + 1, &tv.vval.v_float);
21881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021883 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021884 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021885 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021886 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 }
21888 }
21889 }
21890
21891 return viminfo_readline(virp);
21892}
21893
21894/*
21895 * Write global vars that start with a capital to the viminfo file
21896 */
21897 void
21898write_viminfo_varlist(fp)
21899 FILE *fp;
21900{
Bram Moolenaar33570922005-01-25 22:26:29 +000021901 hashitem_T *hi;
21902 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021903 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021904 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021905 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021906 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021907 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908
21909 if (find_viminfo_parameter('!') == NULL)
21910 return;
21911
21912 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000021913
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021914 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021915 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021917 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021919 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021920 this_var = HI2DI(hi);
21921 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021922 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021923 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000021924 {
21925 case VAR_STRING: s = "STR"; break;
21926 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021927#ifdef FEAT_FLOAT
21928 case VAR_FLOAT: s = "FLO"; break;
21929#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000021930 default: continue;
21931 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021932 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000021933 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021934 if (p != NULL)
21935 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000021936 vim_free(tofree);
21937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 }
21939 }
21940}
21941#endif
21942
21943#if defined(FEAT_SESSION) || defined(PROTO)
21944 int
21945store_session_globals(fd)
21946 FILE *fd;
21947{
Bram Moolenaar33570922005-01-25 22:26:29 +000021948 hashitem_T *hi;
21949 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000021950 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 char_u *p, *t;
21952
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021953 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000021954 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021956 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021958 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021959 this_var = HI2DI(hi);
21960 if ((this_var->di_tv.v_type == VAR_NUMBER
21961 || this_var->di_tv.v_type == VAR_STRING)
21962 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000021963 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021964 /* Escape special characters with a backslash. Turn a LF and
21965 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021966 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000021967 (char_u *)"\\\"\n\r");
21968 if (p == NULL) /* out of memory */
21969 break;
21970 for (t = p; *t != NUL; ++t)
21971 if (*t == '\n')
21972 *t = 'n';
21973 else if (*t == '\r')
21974 *t = 'r';
21975 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000021976 this_var->di_key,
21977 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21978 : ' ',
21979 p,
21980 (this_var->di_tv.v_type == VAR_STRING) ? '"'
21981 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000021982 || put_eol(fd) == FAIL)
21983 {
21984 vim_free(p);
21985 return FAIL;
21986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 vim_free(p);
21988 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021989#ifdef FEAT_FLOAT
21990 else if (this_var->di_tv.v_type == VAR_FLOAT
21991 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
21992 {
21993 float_T f = this_var->di_tv.vval.v_float;
21994 int sign = ' ';
21995
21996 if (f < 0)
21997 {
21998 f = -f;
21999 sign = '-';
22000 }
22001 if ((fprintf(fd, "let %s = %c&%f",
22002 this_var->di_key, sign, f) < 0)
22003 || put_eol(fd) == FAIL)
22004 return FAIL;
22005 }
22006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007 }
22008 }
22009 return OK;
22010}
22011#endif
22012
Bram Moolenaar661b1822005-07-28 22:36:45 +000022013/*
22014 * Display script name where an item was last set.
22015 * Should only be invoked when 'verbose' is non-zero.
22016 */
22017 void
22018last_set_msg(scriptID)
22019 scid_T scriptID;
22020{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022021 char_u *p;
22022
Bram Moolenaar661b1822005-07-28 22:36:45 +000022023 if (scriptID != 0)
22024 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022025 p = home_replace_save(NULL, get_scriptname(scriptID));
22026 if (p != NULL)
22027 {
22028 verbose_enter();
22029 MSG_PUTS(_("\n\tLast set from "));
22030 MSG_PUTS(p);
22031 vim_free(p);
22032 verbose_leave();
22033 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022034 }
22035}
22036
Bram Moolenaard812df62008-11-09 12:46:09 +000022037/*
22038 * List v:oldfiles in a nice way.
22039 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022040 void
22041ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022042 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022043{
22044 list_T *l = vimvars[VV_OLDFILES].vv_list;
22045 listitem_T *li;
22046 int nr = 0;
22047
22048 if (l == NULL)
22049 msg((char_u *)_("No old files"));
22050 else
22051 {
22052 msg_start();
22053 msg_scroll = TRUE;
22054 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22055 {
22056 msg_outnum((long)++nr);
22057 MSG_PUTS(": ");
22058 msg_outtrans(get_tv_string(&li->li_tv));
22059 msg_putchar('\n');
22060 out_flush(); /* output one line at a time */
22061 ui_breakcheck();
22062 }
22063 /* Assume "got_int" was set to truncate the listing. */
22064 got_int = FALSE;
22065
22066#ifdef FEAT_BROWSE_CMD
22067 if (cmdmod.browse)
22068 {
22069 quit_more = FALSE;
22070 nr = prompt_for_number(FALSE);
22071 msg_starthere();
22072 if (nr > 0)
22073 {
22074 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22075 (long)nr);
22076
22077 if (p != NULL)
22078 {
22079 p = expand_env_save(p);
22080 eap->arg = p;
22081 eap->cmdidx = CMD_edit;
22082 cmdmod.browse = FALSE;
22083 do_exedit(eap, NULL);
22084 vim_free(p);
22085 }
22086 }
22087 }
22088#endif
22089 }
22090}
22091
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092#endif /* FEAT_EVAL */
22093
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022095#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096
22097#ifdef WIN3264
22098/*
22099 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22100 */
22101static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22102static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22103static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22104
22105/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022106 * Get the short path (8.3) for the filename in "fnamep".
22107 * Only works for a valid file name.
22108 * When the path gets longer "fnamep" is changed and the allocated buffer
22109 * is put in "bufp".
22110 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22111 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 */
22113 static int
22114get_short_pathname(fnamep, bufp, fnamelen)
22115 char_u **fnamep;
22116 char_u **bufp;
22117 int *fnamelen;
22118{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022119 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 char_u *newbuf;
22121
22122 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 l = GetShortPathName(*fnamep, *fnamep, len);
22124 if (l > len - 1)
22125 {
22126 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022127 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128 newbuf = vim_strnsave(*fnamep, l);
22129 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022130 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131
22132 vim_free(*bufp);
22133 *fnamep = *bufp = newbuf;
22134
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022135 /* Really should always succeed, as the buffer is big enough. */
22136 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 }
22138
22139 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022140 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141}
22142
22143/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022144 * Get the short path (8.3) for the filename in "fname". The converted
22145 * path is returned in "bufp".
22146 *
22147 * Some of the directories specified in "fname" may not exist. This function
22148 * will shorten the existing directories at the beginning of the path and then
22149 * append the remaining non-existing path.
22150 *
22151 * fname - Pointer to the filename to shorten. On return, contains the
22152 * pointer to the shortened pathname
22153 * bufp - Pointer to an allocated buffer for the filename.
22154 * fnamelen - Length of the filename pointed to by fname
22155 *
22156 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 */
22158 static int
22159shortpath_for_invalid_fname(fname, bufp, fnamelen)
22160 char_u **fname;
22161 char_u **bufp;
22162 int *fnamelen;
22163{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022164 char_u *short_fname, *save_fname, *pbuf_unused;
22165 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022167 int old_len, len;
22168 int new_len, sfx_len;
22169 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170
22171 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022172 old_len = *fnamelen;
22173 save_fname = vim_strnsave(*fname, old_len);
22174 pbuf_unused = NULL;
22175 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022177 endp = save_fname + old_len - 1; /* Find the end of the copy */
22178 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022180 /*
22181 * Try shortening the supplied path till it succeeds by removing one
22182 * directory at a time from the tail of the path.
22183 */
22184 len = 0;
22185 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022187 /* go back one path-separator */
22188 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22189 --endp;
22190 if (endp <= save_fname)
22191 break; /* processed the complete path */
22192
22193 /*
22194 * Replace the path separator with a NUL and try to shorten the
22195 * resulting path.
22196 */
22197 ch = *endp;
22198 *endp = 0;
22199 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022200 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022201 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22202 {
22203 retval = FAIL;
22204 goto theend;
22205 }
22206 *endp = ch; /* preserve the string */
22207
22208 if (len > 0)
22209 break; /* successfully shortened the path */
22210
22211 /* failed to shorten the path. Skip the path separator */
22212 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 }
22214
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022215 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022217 /*
22218 * Succeeded in shortening the path. Now concatenate the shortened
22219 * path with the remaining path at the tail.
22220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022222 /* Compute the length of the new path. */
22223 sfx_len = (int)(save_endp - endp) + 1;
22224 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022226 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022228 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022230 /* There is not enough space in the currently allocated string,
22231 * copy it to a buffer big enough. */
22232 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022234 {
22235 retval = FAIL;
22236 goto theend;
22237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 }
22239 else
22240 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022241 /* Transfer short_fname to the main buffer (it's big enough),
22242 * unless get_short_pathname() did its work in-place. */
22243 *fname = *bufp = save_fname;
22244 if (short_fname != save_fname)
22245 vim_strncpy(save_fname, short_fname, len);
22246 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022248
22249 /* concat the not-shortened part of the path */
22250 vim_strncpy(*fname + len, endp, sfx_len);
22251 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022253
22254theend:
22255 vim_free(pbuf_unused);
22256 vim_free(save_fname);
22257
22258 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259}
22260
22261/*
22262 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022263 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 */
22265 static int
22266shortpath_for_partial(fnamep, bufp, fnamelen)
22267 char_u **fnamep;
22268 char_u **bufp;
22269 int *fnamelen;
22270{
22271 int sepcount, len, tflen;
22272 char_u *p;
22273 char_u *pbuf, *tfname;
22274 int hasTilde;
22275
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022276 /* Count up the path separators from the RHS.. so we know which part
22277 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022279 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 if (vim_ispathsep(*p))
22281 ++sepcount;
22282
22283 /* Need full path first (use expand_env() to remove a "~/") */
22284 hasTilde = (**fnamep == '~');
22285 if (hasTilde)
22286 pbuf = tfname = expand_env_save(*fnamep);
22287 else
22288 pbuf = tfname = FullName_save(*fnamep, FALSE);
22289
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022290 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022292 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22293 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294
22295 if (len == 0)
22296 {
22297 /* Don't have a valid filename, so shorten the rest of the
22298 * path if we can. This CAN give us invalid 8.3 filenames, but
22299 * there's not a lot of point in guessing what it might be.
22300 */
22301 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022302 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22303 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 }
22305
22306 /* Count the paths backward to find the beginning of the desired string. */
22307 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022308 {
22309#ifdef FEAT_MBYTE
22310 if (has_mbyte)
22311 p -= mb_head_off(tfname, p);
22312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313 if (vim_ispathsep(*p))
22314 {
22315 if (sepcount == 0 || (hasTilde && sepcount == 1))
22316 break;
22317 else
22318 sepcount --;
22319 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321 if (hasTilde)
22322 {
22323 --p;
22324 if (p >= tfname)
22325 *p = '~';
22326 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022327 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328 }
22329 else
22330 ++p;
22331
22332 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22333 vim_free(*bufp);
22334 *fnamelen = (int)STRLEN(p);
22335 *bufp = pbuf;
22336 *fnamep = p;
22337
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022338 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339}
22340#endif /* WIN3264 */
22341
22342/*
22343 * Adjust a filename, according to a string of modifiers.
22344 * *fnamep must be NUL terminated when called. When returning, the length is
22345 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022346 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347 * When there is an error, *fnamep is set to NULL.
22348 */
22349 int
22350modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22351 char_u *src; /* string with modifiers */
22352 int *usedlen; /* characters after src that are used */
22353 char_u **fnamep; /* file name so far */
22354 char_u **bufp; /* buffer for allocated file name or NULL */
22355 int *fnamelen; /* length of fnamep */
22356{
22357 int valid = 0;
22358 char_u *tail;
22359 char_u *s, *p, *pbuf;
22360 char_u dirname[MAXPATHL];
22361 int c;
22362 int has_fullname = 0;
22363#ifdef WIN3264
22364 int has_shortname = 0;
22365#endif
22366
22367repeat:
22368 /* ":p" - full path/file_name */
22369 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22370 {
22371 has_fullname = 1;
22372
22373 valid |= VALID_PATH;
22374 *usedlen += 2;
22375
22376 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22377 if ((*fnamep)[0] == '~'
22378#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22379 && ((*fnamep)[1] == '/'
22380# ifdef BACKSLASH_IN_FILENAME
22381 || (*fnamep)[1] == '\\'
22382# endif
22383 || (*fnamep)[1] == NUL)
22384
22385#endif
22386 )
22387 {
22388 *fnamep = expand_env_save(*fnamep);
22389 vim_free(*bufp); /* free any allocated file name */
22390 *bufp = *fnamep;
22391 if (*fnamep == NULL)
22392 return -1;
22393 }
22394
22395 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022396 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 {
22398 if (vim_ispathsep(*p)
22399 && p[1] == '.'
22400 && (p[2] == NUL
22401 || vim_ispathsep(p[2])
22402 || (p[2] == '.'
22403 && (p[3] == NUL || vim_ispathsep(p[3])))))
22404 break;
22405 }
22406
22407 /* FullName_save() is slow, don't use it when not needed. */
22408 if (*p != NUL || !vim_isAbsName(*fnamep))
22409 {
22410 *fnamep = FullName_save(*fnamep, *p != NUL);
22411 vim_free(*bufp); /* free any allocated file name */
22412 *bufp = *fnamep;
22413 if (*fnamep == NULL)
22414 return -1;
22415 }
22416
22417 /* Append a path separator to a directory. */
22418 if (mch_isdir(*fnamep))
22419 {
22420 /* Make room for one or two extra characters. */
22421 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22422 vim_free(*bufp); /* free any allocated file name */
22423 *bufp = *fnamep;
22424 if (*fnamep == NULL)
22425 return -1;
22426 add_pathsep(*fnamep);
22427 }
22428 }
22429
22430 /* ":." - path relative to the current directory */
22431 /* ":~" - path relative to the home directory */
22432 /* ":8" - shortname path - postponed till after */
22433 while (src[*usedlen] == ':'
22434 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22435 {
22436 *usedlen += 2;
22437 if (c == '8')
22438 {
22439#ifdef WIN3264
22440 has_shortname = 1; /* Postpone this. */
22441#endif
22442 continue;
22443 }
22444 pbuf = NULL;
22445 /* Need full path first (use expand_env() to remove a "~/") */
22446 if (!has_fullname)
22447 {
22448 if (c == '.' && **fnamep == '~')
22449 p = pbuf = expand_env_save(*fnamep);
22450 else
22451 p = pbuf = FullName_save(*fnamep, FALSE);
22452 }
22453 else
22454 p = *fnamep;
22455
22456 has_fullname = 0;
22457
22458 if (p != NULL)
22459 {
22460 if (c == '.')
22461 {
22462 mch_dirname(dirname, MAXPATHL);
22463 s = shorten_fname(p, dirname);
22464 if (s != NULL)
22465 {
22466 *fnamep = s;
22467 if (pbuf != NULL)
22468 {
22469 vim_free(*bufp); /* free any allocated file name */
22470 *bufp = pbuf;
22471 pbuf = NULL;
22472 }
22473 }
22474 }
22475 else
22476 {
22477 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22478 /* Only replace it when it starts with '~' */
22479 if (*dirname == '~')
22480 {
22481 s = vim_strsave(dirname);
22482 if (s != NULL)
22483 {
22484 *fnamep = s;
22485 vim_free(*bufp);
22486 *bufp = s;
22487 }
22488 }
22489 }
22490 vim_free(pbuf);
22491 }
22492 }
22493
22494 tail = gettail(*fnamep);
22495 *fnamelen = (int)STRLEN(*fnamep);
22496
22497 /* ":h" - head, remove "/file_name", can be repeated */
22498 /* Don't remove the first "/" or "c:\" */
22499 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22500 {
22501 valid |= VALID_HEAD;
22502 *usedlen += 2;
22503 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022504 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022505 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 *fnamelen = (int)(tail - *fnamep);
22507#ifdef VMS
22508 if (*fnamelen > 0)
22509 *fnamelen += 1; /* the path separator is part of the path */
22510#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022511 if (*fnamelen == 0)
22512 {
22513 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22514 p = vim_strsave((char_u *)".");
22515 if (p == NULL)
22516 return -1;
22517 vim_free(*bufp);
22518 *bufp = *fnamep = tail = p;
22519 *fnamelen = 1;
22520 }
22521 else
22522 {
22523 while (tail > s && !after_pathsep(s, tail))
22524 mb_ptr_back(*fnamep, tail);
22525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 }
22527
22528 /* ":8" - shortname */
22529 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22530 {
22531 *usedlen += 2;
22532#ifdef WIN3264
22533 has_shortname = 1;
22534#endif
22535 }
22536
22537#ifdef WIN3264
22538 /* Check shortname after we have done 'heads' and before we do 'tails'
22539 */
22540 if (has_shortname)
22541 {
22542 pbuf = NULL;
22543 /* Copy the string if it is shortened by :h */
22544 if (*fnamelen < (int)STRLEN(*fnamep))
22545 {
22546 p = vim_strnsave(*fnamep, *fnamelen);
22547 if (p == 0)
22548 return -1;
22549 vim_free(*bufp);
22550 *bufp = *fnamep = p;
22551 }
22552
22553 /* Split into two implementations - makes it easier. First is where
22554 * there isn't a full name already, second is where there is.
22555 */
22556 if (!has_fullname && !vim_isAbsName(*fnamep))
22557 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022558 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559 return -1;
22560 }
22561 else
22562 {
22563 int l;
22564
22565 /* Simple case, already have the full-name
22566 * Nearly always shorter, so try first time. */
22567 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022568 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569 return -1;
22570
22571 if (l == 0)
22572 {
22573 /* Couldn't find the filename.. search the paths.
22574 */
22575 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022576 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 return -1;
22578 }
22579 *fnamelen = l;
22580 }
22581 }
22582#endif /* WIN3264 */
22583
22584 /* ":t" - tail, just the basename */
22585 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
22586 {
22587 *usedlen += 2;
22588 *fnamelen -= (int)(tail - *fnamep);
22589 *fnamep = tail;
22590 }
22591
22592 /* ":e" - extension, can be repeated */
22593 /* ":r" - root, without extension, can be repeated */
22594 while (src[*usedlen] == ':'
22595 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
22596 {
22597 /* find a '.' in the tail:
22598 * - for second :e: before the current fname
22599 * - otherwise: The last '.'
22600 */
22601 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
22602 s = *fnamep - 2;
22603 else
22604 s = *fnamep + *fnamelen - 1;
22605 for ( ; s > tail; --s)
22606 if (s[0] == '.')
22607 break;
22608 if (src[*usedlen + 1] == 'e') /* :e */
22609 {
22610 if (s > tail)
22611 {
22612 *fnamelen += (int)(*fnamep - (s + 1));
22613 *fnamep = s + 1;
22614#ifdef VMS
22615 /* cut version from the extension */
22616 s = *fnamep + *fnamelen - 1;
22617 for ( ; s > *fnamep; --s)
22618 if (s[0] == ';')
22619 break;
22620 if (s > *fnamep)
22621 *fnamelen = s - *fnamep;
22622#endif
22623 }
22624 else if (*fnamep <= tail)
22625 *fnamelen = 0;
22626 }
22627 else /* :r */
22628 {
22629 if (s > tail) /* remove one extension */
22630 *fnamelen = (int)(s - *fnamep);
22631 }
22632 *usedlen += 2;
22633 }
22634
22635 /* ":s?pat?foo?" - substitute */
22636 /* ":gs?pat?foo?" - global substitute */
22637 if (src[*usedlen] == ':'
22638 && (src[*usedlen + 1] == 's'
22639 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
22640 {
22641 char_u *str;
22642 char_u *pat;
22643 char_u *sub;
22644 int sep;
22645 char_u *flags;
22646 int didit = FALSE;
22647
22648 flags = (char_u *)"";
22649 s = src + *usedlen + 2;
22650 if (src[*usedlen + 1] == 'g')
22651 {
22652 flags = (char_u *)"g";
22653 ++s;
22654 }
22655
22656 sep = *s++;
22657 if (sep)
22658 {
22659 /* find end of pattern */
22660 p = vim_strchr(s, sep);
22661 if (p != NULL)
22662 {
22663 pat = vim_strnsave(s, (int)(p - s));
22664 if (pat != NULL)
22665 {
22666 s = p + 1;
22667 /* find end of substitution */
22668 p = vim_strchr(s, sep);
22669 if (p != NULL)
22670 {
22671 sub = vim_strnsave(s, (int)(p - s));
22672 str = vim_strnsave(*fnamep, *fnamelen);
22673 if (sub != NULL && str != NULL)
22674 {
22675 *usedlen = (int)(p + 1 - src);
22676 s = do_string_sub(str, pat, sub, flags);
22677 if (s != NULL)
22678 {
22679 *fnamep = s;
22680 *fnamelen = (int)STRLEN(s);
22681 vim_free(*bufp);
22682 *bufp = s;
22683 didit = TRUE;
22684 }
22685 }
22686 vim_free(sub);
22687 vim_free(str);
22688 }
22689 vim_free(pat);
22690 }
22691 }
22692 /* after using ":s", repeat all the modifiers */
22693 if (didit)
22694 goto repeat;
22695 }
22696 }
22697
22698 return valid;
22699}
22700
22701/*
22702 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22703 * "flags" can be "g" to do a global substitute.
22704 * Returns an allocated string, NULL for error.
22705 */
22706 char_u *
22707do_string_sub(str, pat, sub, flags)
22708 char_u *str;
22709 char_u *pat;
22710 char_u *sub;
22711 char_u *flags;
22712{
22713 int sublen;
22714 regmatch_T regmatch;
22715 int i;
22716 int do_all;
22717 char_u *tail;
22718 garray_T ga;
22719 char_u *ret;
22720 char_u *save_cpo;
22721
22722 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22723 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022724 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725
22726 ga_init2(&ga, 1, 200);
22727
22728 do_all = (flags[0] == 'g');
22729
22730 regmatch.rm_ic = p_ic;
22731 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
22732 if (regmatch.regprog != NULL)
22733 {
22734 tail = str;
22735 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
22736 {
22737 /*
22738 * Get some space for a temporary buffer to do the substitution
22739 * into. It will contain:
22740 * - The text up to where the match is.
22741 * - The substituted text.
22742 * - The text after the match.
22743 */
22744 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
22745 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
22746 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
22747 {
22748 ga_clear(&ga);
22749 break;
22750 }
22751
22752 /* copy the text up to where the match is */
22753 i = (int)(regmatch.startp[0] - tail);
22754 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
22755 /* add the substituted text */
22756 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
22757 + ga.ga_len + i, TRUE, TRUE, FALSE);
22758 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 /* avoid getting stuck on a match with an empty string */
22760 if (tail == regmatch.endp[0])
22761 {
22762 if (*tail == NUL)
22763 break;
22764 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
22765 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766 }
22767 else
22768 {
22769 tail = regmatch.endp[0];
22770 if (*tail == NUL)
22771 break;
22772 }
22773 if (!do_all)
22774 break;
22775 }
22776
22777 if (ga.ga_data != NULL)
22778 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
22779
22780 vim_free(regmatch.regprog);
22781 }
22782
22783 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
22784 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000022785 if (p_cpo == empty_option)
22786 p_cpo = save_cpo;
22787 else
22788 /* Darn, evaluating {sub} expression changed the value. */
22789 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790
22791 return ret;
22792}
22793
22794#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */