blob: f3fd5ff01b6e370328896c4d356e81f5acd06d87 [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
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200129/* When using exists() don't auto-load a script. */
130static int no_autoload = FALSE;
131
Bram Moolenaar532c7802005-01-27 14:44:31 +0000132/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133 * When recursively copying lists and dicts we need to remember which ones we
134 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000135 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136 */
137static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000138#define COPYID_INC 2
139#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140
141/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000142 * Array to hold the hashtab with variables local to each sourced script.
143 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000145typedef struct
146{
147 dictitem_T sv_var;
148 dict_T sv_dict;
149} scriptvar_T;
150
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200151static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
152#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
153#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155static int echo_attr = 0; /* attributes used for ":echo" */
156
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000157/* Values for trans_function_name() argument: */
158#define TFN_INT 1 /* internal function name OK */
159#define TFN_QUIET 2 /* no error messages */
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161/*
162 * Structure to hold info for a user function.
163 */
164typedef struct ufunc ufunc_T;
165
166struct ufunc
167{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000168 int uf_varargs; /* variable nr of arguments */
169 int uf_flags;
170 int uf_calls; /* nr of active calls */
171 garray_T uf_args; /* arguments */
172 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000173#ifdef FEAT_PROFILE
174 int uf_profiling; /* TRUE when func is being profiled */
175 /* profiling the function as a whole */
176 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000177 proftime_T uf_tm_total; /* time spent in function + children */
178 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000179 proftime_T uf_tm_children; /* time spent in children this call */
180 /* profiling the function per line */
181 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000182 proftime_T *uf_tml_total; /* time spent in a line + children */
183 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000184 proftime_T uf_tml_start; /* start time for current line */
185 proftime_T uf_tml_children; /* time spent in children for this line */
186 proftime_T uf_tml_wait; /* start wait time for current line */
187 int uf_tml_idx; /* index of line being timed; -1 if none */
188 int uf_tml_execed; /* line being timed was executed */
189#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000190 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000192 int uf_refcount; /* for numbered function: reference count */
193 char_u uf_name[1]; /* name of function (actually longer); can
194 start with <SNR>123_ (<SNR> is K_SPECIAL
195 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196};
197
198/* function flags */
199#define FC_ABORT 1 /* abort function on error */
200#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000201#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
203/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000204 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000206static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000208/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000209static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
210
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000211/* list heads for garbage collection */
212static dict_T *first_dict = NULL; /* list of all dicts */
213static list_T *first_list = NULL; /* list of all lists */
214
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000215/* From user function to hashitem and back. */
216static ufunc_T dumuf;
217#define UF2HIKEY(fp) ((fp)->uf_name)
218#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
219#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
220
221#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
222#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223
Bram Moolenaar33570922005-01-25 22:26:29 +0000224#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
225#define VAR_SHORT_LEN 20 /* short variable name length */
226#define FIXVAR_CNT 12 /* number of fixed variables */
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000229typedef struct funccall_S funccall_T;
230
231struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232{
233 ufunc_T *func; /* function being called */
234 int linenr; /* next line to be executed */
235 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000236 struct /* fixed variables for arguments */
237 {
238 dictitem_T var; /* variable (without room for name) */
239 char_u room[VAR_SHORT_LEN]; /* room for the name */
240 } fixvar[FIXVAR_CNT];
241 dict_T l_vars; /* l: local function variables */
242 dictitem_T l_vars_var; /* variable for l: scope */
243 dict_T l_avars; /* a: argument variables */
244 dictitem_T l_avars_var; /* variable for a: scope */
245 list_T l_varlist; /* list for a:000 */
246 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
247 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 linenr_T breakpoint; /* next line with breakpoint or zero */
249 int dbg_tick; /* debug_tick when breakpoint was set */
250 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000251#ifdef FEAT_PROFILE
252 proftime_T prof_child; /* time spent in a child */
253#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000254 funccall_T *caller; /* calling function or NULL */
255};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256
257/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258 * Info used by a ":for" loop.
259 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000260typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261{
262 int fi_semicolon; /* TRUE if ending in '; var]' */
263 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264 listwatch_T fi_lw; /* keep an eye on the item used. */
265 list_T *fi_list; /* list being used */
266} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000269 * Struct used by trans_function_name()
270 */
271typedef struct
272{
Bram Moolenaar33570922005-01-25 22:26:29 +0000273 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000274 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000275 dictitem_T *fd_di; /* Dictionary item used */
276} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000277
Bram Moolenaara7043832005-01-21 11:56:39 +0000278
279/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000280 * Array to hold the value of v: variables.
281 * The value is in a dictitem, so that it can also be used in the v: scope.
282 * The reason to use this table anyway is for very quick access to the
283 * variables with the VV_ defines.
284 */
285#include "version.h"
286
287/* values for vv_flags: */
288#define VV_COMPAT 1 /* compatible, also used without "v:" */
289#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000290#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000291
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000292#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000293
294static struct vimvar
295{
296 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297 dictitem_T vv_di; /* value and name for key */
298 char vv_filler[16]; /* space for LONGEST name below!!! */
299 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
300} vimvars[VV_LEN] =
301{
302 /*
303 * The order here must match the VV_ defines in vim.h!
304 * Initializing a union does not work, leave tv.vval empty to get zero's.
305 */
306 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
307 {VV_NAME("count1", VAR_NUMBER), VV_RO},
308 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
309 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
310 {VV_NAME("warningmsg", VAR_STRING), 0},
311 {VV_NAME("statusmsg", VAR_STRING), 0},
312 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
313 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
314 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
316 {VV_NAME("termresponse", VAR_STRING), VV_RO},
317 {VV_NAME("fname", VAR_STRING), VV_RO},
318 {VV_NAME("lang", VAR_STRING), VV_RO},
319 {VV_NAME("lc_time", VAR_STRING), VV_RO},
320 {VV_NAME("ctype", VAR_STRING), VV_RO},
321 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
323 {VV_NAME("fname_in", VAR_STRING), VV_RO},
324 {VV_NAME("fname_out", VAR_STRING), VV_RO},
325 {VV_NAME("fname_new", VAR_STRING), VV_RO},
326 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
327 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
328 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
331 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("progname", VAR_STRING), VV_RO},
333 {VV_NAME("servername", VAR_STRING), VV_RO},
334 {VV_NAME("dying", VAR_NUMBER), VV_RO},
335 {VV_NAME("exception", VAR_STRING), VV_RO},
336 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
337 {VV_NAME("register", VAR_STRING), VV_RO},
338 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
339 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000340 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
341 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000342 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000343 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
344 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000345 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000350 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000351 {VV_NAME("swapname", VAR_STRING), VV_RO},
352 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000353 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000354 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000355 {VV_NAME("mouse_win", VAR_NUMBER), 0},
356 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
357 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000358 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000359 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000360 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000361};
362
363/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364#define vv_type vv_di.di_tv.v_type
365#define vv_nr vv_di.di_tv.vval.v_number
366#define vv_float vv_di.di_tv.vval.v_float
367#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000368#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000370
371/*
372 * The v: variables are stored in dictionary "vimvardict".
373 * "vimvars_var" is the variable that is used for the "l:" scope.
374 */
375static dict_T vimvardict;
376static dictitem_T vimvars_var;
377#define vimvarht vimvardict.dv_hashtab
378
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
380static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
381#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
382static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
383#endif
384static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
385static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
386static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000387static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
388static void list_glob_vars __ARGS((int *first));
389static void list_buf_vars __ARGS((int *first));
390static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000392static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000393#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000394static void list_vim_vars __ARGS((int *first));
395static void list_script_vars __ARGS((int *first));
396static void list_func_vars __ARGS((int *first));
397static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000398static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
399static int check_changedtick __ARGS((char_u *arg));
400static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
401static void clear_lval __ARGS((lval_T *lp));
402static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
403static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
404static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
405static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
406static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
407static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
408static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
409static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
410static void item_lock __ARGS((typval_T *tv, int deep, int lock));
411static int tv_islocked __ARGS((typval_T *tv));
412
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
414static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000419static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
420static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000421
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000422static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000427static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static listitem_T *listitem_alloc __ARGS((void));
429static void listitem_free __ARGS((listitem_T *item));
430static void listitem_remove __ARGS((list_T *l, listitem_T *item));
431static long list_len __ARGS((list_T *l));
432static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
433static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
434static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000436static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000439static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
441static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
442static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *list2string __ARGS((typval_T *tv, int copyID));
446static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000447static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000448static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
449static void set_ref_in_list __ARGS((list_T *l, int copyID));
450static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200451static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000453static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
455static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000456static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000458static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000460static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
461static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#ifdef FEAT_FLOAT
464static int string2float __ARGS((char_u *text, float_T *value));
465#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000466static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
467static int find_internal_func __ARGS((char_u *name));
468static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
469static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200470static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000471static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000472static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000473
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
475static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200476static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477#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
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200484static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000502static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000505static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000508static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
515static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200516static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
521static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200532#ifdef FEAT_FLOAT
533static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
534#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000537static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
544static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000548static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000557static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000559static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000573static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000574static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000575static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000576static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200579static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000580static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000588static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000602static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000608static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000620#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200621static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000629static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000632static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000636#ifdef vim_mkdir
637static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100640#ifdef FEAT_MZSCHEME
641static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000645static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000646#ifdef FEAT_FLOAT
647static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
648#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000650static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000651static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000653static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000654static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000666#ifdef FEAT_FLOAT
667static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
668#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000670static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000672static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000679static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000680static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000681static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000682static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200684static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000685static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000687static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
690static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200691static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000694static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000695static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#ifdef FEAT_FLOAT
699static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
701#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000702static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703#ifdef HAVE_STRFTIME
704static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
706static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000717static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000718static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000719static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000720static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000721static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000722static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000723static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000725static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200726#ifdef FEAT_FLOAT
727static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
729#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#ifdef FEAT_FLOAT
734static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200737static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200738static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000748static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000750static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000751static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000753static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000754static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static int get_env_len __ARGS((char_u **arg));
756static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000758static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
759#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
760#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
761 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000762static 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 +0000763static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000764static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000765static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
766static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static typval_T *alloc_tv __ARGS((void));
768static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void init_tv __ARGS((typval_T *varp));
770static long get_tv_number __ARGS((typval_T *varp));
771static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000772static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static char_u *get_tv_string __ARGS((typval_T *varp));
774static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000775static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000777static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
779static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
780static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000781static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
782static 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 +0000783static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
784static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000785static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000786static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000787static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
789static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
790static int eval_fname_script __ARGS((char_u *p));
791static int eval_fname_sid __ARGS((char_u *p));
792static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793static ufunc_T *find_func __ARGS((char_u *name));
794static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000795static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796#ifdef FEAT_PROFILE
797static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000798static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
799static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
800static int
801# ifdef __BORLANDC__
802 _RTLENTRYF
803# endif
804 prof_total_cmp __ARGS((const void *s1, const void *s2));
805static int
806# ifdef __BORLANDC__
807 _RTLENTRYF
808# endif
809 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000811static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000812static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000813static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000814static void func_free __ARGS((ufunc_T *fp));
815static void func_unref __ARGS((char_u *name));
816static void func_ref __ARGS((char_u *name));
817static 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 +0000818static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
819static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000820static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000821static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
822static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000823static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000824static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000825static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200827
828#ifdef EBCDIC
829static int compare_func_name __ARGS((const void *s1, const void *s2));
830static void sortFunctions __ARGS(());
831#endif
832
833
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000834/* Character used as separated in autoload function/variable names. */
835#define AUTOLOAD_CHAR '#'
836
Bram Moolenaar33570922005-01-25 22:26:29 +0000837/*
838 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000839 */
840 void
841eval_init()
842{
Bram Moolenaar33570922005-01-25 22:26:29 +0000843 int i;
844 struct vimvar *p;
845
846 init_var_dict(&globvardict, &globvars_var);
847 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000848 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000849 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000850
851 for (i = 0; i < VV_LEN; ++i)
852 {
853 p = &vimvars[i];
854 STRCPY(p->vv_di.di_key, p->vv_name);
855 if (p->vv_flags & VV_RO)
856 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
857 else if (p->vv_flags & VV_RO_SBX)
858 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
859 else
860 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000861
862 /* add to v: scope dict, unless the value is not always available */
863 if (p->vv_type != VAR_UNKNOWN)
864 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000865 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000866 /* add to compat scope dict */
867 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000868 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000869 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200870
871#ifdef EBCDIC
872 /*
873 * Sort the function table, to enable binary sort.
874 */
875 sortFunctions();
876#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000877}
878
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000879#if defined(EXITFREE) || defined(PROTO)
880 void
881eval_clear()
882{
883 int i;
884 struct vimvar *p;
885
886 for (i = 0; i < VV_LEN; ++i)
887 {
888 p = &vimvars[i];
889 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000890 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000891 vim_free(p->vv_str);
892 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000893 }
894 else if (p->vv_di.di_tv.v_type == VAR_LIST)
895 {
896 list_unref(p->vv_list);
897 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000898 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000899 }
900 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000901 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000902 hash_clear(&compat_hashtab);
903
Bram Moolenaard9fba312005-06-26 22:34:35 +0000904 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905
906 /* global variables */
907 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000908
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000909 /* autoloaded script names */
910 ga_clear_strings(&ga_loaded);
911
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200912 /* script-local variables */
913 for (i = 1; i <= ga_scripts.ga_len; ++i)
914 {
915 vars_clear(&SCRIPT_VARS(i));
916 vim_free(SCRIPT_SV(i));
917 }
918 ga_clear(&ga_scripts);
919
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000920 /* unreferenced lists and dicts */
921 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000922
923 /* functions */
924 free_all_functions();
925 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000926}
927#endif
928
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 * Return the name of the executed function.
931 */
932 char_u *
933func_name(cookie)
934 void *cookie;
935{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000936 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937}
938
939/*
940 * Return the address holding the next breakpoint line for a funccall cookie.
941 */
942 linenr_T *
943func_breakpoint(cookie)
944 void *cookie;
945{
Bram Moolenaar33570922005-01-25 22:26:29 +0000946 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947}
948
949/*
950 * Return the address holding the debug tick for a funccall cookie.
951 */
952 int *
953func_dbg_tick(cookie)
954 void *cookie;
955{
Bram Moolenaar33570922005-01-25 22:26:29 +0000956 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959/*
960 * Return the nesting level for a funccall cookie.
961 */
962 int
963func_level(cookie)
964 void *cookie;
965{
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
969/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000970funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000972/* pointer to list of previously used funccal, still around because some
973 * item in it is still being used. */
974funccall_T *previous_funccal = NULL;
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976/*
977 * Return TRUE when a function was ended by a ":return" command.
978 */
979 int
980current_func_returned()
981{
982 return current_funccal->returned;
983}
984
985
986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 * Set an internal variable to a string value. Creates the variable if it does
988 * not already exist.
989 */
990 void
991set_internal_string_var(name, value)
992 char_u *name;
993 char_u *value;
994{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000995 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000996 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
998 val = vim_strsave(value);
999 if (val != NULL)
1000 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001001 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001002 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001004 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001005 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007 }
1008}
1009
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001010static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001011static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001012static char_u *redir_endp = NULL;
1013static char_u *redir_varname = NULL;
1014
1015/*
1016 * Start recording command output to a variable
1017 * Returns OK if successfully completed the setup. FAIL otherwise.
1018 */
1019 int
1020var_redir_start(name, append)
1021 char_u *name;
1022 int append; /* append to an existing variable */
1023{
1024 int save_emsg;
1025 int err;
1026 typval_T tv;
1027
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001028 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001029 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001030 {
1031 EMSG(_(e_invarg));
1032 return FAIL;
1033 }
1034
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001035 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 redir_varname = vim_strsave(name);
1037 if (redir_varname == NULL)
1038 return FAIL;
1039
1040 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1041 if (redir_lval == NULL)
1042 {
1043 var_redir_stop();
1044 return FAIL;
1045 }
1046
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001047 /* The output is stored in growarray "redir_ga" until redirection ends. */
1048 ga_init2(&redir_ga, (int)sizeof(char), 500);
1049
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001051 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1052 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1054 {
1055 if (redir_endp != NULL && *redir_endp != NUL)
1056 /* Trailing characters are present after the variable name */
1057 EMSG(_(e_trailing));
1058 else
1059 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001060 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 var_redir_stop();
1062 return FAIL;
1063 }
1064
1065 /* check if we can write to the variable: set it to or append an empty
1066 * string */
1067 save_emsg = did_emsg;
1068 did_emsg = FALSE;
1069 tv.v_type = VAR_STRING;
1070 tv.vval.v_string = (char_u *)"";
1071 if (append)
1072 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1073 else
1074 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1075 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001076 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001077 if (err)
1078 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001079 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 var_redir_stop();
1081 return FAIL;
1082 }
1083 if (redir_lval->ll_newkey != NULL)
1084 {
1085 /* Dictionary item was created, don't do it again. */
1086 vim_free(redir_lval->ll_newkey);
1087 redir_lval->ll_newkey = NULL;
1088 }
1089
1090 return OK;
1091}
1092
1093/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001094 * Append "value[value_len]" to the variable set by var_redir_start().
1095 * The actual appending is postponed until redirection ends, because the value
1096 * appended may in fact be the string we write to, changing it may cause freed
1097 * memory to be used:
1098 * :redir => foo
1099 * :let foo
1100 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 */
1102 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001107 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108
1109 if (redir_lval == NULL)
1110 return;
1111
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 {
1119 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 }
1122 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124}
1125
1126/*
1127 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001128 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 */
1130 void
1131var_redir_stop()
1132{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 typval_T tv;
1134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 if (redir_lval != NULL)
1136 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 /* If there was no error: assign the text to the variable. */
1138 if (redir_endp != NULL)
1139 {
1140 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1141 tv.v_type = VAR_STRING;
1142 tv.vval.v_string = redir_ga.ga_data;
1143 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1144 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001145
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 /* free the collected output */
1147 vim_free(redir_ga.ga_data);
1148 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 clear_lval(redir_lval);
1151 vim_free(redir_lval);
1152 redir_lval = NULL;
1153 }
1154 vim_free(redir_varname);
1155 redir_varname = NULL;
1156}
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158# if defined(FEAT_MBYTE) || defined(PROTO)
1159 int
1160eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1161 char_u *enc_from;
1162 char_u *enc_to;
1163 char_u *fname_from;
1164 char_u *fname_to;
1165{
1166 int err = FALSE;
1167
1168 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1169 set_vim_var_string(VV_CC_TO, enc_to, -1);
1170 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1171 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1172 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1173 err = TRUE;
1174 set_vim_var_string(VV_CC_FROM, NULL, -1);
1175 set_vim_var_string(VV_CC_TO, NULL, -1);
1176 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1177 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1178
1179 if (err)
1180 return FAIL;
1181 return OK;
1182}
1183# endif
1184
1185# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1186 int
1187eval_printexpr(fname, args)
1188 char_u *fname;
1189 char_u *args;
1190{
1191 int err = FALSE;
1192
1193 set_vim_var_string(VV_FNAME_IN, fname, -1);
1194 set_vim_var_string(VV_CMDARG, args, -1);
1195 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1196 err = TRUE;
1197 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1198 set_vim_var_string(VV_CMDARG, NULL, -1);
1199
1200 if (err)
1201 {
1202 mch_remove(fname);
1203 return FAIL;
1204 }
1205 return OK;
1206}
1207# endif
1208
1209# if defined(FEAT_DIFF) || defined(PROTO)
1210 void
1211eval_diff(origfile, newfile, outfile)
1212 char_u *origfile;
1213 char_u *newfile;
1214 char_u *outfile;
1215{
1216 int err = FALSE;
1217
1218 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1219 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1220 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1221 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1222 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1223 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1224 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1225}
1226
1227 void
1228eval_patch(origfile, difffile, outfile)
1229 char_u *origfile;
1230 char_u *difffile;
1231 char_u *outfile;
1232{
1233 int err;
1234
1235 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1236 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1237 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1238 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1239 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1240 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1241 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1242}
1243# endif
1244
1245/*
1246 * Top level evaluation function, returning a boolean.
1247 * Sets "error" to TRUE if there was an error.
1248 * Return TRUE or FALSE.
1249 */
1250 int
1251eval_to_bool(arg, error, nextcmd, skip)
1252 char_u *arg;
1253 int *error;
1254 char_u **nextcmd;
1255 int skip; /* only parse, don't execute */
1256{
Bram Moolenaar33570922005-01-25 22:26:29 +00001257 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 int retval = FALSE;
1259
1260 if (skip)
1261 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001262 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 else
1265 {
1266 *error = FALSE;
1267 if (!skip)
1268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001269 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272 }
1273 if (skip)
1274 --emsg_skip;
1275
1276 return retval;
1277}
1278
1279/*
1280 * Top level evaluation function, returning a string. If "skip" is TRUE,
1281 * only parsing to "nextcmd" is done, without reporting errors. Return
1282 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1283 */
1284 char_u *
1285eval_to_string_skip(arg, nextcmd, skip)
1286 char_u *arg;
1287 char_u **nextcmd;
1288 int skip; /* only parse, don't execute */
1289{
Bram Moolenaar33570922005-01-25 22:26:29 +00001290 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 char_u *retval;
1292
1293 if (skip)
1294 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001295 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 retval = NULL;
1297 else
1298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001299 retval = vim_strsave(get_tv_string(&tv));
1300 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
1302 if (skip)
1303 --emsg_skip;
1304
1305 return retval;
1306}
1307
1308/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001309 * Skip over an expression at "*pp".
1310 * Return FAIL for an error, OK otherwise.
1311 */
1312 int
1313skip_expr(pp)
1314 char_u **pp;
1315{
Bram Moolenaar33570922005-01-25 22:26:29 +00001316 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001317
1318 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001320}
1321
1322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001324 * When "convert" is TRUE convert a List into a sequence of lines and convert
1325 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 * Return pointer to allocated memory, or NULL for failure.
1327 */
1328 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001329eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 char_u *arg;
1331 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001332 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
Bram Moolenaar33570922005-01-25 22:26:29 +00001334 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001336 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001337#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 retval = NULL;
1343 else
1344 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001346 {
1347 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001348 if (tv.vval.v_list != NULL)
1349 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001350 ga_append(&ga, NUL);
1351 retval = (char_u *)ga.ga_data;
1352 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001353#ifdef FEAT_FLOAT
1354 else if (convert && tv.v_type == VAR_FLOAT)
1355 {
1356 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1357 retval = vim_strsave(numbuf);
1358 }
1359#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 else
1361 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001362 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364
1365 return retval;
1366}
1367
1368/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001369 * Call eval_to_string() without using current local variables and using
1370 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 */
1372 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001373eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 char_u *arg;
1375 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001376 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 char_u *retval;
1379 void *save_funccalp;
1380
1381 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001382 if (use_sandbox)
1383 ++sandbox;
1384 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001385 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001386 if (use_sandbox)
1387 --sandbox;
1388 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 restore_funccal(save_funccalp);
1390 return retval;
1391}
1392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393/*
1394 * Top level evaluation function, returning a number.
1395 * Evaluates "expr" silently.
1396 * Returns -1 for an error.
1397 */
1398 int
1399eval_to_number(expr)
1400 char_u *expr;
1401{
Bram Moolenaar33570922005-01-25 22:26:29 +00001402 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001404 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
1406 ++emsg_off;
1407
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001408 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 retval = -1;
1410 else
1411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001412 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001413 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415 --emsg_off;
1416
1417 return retval;
1418}
1419
Bram Moolenaara40058a2005-07-11 22:42:07 +00001420/*
1421 * Prepare v: variable "idx" to be used.
1422 * Save the current typeval in "save_tv".
1423 * When not used yet add the variable to the v: hashtable.
1424 */
1425 static void
1426prepare_vimvar(idx, save_tv)
1427 int idx;
1428 typval_T *save_tv;
1429{
1430 *save_tv = vimvars[idx].vv_tv;
1431 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1432 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1433}
1434
1435/*
1436 * Restore v: variable "idx" to typeval "save_tv".
1437 * When no longer defined, remove the variable from the v: hashtable.
1438 */
1439 static void
1440restore_vimvar(idx, save_tv)
1441 int idx;
1442 typval_T *save_tv;
1443{
1444 hashitem_T *hi;
1445
Bram Moolenaara40058a2005-07-11 22:42:07 +00001446 vimvars[idx].vv_tv = *save_tv;
1447 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1448 {
1449 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1450 if (HASHITEM_EMPTY(hi))
1451 EMSG2(_(e_intern2), "restore_vimvar()");
1452 else
1453 hash_remove(&vimvarht, hi);
1454 }
1455}
1456
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001457#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001458/*
1459 * Evaluate an expression to a list with suggestions.
1460 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001461 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001462 */
1463 list_T *
1464eval_spell_expr(badword, expr)
1465 char_u *badword;
1466 char_u *expr;
1467{
1468 typval_T save_val;
1469 typval_T rettv;
1470 list_T *list = NULL;
1471 char_u *p = skipwhite(expr);
1472
1473 /* Set "v:val" to the bad word. */
1474 prepare_vimvar(VV_VAL, &save_val);
1475 vimvars[VV_VAL].vv_type = VAR_STRING;
1476 vimvars[VV_VAL].vv_str = badword;
1477 if (p_verbose == 0)
1478 ++emsg_off;
1479
1480 if (eval1(&p, &rettv, TRUE) == OK)
1481 {
1482 if (rettv.v_type != VAR_LIST)
1483 clear_tv(&rettv);
1484 else
1485 list = rettv.vval.v_list;
1486 }
1487
1488 if (p_verbose == 0)
1489 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001490 restore_vimvar(VV_VAL, &save_val);
1491
1492 return list;
1493}
1494
1495/*
1496 * "list" is supposed to contain two items: a word and a number. Return the
1497 * word in "pp" and the number as the return value.
1498 * Return -1 if anything isn't right.
1499 * Used to get the good word and score from the eval_spell_expr() result.
1500 */
1501 int
1502get_spellword(list, pp)
1503 list_T *list;
1504 char_u **pp;
1505{
1506 listitem_T *li;
1507
1508 li = list->lv_first;
1509 if (li == NULL)
1510 return -1;
1511 *pp = get_tv_string(&li->li_tv);
1512
1513 li = li->li_next;
1514 if (li == NULL)
1515 return -1;
1516 return get_tv_number(&li->li_tv);
1517}
1518#endif
1519
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001520/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001521 * Top level evaluation function.
1522 * Returns an allocated typval_T with the result.
1523 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001524 */
1525 typval_T *
1526eval_expr(arg, nextcmd)
1527 char_u *arg;
1528 char_u **nextcmd;
1529{
1530 typval_T *tv;
1531
1532 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001533 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001534 {
1535 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001536 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001537 }
1538
1539 return tv;
1540}
1541
1542
Bram Moolenaar4f688582007-07-24 12:34:30 +00001543#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1544 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001546 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001547 * Uses argv[argc] for the function arguments. Only Number and String
1548 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001549 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001551 static int
1552call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 char_u *func;
1554 int argc;
1555 char_u **argv;
1556 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
Bram Moolenaar33570922005-01-25 22:26:29 +00001559 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 long n;
1561 int len;
1562 int i;
1563 int doesrange;
1564 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001567 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001569 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
1571 for (i = 0; i < argc; i++)
1572 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001573 /* Pass a NULL or empty argument as an empty string */
1574 if (argv[i] == NULL || *argv[i] == NUL)
1575 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001576 argvars[i].v_type = VAR_STRING;
1577 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001578 continue;
1579 }
1580
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 /* Recognize a number argument, the others must be strings. */
1582 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1583 if (len != 0 && len == (int)STRLEN(argv[i]))
1584 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001585 argvars[i].v_type = VAR_NUMBER;
1586 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588 else
1589 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001590 argvars[i].v_type = VAR_STRING;
1591 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593 }
1594
1595 if (safe)
1596 {
1597 save_funccalp = save_funccal();
1598 ++sandbox;
1599 }
1600
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001601 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1602 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001604 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (safe)
1606 {
1607 --sandbox;
1608 restore_funccal(save_funccalp);
1609 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 vim_free(argvars);
1611
1612 if (ret == FAIL)
1613 clear_tv(rettv);
1614
1615 return ret;
1616}
1617
Bram Moolenaar4f688582007-07-24 12:34:30 +00001618# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001620 * Call vimL function "func" and return the result as a string.
1621 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001622 * Uses argv[argc] for the function arguments.
1623 */
1624 void *
1625call_func_retstr(func, argc, argv, safe)
1626 char_u *func;
1627 int argc;
1628 char_u **argv;
1629 int safe; /* use the sandbox */
1630{
1631 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001632 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633
1634 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1635 return NULL;
1636
1637 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 return retval;
1640}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001641# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642
Bram Moolenaar4f688582007-07-24 12:34:30 +00001643# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001645 * Call vimL function "func" and return the result as a number.
1646 * Returns -1 when calling the function fails.
1647 * Uses argv[argc] for the function arguments.
1648 */
1649 long
1650call_func_retnr(func, argc, argv, safe)
1651 char_u *func;
1652 int argc;
1653 char_u **argv;
1654 int safe; /* use the sandbox */
1655{
1656 typval_T rettv;
1657 long retval;
1658
1659 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1660 return -1;
1661
1662 retval = get_tv_number_chk(&rettv, NULL);
1663 clear_tv(&rettv);
1664 return retval;
1665}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001666# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001667
1668/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001669 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001670 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001671 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 */
1673 void *
1674call_func_retlist(func, argc, argv, safe)
1675 char_u *func;
1676 int argc;
1677 char_u **argv;
1678 int safe; /* use the sandbox */
1679{
1680 typval_T rettv;
1681
1682 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1683 return NULL;
1684
1685 if (rettv.v_type != VAR_LIST)
1686 {
1687 clear_tv(&rettv);
1688 return NULL;
1689 }
1690
1691 return rettv.vval.v_list;
1692}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#endif
1694
Bram Moolenaar4f688582007-07-24 12:34:30 +00001695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696/*
1697 * Save the current function call pointer, and set it to NULL.
1698 * Used when executing autocommands and for ":source".
1699 */
1700 void *
1701save_funccal()
1702{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001703 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 current_funccal = NULL;
1706 return (void *)fc;
1707}
1708
1709 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001710restore_funccal(vfc)
1711 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001713 funccall_T *fc = (funccall_T *)vfc;
1714
1715 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716}
1717
Bram Moolenaar05159a02005-02-26 23:04:13 +00001718#if defined(FEAT_PROFILE) || defined(PROTO)
1719/*
1720 * Prepare profiling for entering a child or something else that is not
1721 * counted for the script/function itself.
1722 * Should always be called in pair with prof_child_exit().
1723 */
1724 void
1725prof_child_enter(tm)
1726 proftime_T *tm; /* place to store waittime */
1727{
1728 funccall_T *fc = current_funccal;
1729
1730 if (fc != NULL && fc->func->uf_profiling)
1731 profile_start(&fc->prof_child);
1732 script_prof_save(tm);
1733}
1734
1735/*
1736 * Take care of time spent in a child.
1737 * Should always be called after prof_child_enter().
1738 */
1739 void
1740prof_child_exit(tm)
1741 proftime_T *tm; /* where waittime was stored */
1742{
1743 funccall_T *fc = current_funccal;
1744
1745 if (fc != NULL && fc->func->uf_profiling)
1746 {
1747 profile_end(&fc->prof_child);
1748 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1749 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1750 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1751 }
1752 script_prof_restore(tm);
1753}
1754#endif
1755
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757#ifdef FEAT_FOLDING
1758/*
1759 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1760 * it in "*cp". Doesn't give error messages.
1761 */
1762 int
1763eval_foldexpr(arg, cp)
1764 char_u *arg;
1765 int *cp;
1766{
Bram Moolenaar33570922005-01-25 22:26:29 +00001767 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 int retval;
1769 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001770 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1771 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001774 if (use_sandbox)
1775 ++sandbox;
1776 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 retval = 0;
1780 else
1781 {
1782 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 if (tv.v_type == VAR_NUMBER)
1784 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001785 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 retval = 0;
1787 else
1788 {
1789 /* If the result is a string, check if there is a non-digit before
1790 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (!VIM_ISDIGIT(*s) && *s != '-')
1793 *cp = *s++;
1794 retval = atol((char *)s);
1795 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
1798 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001799 if (use_sandbox)
1800 --sandbox;
1801 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
1803 return retval;
1804}
1805#endif
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 * ":let" list all variable values
1809 * ":let var1 var2" list variable values
1810 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001811 * ":let var += expr" assignment command.
1812 * ":let var -= expr" assignment command.
1813 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 */
1816 void
1817ex_let(eap)
1818 exarg_T *eap;
1819{
1820 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001822 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 int var_count = 0;
1825 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001827 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001828 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaardb552d602006-03-23 22:59:57 +00001830 argend = skip_var_list(arg, &var_count, &semicolon);
1831 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001833 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1834 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001835 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001838 /*
1839 * ":let" without "=": list variables
1840 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 if (*arg == '[')
1842 EMSG(_(e_invarg));
1843 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001845 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001849 list_glob_vars(&first);
1850 list_buf_vars(&first);
1851 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001852#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001853 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001854#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001855 list_script_vars(&first);
1856 list_func_vars(&first);
1857 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 eap->nextcmd = check_nextcmd(arg);
1860 }
1861 else
1862 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001863 op[0] = '=';
1864 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001865 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001866 {
1867 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1868 op[0] = expr[-1]; /* +=, -= or .= */
1869 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (eap->skip)
1873 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (eap->skip)
1876 {
1877 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 --emsg_skip;
1880 }
1881 else if (i != FAIL)
1882 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001884 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 }
1887 }
1888}
1889
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890/*
1891 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1892 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 * When "nextchars" is not NULL it points to a string with characters that
1894 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1895 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 * Returns OK or FAIL;
1897 */
1898 static int
1899ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1900 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001901 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 int copy; /* copy values from "tv", don't move */
1903 int semicolon; /* from skip_var_list() */
1904 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001905 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906{
1907 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001908 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001910 listitem_T *item;
1911 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912
1913 if (*arg != '[')
1914 {
1915 /*
1916 * ":let var = expr" or ":for var in list"
1917 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 return FAIL;
1920 return OK;
1921 }
1922
1923 /*
1924 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1925 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001926 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927 {
1928 EMSG(_(e_listreq));
1929 return FAIL;
1930 }
1931
1932 i = list_len(l);
1933 if (semicolon == 0 && var_count < i)
1934 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001935 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 return FAIL;
1937 }
1938 if (var_count - semicolon > i)
1939 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001940 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 return FAIL;
1942 }
1943
1944 item = l->lv_first;
1945 while (*arg != ']')
1946 {
1947 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 item = item->li_next;
1950 if (arg == NULL)
1951 return FAIL;
1952
1953 arg = skipwhite(arg);
1954 if (*arg == ';')
1955 {
1956 /* Put the rest of the list (may be empty) in the var after ';'.
1957 * Create a new list for this. */
1958 l = list_alloc();
1959 if (l == NULL)
1960 return FAIL;
1961 while (item != NULL)
1962 {
1963 list_append_tv(l, &item->li_tv);
1964 item = item->li_next;
1965 }
1966
1967 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001968 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 ltv.vval.v_list = l;
1970 l->lv_refcount = 1;
1971
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001972 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1973 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 clear_tv(&ltv);
1975 if (arg == NULL)
1976 return FAIL;
1977 break;
1978 }
1979 else if (*arg != ',' && *arg != ']')
1980 {
1981 EMSG2(_(e_intern2), "ex_let_vars()");
1982 return FAIL;
1983 }
1984 }
1985
1986 return OK;
1987}
1988
1989/*
1990 * Skip over assignable variable "var" or list of variables "[var, var]".
1991 * Used for ":let varvar = expr" and ":for varvar in expr".
1992 * For "[var, var]" increment "*var_count" for each variable.
1993 * for "[var, var; var]" set "semicolon".
1994 * Return NULL for an error.
1995 */
1996 static char_u *
1997skip_var_list(arg, var_count, semicolon)
1998 char_u *arg;
1999 int *var_count;
2000 int *semicolon;
2001{
2002 char_u *p, *s;
2003
2004 if (*arg == '[')
2005 {
2006 /* "[var, var]": find the matching ']'. */
2007 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002008 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009 {
2010 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2011 s = skip_var_one(p);
2012 if (s == p)
2013 {
2014 EMSG2(_(e_invarg2), p);
2015 return NULL;
2016 }
2017 ++*var_count;
2018
2019 p = skipwhite(s);
2020 if (*p == ']')
2021 break;
2022 else if (*p == ';')
2023 {
2024 if (*semicolon == 1)
2025 {
2026 EMSG(_("Double ; in list of variables"));
2027 return NULL;
2028 }
2029 *semicolon = 1;
2030 }
2031 else if (*p != ',')
2032 {
2033 EMSG2(_(e_invarg2), p);
2034 return NULL;
2035 }
2036 }
2037 return p + 1;
2038 }
2039 else
2040 return skip_var_one(arg);
2041}
2042
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002043/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002044 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002045 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002046 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047 static char_u *
2048skip_var_one(arg)
2049 char_u *arg;
2050{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002051 if (*arg == '@' && arg[1] != NUL)
2052 return arg + 2;
2053 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2054 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002055}
2056
Bram Moolenaara7043832005-01-21 11:56:39 +00002057/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002058 * List variables for hashtab "ht" with prefix "prefix".
2059 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002060 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002062list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002063 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002064 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002065 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002066 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002067{
Bram Moolenaar33570922005-01-25 22:26:29 +00002068 hashitem_T *hi;
2069 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 int todo;
2071
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002072 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2074 {
2075 if (!HASHITEM_EMPTY(hi))
2076 {
2077 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 di = HI2DI(hi);
2079 if (empty || di->di_tv.v_type != VAR_STRING
2080 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002082 }
2083 }
2084}
2085
2086/*
2087 * List global variables.
2088 */
2089 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002090list_glob_vars(first)
2091 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002092{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002093 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002094}
2095
2096/*
2097 * List buffer variables.
2098 */
2099 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100list_buf_vars(first)
2101 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002103 char_u numbuf[NUMBUFLEN];
2104
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2106 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107
2108 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2110 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002111}
2112
2113/*
2114 * List window variables.
2115 */
2116 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117list_win_vars(first)
2118 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2121 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002122}
2123
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002124#ifdef FEAT_WINDOWS
2125/*
2126 * List tab page variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_tab_vars(first)
2130 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002131{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2133 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002134}
2135#endif
2136
Bram Moolenaara7043832005-01-21 11:56:39 +00002137/*
2138 * List Vim variables.
2139 */
2140 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141list_vim_vars(first)
2142 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145}
2146
2147/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002148 * List script-local variables, if there is a script.
2149 */
2150 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151list_script_vars(first)
2152 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002153{
2154 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2156 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002157}
2158
2159/*
2160 * List function variables, if there is a function.
2161 */
2162 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163list_func_vars(first)
2164 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002165{
2166 if (current_funccal != NULL)
2167 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002169}
2170
2171/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 * List variables in "arg".
2173 */
2174 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176 exarg_T *eap;
2177 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179{
2180 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002181 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002183 char_u *name_start;
2184 char_u *arg_subsc;
2185 char_u *tofree;
2186 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187
2188 while (!ends_excmd(*arg) && !got_int)
2189 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002192 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2194 {
2195 emsg_severe = TRUE;
2196 EMSG(_(e_trailing));
2197 break;
2198 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002202 /* get_name_len() takes care of expanding curly braces */
2203 name_start = name = arg;
2204 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2205 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 /* This is mainly to keep test 49 working: when expanding
2208 * curly braces fails overrule the exception error message. */
2209 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 emsg_severe = TRUE;
2212 EMSG2(_(e_invarg2), arg);
2213 break;
2214 }
2215 error = TRUE;
2216 }
2217 else
2218 {
2219 if (tofree != NULL)
2220 name = tofree;
2221 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 else
2224 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 /* handle d.key, l[idx], f(expr) */
2226 arg_subsc = arg;
2227 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002228 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002230 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002232 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002234 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002235 case 'g': list_glob_vars(first); break;
2236 case 'b': list_buf_vars(first); break;
2237 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002238#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002239 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002240#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 case 'v': list_vim_vars(first); break;
2242 case 's': list_script_vars(first); break;
2243 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 default:
2245 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002246 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 }
2248 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 {
2250 char_u numbuf[NUMBUFLEN];
2251 char_u *tf;
2252 int c;
2253 char_u *s;
2254
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002255 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 c = *arg;
2257 *arg = NUL;
2258 list_one_var_a((char_u *)"",
2259 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 tv.v_type,
2261 s == NULL ? (char_u *)"" : s,
2262 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 *arg = c;
2264 vim_free(tf);
2265 }
2266 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 }
2269 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270
2271 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273
2274 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 }
2276
2277 return arg;
2278}
2279
2280/*
2281 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2282 * Returns a pointer to the char just after the var name.
2283 * Returns NULL if there is an error.
2284 */
2285 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002286ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002288 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002290 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002291 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292{
2293 int c1;
2294 char_u *name;
2295 char_u *p;
2296 char_u *arg_end = NULL;
2297 int len;
2298 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002299 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300
2301 /*
2302 * ":let $VAR = expr": Set environment variable.
2303 */
2304 if (*arg == '$')
2305 {
2306 /* Find the end of the name. */
2307 ++arg;
2308 name = arg;
2309 len = get_env_len(&arg);
2310 if (len == 0)
2311 EMSG2(_(e_invarg2), name - 1);
2312 else
2313 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 if (op != NULL && (*op == '+' || *op == '-'))
2315 EMSG2(_(e_letwrong), op);
2316 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002317 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 EMSG(_(e_letunexp));
2319 else
2320 {
2321 c1 = name[len];
2322 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002323 p = get_tv_string_chk(tv);
2324 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325 {
2326 int mustfree = FALSE;
2327 char_u *s = vim_getenv(name, &mustfree);
2328
2329 if (s != NULL)
2330 {
2331 p = tofree = concat_str(s, p);
2332 if (mustfree)
2333 vim_free(s);
2334 }
2335 }
2336 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002337 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002339 if (STRICMP(name, "HOME") == 0)
2340 init_homedir();
2341 else if (didset_vim && STRICMP(name, "VIM") == 0)
2342 didset_vim = FALSE;
2343 else if (didset_vimruntime
2344 && STRICMP(name, "VIMRUNTIME") == 0)
2345 didset_vimruntime = FALSE;
2346 arg_end = arg;
2347 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002348 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 }
2351 }
2352 }
2353
2354 /*
2355 * ":let &option = expr": Set option value.
2356 * ":let &l:option = expr": Set local option value.
2357 * ":let &g:option = expr": Set global option value.
2358 */
2359 else if (*arg == '&')
2360 {
2361 /* Find the end of the name. */
2362 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002363 if (p == NULL || (endchars != NULL
2364 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 EMSG(_(e_letunexp));
2366 else
2367 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 long n;
2369 int opt_type;
2370 long numval;
2371 char_u *stringval = NULL;
2372 char_u *s;
2373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 c1 = *p;
2375 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002376
2377 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 s = get_tv_string_chk(tv); /* != NULL if number or string */
2379 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 {
2381 opt_type = get_option_value(arg, &numval,
2382 &stringval, opt_flags);
2383 if ((opt_type == 1 && *op == '.')
2384 || (opt_type == 0 && *op != '.'))
2385 EMSG2(_(e_letwrong), op);
2386 else
2387 {
2388 if (opt_type == 1) /* number */
2389 {
2390 if (*op == '+')
2391 n = numval + n;
2392 else
2393 n = numval - n;
2394 }
2395 else if (opt_type == 0 && stringval != NULL) /* string */
2396 {
2397 s = concat_str(stringval, s);
2398 vim_free(stringval);
2399 stringval = s;
2400 }
2401 }
2402 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 if (s != NULL)
2404 {
2405 set_option_value(arg, n, s, opt_flags);
2406 arg_end = p;
2407 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002408 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 }
2411 }
2412
2413 /*
2414 * ":let @r = expr": Set register contents.
2415 */
2416 else if (*arg == '@')
2417 {
2418 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 if (op != NULL && (*op == '+' || *op == '-'))
2420 EMSG2(_(e_letwrong), op);
2421 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002422 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 EMSG(_(e_letunexp));
2424 else
2425 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002426 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 char_u *s;
2428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 p = get_tv_string_chk(tv);
2430 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002432 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433 if (s != NULL)
2434 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002435 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 vim_free(s);
2437 }
2438 }
2439 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 arg_end = arg + 1;
2443 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002444 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002445 }
2446 }
2447
2448 /*
2449 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002452 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002454 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002456 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2460 EMSG(_(e_letunexp));
2461 else
2462 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 arg_end = p;
2465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
2469
2470 else
2471 EMSG2(_(e_invarg2), arg);
2472
2473 return arg_end;
2474}
2475
2476/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2478 */
2479 static int
2480check_changedtick(arg)
2481 char_u *arg;
2482{
2483 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2484 {
2485 EMSG2(_(e_readonlyvar), arg);
2486 return TRUE;
2487 }
2488 return FALSE;
2489}
2490
2491/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 * Get an lval: variable, Dict item or List item that can be assigned a value
2493 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2494 * "name.key", "name.key[expr]" etc.
2495 * Indexing only works if "name" is an existing List or Dictionary.
2496 * "name" points to the start of the name.
2497 * If "rettv" is not NULL it points to the value to be assigned.
2498 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2499 * wrong; must end in space or cmd separator.
2500 *
2501 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002502 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 */
2505 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002506get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002508 typval_T *rettv;
2509 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 int unlet;
2511 int skip;
2512 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002513 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 char_u *expr_start, *expr_end;
2517 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 dictitem_T *v;
2519 typval_T var1;
2520 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002522 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529
2530 if (skip)
2531 {
2532 /* When skipping just find the end of the name. */
2533 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002534 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 }
2536
2537 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002538 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 if (expr_start != NULL)
2540 {
2541 /* Don't expand the name when we already know there is an error. */
2542 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2543 && *p != '[' && *p != '.')
2544 {
2545 EMSG(_(e_trailing));
2546 return NULL;
2547 }
2548
2549 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2550 if (lp->ll_exp_name == NULL)
2551 {
2552 /* Report an invalid expression in braces, unless the
2553 * expression evaluation has been cancelled due to an
2554 * aborting error, an interrupt, or an exception. */
2555 if (!aborting() && !quiet)
2556 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002557 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 EMSG2(_(e_invarg2), name);
2559 return NULL;
2560 }
2561 }
2562 lp->ll_name = lp->ll_exp_name;
2563 }
2564 else
2565 lp->ll_name = name;
2566
2567 /* Without [idx] or .key we are done. */
2568 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2569 return p;
2570
2571 cc = *p;
2572 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002573 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 if (v == NULL && !quiet)
2575 EMSG2(_(e_undefvar), lp->ll_name);
2576 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 if (v == NULL)
2578 return NULL;
2579
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 /*
2581 * Loop until no more [idx] or .key is following.
2582 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002583 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2587 && !(lp->ll_tv->v_type == VAR_DICT
2588 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (!quiet)
2591 EMSG(_("E689: Can only index a List or Dictionary"));
2592 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (!quiet)
2597 EMSG(_("E708: [:] must come last"));
2598 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002600
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 len = -1;
2602 if (*p == '.')
2603 {
2604 key = p + 1;
2605 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2606 ;
2607 if (len == 0)
2608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (!quiet)
2610 EMSG(_(e_emptykey));
2611 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 }
2613 p = key + len;
2614 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615 else
2616 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002618 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 if (*p == ':')
2620 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002621 else
2622 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 empty1 = FALSE;
2624 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002626 if (get_tv_string_chk(&var1) == NULL)
2627 {
2628 /* not a number or string */
2629 clear_tv(&var1);
2630 return NULL;
2631 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 }
2633
2634 /* Optionally get the second index [ :expr]. */
2635 if (*p == ':')
2636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002640 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002641 if (!empty1)
2642 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (rettv != NULL && (rettv->v_type != VAR_LIST
2646 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (!quiet)
2649 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 if (!empty1)
2651 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 }
2654 p = skipwhite(p + 1);
2655 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 else
2658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2661 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 if (!empty1)
2663 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002666 if (get_tv_string_chk(&var2) == NULL)
2667 {
2668 /* not a number or string */
2669 if (!empty1)
2670 clear_tv(&var1);
2671 clear_tv(&var2);
2672 return NULL;
2673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
2683 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 if (!empty1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
2690
2691 /* Skip to past ']'. */
2692 ++p;
2693 }
2694
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 {
2697 if (len == -1)
2698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002700 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 if (*key == NUL)
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (!quiet)
2704 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 }
2708 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002709 lp->ll_list = NULL;
2710 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002711 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002714 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 if (len == -1)
2720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (len == -1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 p = NULL;
2731 break;
2732 }
2733 if (len == -1)
2734 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 }
2737 else
2738 {
2739 /*
2740 * Get the number and item for the only or first index of the List.
2741 */
2742 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 else
2745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002746 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 clear_tv(&var1);
2748 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002749 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 lp->ll_list = lp->ll_tv->vval.v_list;
2751 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2752 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002754 if (lp->ll_n1 < 0)
2755 {
2756 lp->ll_n1 = 0;
2757 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2758 }
2759 }
2760 if (lp->ll_li == NULL)
2761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 }
2766
2767 /*
2768 * May need to find the item or absolute index for the second
2769 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 * When no index given: "lp->ll_empty2" is TRUE.
2771 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002775 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
2784
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2786 if (lp->ll_n1 < 0)
2787 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2788 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002790 }
2791
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002793 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002794 }
2795
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 return p;
2797}
2798
2799/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 */
2802 static void
2803clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002804 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805{
2806 vim_free(lp->ll_exp_name);
2807 vim_free(lp->ll_newkey);
2808}
2809
2810/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002811 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 */
2815 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002816set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002817 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002819 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822{
2823 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002824 listitem_T *ri;
2825 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826
2827 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002828 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002830 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 cc = *endp;
2832 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002833 if (op != NULL && *op != '=')
2834 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002835 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002836
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002837 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002838 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002839 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002840 {
2841 if (tv_op(&tv, rettv, op) == OK)
2842 set_var(lp->ll_name, &tv, FALSE);
2843 clear_tv(&tv);
2844 }
2845 }
2846 else
2847 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002849 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002851 else if (tv_check_lock(lp->ll_newkey == NULL
2852 ? lp->ll_tv->v_lock
2853 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2854 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 else if (lp->ll_range)
2856 {
2857 /*
2858 * Assign the List values to the list items.
2859 */
2860 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002861 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002862 if (op != NULL && *op != '=')
2863 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2864 else
2865 {
2866 clear_tv(&lp->ll_li->li_tv);
2867 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2868 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 ri = ri->li_next;
2870 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2871 break;
2872 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002875 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 ri = NULL;
2878 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002879 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 lp->ll_li = lp->ll_li->li_next;
2882 ++lp->ll_n1;
2883 }
2884 if (ri != NULL)
2885 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002886 else if (lp->ll_empty2
2887 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 : lp->ll_n1 != lp->ll_n2)
2889 EMSG(_("E711: List value has not enough items"));
2890 }
2891 else
2892 {
2893 /*
2894 * Assign to a List or Dictionary item.
2895 */
2896 if (lp->ll_newkey != NULL)
2897 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002898 if (op != NULL && *op != '=')
2899 {
2900 EMSG2(_(e_letwrong), op);
2901 return;
2902 }
2903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002905 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 if (di == NULL)
2907 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002908 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2909 {
2910 vim_free(di);
2911 return;
2912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002914 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915 else if (op != NULL && *op != '=')
2916 {
2917 tv_op(lp->ll_tv, rettv, op);
2918 return;
2919 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002920 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002922
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 /*
2924 * Assign the value to the variable or list item.
2925 */
2926 if (copy)
2927 copy_tv(rettv, lp->ll_tv);
2928 else
2929 {
2930 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002931 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 }
2934 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935}
2936
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002937/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2939 * Returns OK or FAIL.
2940 */
2941 static int
2942tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002943 typval_T *tv1;
2944 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 char_u *op;
2946{
2947 long n;
2948 char_u numbuf[NUMBUFLEN];
2949 char_u *s;
2950
2951 /* Can't do anything with a Funcref or a Dict on the right. */
2952 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2953 {
2954 switch (tv1->v_type)
2955 {
2956 case VAR_DICT:
2957 case VAR_FUNC:
2958 break;
2959
2960 case VAR_LIST:
2961 if (*op != '+' || tv2->v_type != VAR_LIST)
2962 break;
2963 /* List += List */
2964 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2965 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2966 return OK;
2967
2968 case VAR_NUMBER:
2969 case VAR_STRING:
2970 if (tv2->v_type == VAR_LIST)
2971 break;
2972 if (*op == '+' || *op == '-')
2973 {
2974 /* nr += nr or nr -= nr*/
2975 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002976#ifdef FEAT_FLOAT
2977 if (tv2->v_type == VAR_FLOAT)
2978 {
2979 float_T f = n;
2980
2981 if (*op == '+')
2982 f += tv2->vval.v_float;
2983 else
2984 f -= tv2->vval.v_float;
2985 clear_tv(tv1);
2986 tv1->v_type = VAR_FLOAT;
2987 tv1->vval.v_float = f;
2988 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002990#endif
2991 {
2992 if (*op == '+')
2993 n += get_tv_number(tv2);
2994 else
2995 n -= get_tv_number(tv2);
2996 clear_tv(tv1);
2997 tv1->v_type = VAR_NUMBER;
2998 tv1->vval.v_number = n;
2999 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 }
3001 else
3002 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003003 if (tv2->v_type == VAR_FLOAT)
3004 break;
3005
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 /* str .= str */
3007 s = get_tv_string(tv1);
3008 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3009 clear_tv(tv1);
3010 tv1->v_type = VAR_STRING;
3011 tv1->vval.v_string = s;
3012 }
3013 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003014
3015#ifdef FEAT_FLOAT
3016 case VAR_FLOAT:
3017 {
3018 float_T f;
3019
3020 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3021 && tv2->v_type != VAR_NUMBER
3022 && tv2->v_type != VAR_STRING))
3023 break;
3024 if (tv2->v_type == VAR_FLOAT)
3025 f = tv2->vval.v_float;
3026 else
3027 f = get_tv_number(tv2);
3028 if (*op == '+')
3029 tv1->vval.v_float += f;
3030 else
3031 tv1->vval.v_float -= f;
3032 }
3033 return OK;
3034#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003035 }
3036 }
3037
3038 EMSG2(_(e_letwrong), op);
3039 return FAIL;
3040}
3041
3042/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003043 * Add a watcher to a list.
3044 */
3045 static void
3046list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003047 list_T *l;
3048 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003049{
3050 lw->lw_next = l->lv_watch;
3051 l->lv_watch = lw;
3052}
3053
3054/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003055 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003056 * No warning when it isn't found...
3057 */
3058 static void
3059list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003060 list_T *l;
3061 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003062{
Bram Moolenaar33570922005-01-25 22:26:29 +00003063 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003064
3065 lwp = &l->lv_watch;
3066 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3067 {
3068 if (lw == lwrem)
3069 {
3070 *lwp = lw->lw_next;
3071 break;
3072 }
3073 lwp = &lw->lw_next;
3074 }
3075}
3076
3077/*
3078 * Just before removing an item from a list: advance watchers to the next
3079 * item.
3080 */
3081 static void
3082list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003083 list_T *l;
3084 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003085{
Bram Moolenaar33570922005-01-25 22:26:29 +00003086 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003087
3088 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3089 if (lw->lw_item == item)
3090 lw->lw_item = item->li_next;
3091}
3092
3093/*
3094 * Evaluate the expression used in a ":for var in expr" command.
3095 * "arg" points to "var".
3096 * Set "*errp" to TRUE for an error, FALSE otherwise;
3097 * Return a pointer that holds the info. Null when there is an error.
3098 */
3099 void *
3100eval_for_line(arg, errp, nextcmdp, skip)
3101 char_u *arg;
3102 int *errp;
3103 char_u **nextcmdp;
3104 int skip;
3105{
Bram Moolenaar33570922005-01-25 22:26:29 +00003106 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003108 typval_T tv;
3109 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110
3111 *errp = TRUE; /* default: there is an error */
3112
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114 if (fi == NULL)
3115 return NULL;
3116
3117 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3118 if (expr == NULL)
3119 return fi;
3120
3121 expr = skipwhite(expr);
3122 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3123 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003124 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003125 return fi;
3126 }
3127
3128 if (skip)
3129 ++emsg_skip;
3130 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3131 {
3132 *errp = FALSE;
3133 if (!skip)
3134 {
3135 l = tv.vval.v_list;
3136 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003137 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003138 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003139 clear_tv(&tv);
3140 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003141 else
3142 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003143 /* No need to increment the refcount, it's already set for the
3144 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145 fi->fi_list = l;
3146 list_add_watch(l, &fi->fi_lw);
3147 fi->fi_lw.lw_item = l->lv_first;
3148 }
3149 }
3150 }
3151 if (skip)
3152 --emsg_skip;
3153
3154 return fi;
3155}
3156
3157/*
3158 * Use the first item in a ":for" list. Advance to the next.
3159 * Assign the values to the variable (list). "arg" points to the first one.
3160 * Return TRUE when a valid item was found, FALSE when at end of list or
3161 * something wrong.
3162 */
3163 int
3164next_for_item(fi_void, arg)
3165 void *fi_void;
3166 char_u *arg;
3167{
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171
3172 item = fi->fi_lw.lw_item;
3173 if (item == NULL)
3174 result = FALSE;
3175 else
3176 {
3177 fi->fi_lw.lw_item = item->li_next;
3178 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3179 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3180 }
3181 return result;
3182}
3183
3184/*
3185 * Free the structure used to store info used by ":for".
3186 */
3187 void
3188free_for_info(fi_void)
3189 void *fi_void;
3190{
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003193 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003194 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003196 list_unref(fi->fi_list);
3197 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 vim_free(fi);
3199}
3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3202
3203 void
3204set_context_for_expression(xp, arg, cmdidx)
3205 expand_T *xp;
3206 char_u *arg;
3207 cmdidx_T cmdidx;
3208{
3209 int got_eq = FALSE;
3210 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 if (cmdidx == CMD_let)
3214 {
3215 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003216 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 {
3218 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003219 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 {
3221 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223 if (vim_iswhite(*p))
3224 break;
3225 }
3226 return;
3227 }
3228 }
3229 else
3230 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3231 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 while ((xp->xp_pattern = vim_strpbrk(arg,
3233 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3234 {
3235 c = *xp->xp_pattern;
3236 if (c == '&')
3237 {
3238 c = xp->xp_pattern[1];
3239 if (c == '&')
3240 {
3241 ++xp->xp_pattern;
3242 xp->xp_context = cmdidx != CMD_let || got_eq
3243 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3244 }
3245 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003248 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3249 xp->xp_pattern += 2;
3250
3251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 }
3253 else if (c == '$')
3254 {
3255 /* environment variable */
3256 xp->xp_context = EXPAND_ENV_VARS;
3257 }
3258 else if (c == '=')
3259 {
3260 got_eq = TRUE;
3261 xp->xp_context = EXPAND_EXPRESSION;
3262 }
3263 else if (c == '<'
3264 && xp->xp_context == EXPAND_FUNCTIONS
3265 && vim_strchr(xp->xp_pattern, '(') == NULL)
3266 {
3267 /* Function name can start with "<SNR>" */
3268 break;
3269 }
3270 else if (cmdidx != CMD_let || got_eq)
3271 {
3272 if (c == '"') /* string */
3273 {
3274 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3275 if (c == '\\' && xp->xp_pattern[1] != NUL)
3276 ++xp->xp_pattern;
3277 xp->xp_context = EXPAND_NOTHING;
3278 }
3279 else if (c == '\'') /* literal string */
3280 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003281 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3283 /* skip */ ;
3284 xp->xp_context = EXPAND_NOTHING;
3285 }
3286 else if (c == '|')
3287 {
3288 if (xp->xp_pattern[1] == '|')
3289 {
3290 ++xp->xp_pattern;
3291 xp->xp_context = EXPAND_EXPRESSION;
3292 }
3293 else
3294 xp->xp_context = EXPAND_COMMANDS;
3295 }
3296 else
3297 xp->xp_context = EXPAND_EXPRESSION;
3298 }
3299 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300 /* Doesn't look like something valid, expand as an expression
3301 * anyway. */
3302 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 arg = xp->xp_pattern;
3304 if (*arg != NUL)
3305 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3306 /* skip */ ;
3307 }
3308 xp->xp_pattern = arg;
3309}
3310
3311#endif /* FEAT_CMDL_COMPL */
3312
3313/*
3314 * ":1,25call func(arg1, arg2)" function call.
3315 */
3316 void
3317ex_call(eap)
3318 exarg_T *eap;
3319{
3320 char_u *arg = eap->arg;
3321 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003323 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003325 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 linenr_T lnum;
3327 int doesrange;
3328 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003329 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003331 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003332 if (fudi.fd_newkey != NULL)
3333 {
3334 /* Still need to give an error message for missing key. */
3335 EMSG2(_(e_dictkey), fudi.fd_newkey);
3336 vim_free(fudi.fd_newkey);
3337 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003338 if (tofree == NULL)
3339 return;
3340
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003341 /* Increase refcount on dictionary, it could get deleted when evaluating
3342 * the arguments. */
3343 if (fudi.fd_dict != NULL)
3344 ++fudi.fd_dict->dv_refcount;
3345
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003346 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003347 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003348 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
Bram Moolenaar532c7802005-01-27 14:44:31 +00003350 /* Skip white space to allow ":call func ()". Not good, but required for
3351 * backward compatibility. */
3352 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003353 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
3355 if (*startarg != '(')
3356 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003357 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 goto end;
3359 }
3360
3361 /*
3362 * When skipping, evaluate the function once, to find the end of the
3363 * arguments.
3364 * When the function takes a range, this is discovered after the first
3365 * call, and the loop is broken.
3366 */
3367 if (eap->skip)
3368 {
3369 ++emsg_skip;
3370 lnum = eap->line2; /* do it once, also with an invalid range */
3371 }
3372 else
3373 lnum = eap->line1;
3374 for ( ; lnum <= eap->line2; ++lnum)
3375 {
3376 if (!eap->skip && eap->addr_count > 0)
3377 {
3378 curwin->w_cursor.lnum = lnum;
3379 curwin->w_cursor.col = 0;
3380 }
3381 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003382 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003383 eap->line1, eap->line2, &doesrange,
3384 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
3386 failed = TRUE;
3387 break;
3388 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003389
3390 /* Handle a function returning a Funcref, Dictionary or List. */
3391 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3392 {
3393 failed = TRUE;
3394 break;
3395 }
3396
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003397 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (doesrange || eap->skip)
3399 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003402 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003403 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003404 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 if (aborting())
3406 break;
3407 }
3408 if (eap->skip)
3409 --emsg_skip;
3410
3411 if (!failed)
3412 {
3413 /* Check for trailing illegal characters and a following command. */
3414 if (!ends_excmd(*arg))
3415 {
3416 emsg_severe = TRUE;
3417 EMSG(_(e_trailing));
3418 }
3419 else
3420 eap->nextcmd = check_nextcmd(arg);
3421 }
3422
3423end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003424 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003425 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426}
3427
3428/*
3429 * ":unlet[!] var1 ... " command.
3430 */
3431 void
3432ex_unlet(eap)
3433 exarg_T *eap;
3434{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003435 ex_unletlock(eap, eap->arg, 0);
3436}
3437
3438/*
3439 * ":lockvar" and ":unlockvar" commands
3440 */
3441 void
3442ex_lockvar(eap)
3443 exarg_T *eap;
3444{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003446 int deep = 2;
3447
3448 if (eap->forceit)
3449 deep = -1;
3450 else if (vim_isdigit(*arg))
3451 {
3452 deep = getdigits(&arg);
3453 arg = skipwhite(arg);
3454 }
3455
3456 ex_unletlock(eap, arg, deep);
3457}
3458
3459/*
3460 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3461 */
3462 static void
3463ex_unletlock(eap, argstart, deep)
3464 exarg_T *eap;
3465 char_u *argstart;
3466 int deep;
3467{
3468 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003471 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
3473 do
3474 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003475 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003476 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3477 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003478 if (lv.ll_name == NULL)
3479 error = TRUE; /* error but continue parsing */
3480 if (name_end == NULL || (!vim_iswhite(*name_end)
3481 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003483 if (name_end != NULL)
3484 {
3485 emsg_severe = TRUE;
3486 EMSG(_(e_trailing));
3487 }
3488 if (!(eap->skip || error))
3489 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 break;
3491 }
3492
3493 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003494 {
3495 if (eap->cmdidx == CMD_unlet)
3496 {
3497 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3498 error = TRUE;
3499 }
3500 else
3501 {
3502 if (do_lock_var(&lv, name_end, deep,
3503 eap->cmdidx == CMD_lockvar) == FAIL)
3504 error = TRUE;
3505 }
3506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003508 if (!eap->skip)
3509 clear_lval(&lv);
3510
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 arg = skipwhite(name_end);
3512 } while (!ends_excmd(*arg));
3513
3514 eap->nextcmd = check_nextcmd(arg);
3515}
3516
Bram Moolenaar8c711452005-01-14 21:53:12 +00003517 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003518do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003520 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003521 int forceit;
3522{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003523 int ret = OK;
3524 int cc;
3525
3526 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003528 cc = *name_end;
3529 *name_end = NUL;
3530
3531 /* Normal name or expanded name. */
3532 if (check_changedtick(lp->ll_name))
3533 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003535 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003536 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003538 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3539 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003540 else if (lp->ll_range)
3541 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003542 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003543
3544 /* Delete a range of List items. */
3545 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3546 {
3547 li = lp->ll_li->li_next;
3548 listitem_remove(lp->ll_list, lp->ll_li);
3549 lp->ll_li = li;
3550 ++lp->ll_n1;
3551 }
3552 }
3553 else
3554 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003555 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 /* unlet a List item. */
3557 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003558 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003559 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003560 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 }
3562
3563 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003564}
3565
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566/*
3567 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003568 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 */
3570 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003571do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003573 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
Bram Moolenaar33570922005-01-25 22:26:29 +00003575 hashtab_T *ht;
3576 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003577 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003578 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
Bram Moolenaar33570922005-01-25 22:26:29 +00003580 ht = find_var_ht(name, &varname);
3581 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003583 hi = hash_find(ht, varname);
3584 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003585 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003586 di = HI2DI(hi);
3587 if (var_check_fixed(di->di_flags, name)
3588 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 return FAIL;
3590 delete_var(ht, hi);
3591 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 if (forceit)
3595 return OK;
3596 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 return FAIL;
3598}
3599
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003600/*
3601 * Lock or unlock variable indicated by "lp".
3602 * "deep" is the levels to go (-1 for unlimited);
3603 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3604 */
3605 static int
3606do_lock_var(lp, name_end, deep, lock)
3607 lval_T *lp;
3608 char_u *name_end;
3609 int deep;
3610 int lock;
3611{
3612 int ret = OK;
3613 int cc;
3614 dictitem_T *di;
3615
3616 if (deep == 0) /* nothing to do */
3617 return OK;
3618
3619 if (lp->ll_tv == NULL)
3620 {
3621 cc = *name_end;
3622 *name_end = NUL;
3623
3624 /* Normal name or expanded name. */
3625 if (check_changedtick(lp->ll_name))
3626 ret = FAIL;
3627 else
3628 {
3629 di = find_var(lp->ll_name, NULL);
3630 if (di == NULL)
3631 ret = FAIL;
3632 else
3633 {
3634 if (lock)
3635 di->di_flags |= DI_FLAGS_LOCK;
3636 else
3637 di->di_flags &= ~DI_FLAGS_LOCK;
3638 item_lock(&di->di_tv, deep, lock);
3639 }
3640 }
3641 *name_end = cc;
3642 }
3643 else if (lp->ll_range)
3644 {
3645 listitem_T *li = lp->ll_li;
3646
3647 /* (un)lock a range of List items. */
3648 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3649 {
3650 item_lock(&li->li_tv, deep, lock);
3651 li = li->li_next;
3652 ++lp->ll_n1;
3653 }
3654 }
3655 else if (lp->ll_list != NULL)
3656 /* (un)lock a List item. */
3657 item_lock(&lp->ll_li->li_tv, deep, lock);
3658 else
3659 /* un(lock) a Dictionary item. */
3660 item_lock(&lp->ll_di->di_tv, deep, lock);
3661
3662 return ret;
3663}
3664
3665/*
3666 * Lock or unlock an item. "deep" is nr of levels to go.
3667 */
3668 static void
3669item_lock(tv, deep, lock)
3670 typval_T *tv;
3671 int deep;
3672 int lock;
3673{
3674 static int recurse = 0;
3675 list_T *l;
3676 listitem_T *li;
3677 dict_T *d;
3678 hashitem_T *hi;
3679 int todo;
3680
3681 if (recurse >= DICT_MAXNEST)
3682 {
3683 EMSG(_("E743: variable nested too deep for (un)lock"));
3684 return;
3685 }
3686 if (deep == 0)
3687 return;
3688 ++recurse;
3689
3690 /* lock/unlock the item itself */
3691 if (lock)
3692 tv->v_lock |= VAR_LOCKED;
3693 else
3694 tv->v_lock &= ~VAR_LOCKED;
3695
3696 switch (tv->v_type)
3697 {
3698 case VAR_LIST:
3699 if ((l = tv->vval.v_list) != NULL)
3700 {
3701 if (lock)
3702 l->lv_lock |= VAR_LOCKED;
3703 else
3704 l->lv_lock &= ~VAR_LOCKED;
3705 if (deep < 0 || deep > 1)
3706 /* recursive: lock/unlock the items the List contains */
3707 for (li = l->lv_first; li != NULL; li = li->li_next)
3708 item_lock(&li->li_tv, deep - 1, lock);
3709 }
3710 break;
3711 case VAR_DICT:
3712 if ((d = tv->vval.v_dict) != NULL)
3713 {
3714 if (lock)
3715 d->dv_lock |= VAR_LOCKED;
3716 else
3717 d->dv_lock &= ~VAR_LOCKED;
3718 if (deep < 0 || deep > 1)
3719 {
3720 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003721 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003722 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3723 {
3724 if (!HASHITEM_EMPTY(hi))
3725 {
3726 --todo;
3727 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3728 }
3729 }
3730 }
3731 }
3732 }
3733 --recurse;
3734}
3735
Bram Moolenaara40058a2005-07-11 22:42:07 +00003736/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003737 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3738 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003739 */
3740 static int
3741tv_islocked(tv)
3742 typval_T *tv;
3743{
3744 return (tv->v_lock & VAR_LOCKED)
3745 || (tv->v_type == VAR_LIST
3746 && tv->vval.v_list != NULL
3747 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3748 || (tv->v_type == VAR_DICT
3749 && tv->vval.v_dict != NULL
3750 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3751}
3752
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3754/*
3755 * Delete all "menutrans_" variables.
3756 */
3757 void
3758del_menutrans_vars()
3759{
Bram Moolenaar33570922005-01-25 22:26:29 +00003760 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003761 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
Bram Moolenaar33570922005-01-25 22:26:29 +00003763 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003764 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003765 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003766 {
3767 if (!HASHITEM_EMPTY(hi))
3768 {
3769 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3771 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003772 }
3773 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003774 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775}
3776#endif
3777
3778#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3779
3780/*
3781 * Local string buffer for the next two functions to store a variable name
3782 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3783 * get_user_var_name().
3784 */
3785
3786static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3787
3788static char_u *varnamebuf = NULL;
3789static int varnamebuflen = 0;
3790
3791/*
3792 * Function to concatenate a prefix and a variable name.
3793 */
3794 static char_u *
3795cat_prefix_varname(prefix, name)
3796 int prefix;
3797 char_u *name;
3798{
3799 int len;
3800
3801 len = (int)STRLEN(name) + 3;
3802 if (len > varnamebuflen)
3803 {
3804 vim_free(varnamebuf);
3805 len += 10; /* some additional space */
3806 varnamebuf = alloc(len);
3807 if (varnamebuf == NULL)
3808 {
3809 varnamebuflen = 0;
3810 return NULL;
3811 }
3812 varnamebuflen = len;
3813 }
3814 *varnamebuf = prefix;
3815 varnamebuf[1] = ':';
3816 STRCPY(varnamebuf + 2, name);
3817 return varnamebuf;
3818}
3819
3820/*
3821 * Function given to ExpandGeneric() to obtain the list of user defined
3822 * (global/buffer/window/built-in) variable names.
3823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 char_u *
3825get_user_var_name(xp, idx)
3826 expand_T *xp;
3827 int idx;
3828{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003829 static long_u gdone;
3830 static long_u bdone;
3831 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003832#ifdef FEAT_WINDOWS
3833 static long_u tdone;
3834#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003835 static int vidx;
3836 static hashitem_T *hi;
3837 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
3839 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003840 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003841 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003842#ifdef FEAT_WINDOWS
3843 tdone = 0;
3844#endif
3845 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003846
3847 /* Global variables */
3848 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003851 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003852 else
3853 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003854 while (HASHITEM_EMPTY(hi))
3855 ++hi;
3856 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3857 return cat_prefix_varname('g', hi->hi_key);
3858 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003860
3861 /* b: variables */
3862 ht = &curbuf->b_vars.dv_hashtab;
3863 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003865 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003866 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003867 else
3868 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003869 while (HASHITEM_EMPTY(hi))
3870 ++hi;
3871 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003873 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003875 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 return (char_u *)"b:changedtick";
3877 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003878
3879 /* w: variables */
3880 ht = &curwin->w_vars.dv_hashtab;
3881 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003883 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003884 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003885 else
3886 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003887 while (HASHITEM_EMPTY(hi))
3888 ++hi;
3889 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003891
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003892#ifdef FEAT_WINDOWS
3893 /* t: variables */
3894 ht = &curtab->tp_vars.dv_hashtab;
3895 if (tdone < ht->ht_used)
3896 {
3897 if (tdone++ == 0)
3898 hi = ht->ht_array;
3899 else
3900 ++hi;
3901 while (HASHITEM_EMPTY(hi))
3902 ++hi;
3903 return cat_prefix_varname('t', hi->hi_key);
3904 }
3905#endif
3906
Bram Moolenaar33570922005-01-25 22:26:29 +00003907 /* v: variables */
3908 if (vidx < VV_LEN)
3909 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
3911 vim_free(varnamebuf);
3912 varnamebuf = NULL;
3913 varnamebuflen = 0;
3914 return NULL;
3915}
3916
3917#endif /* FEAT_CMDL_COMPL */
3918
3919/*
3920 * types for expressions.
3921 */
3922typedef enum
3923{
3924 TYPE_UNKNOWN = 0
3925 , TYPE_EQUAL /* == */
3926 , TYPE_NEQUAL /* != */
3927 , TYPE_GREATER /* > */
3928 , TYPE_GEQUAL /* >= */
3929 , TYPE_SMALLER /* < */
3930 , TYPE_SEQUAL /* <= */
3931 , TYPE_MATCH /* =~ */
3932 , TYPE_NOMATCH /* !~ */
3933} exptype_T;
3934
3935/*
3936 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003937 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3939 */
3940
3941/*
3942 * Handle zero level expression.
3943 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003944 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003945 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 * Return OK or FAIL.
3947 */
3948 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003949eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 char_u **nextcmd;
3953 int evaluate;
3954{
3955 int ret;
3956 char_u *p;
3957
3958 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003959 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (ret == FAIL || !ends_excmd(*p))
3961 {
3962 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003963 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 /*
3965 * Report the invalid expression unless the expression evaluation has
3966 * been cancelled due to an aborting error, an interrupt, or an
3967 * exception.
3968 */
3969 if (!aborting())
3970 EMSG2(_(e_invexpr2), arg);
3971 ret = FAIL;
3972 }
3973 if (nextcmd != NULL)
3974 *nextcmd = check_nextcmd(p);
3975
3976 return ret;
3977}
3978
3979/*
3980 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003981 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 *
3983 * "arg" must point to the first non-white of the expression.
3984 * "arg" is advanced to the next non-white after the recognized expression.
3985 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003986 * Note: "rettv.v_lock" is not set.
3987 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 * Return OK or FAIL.
3989 */
3990 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003991eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 int evaluate;
3995{
3996 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999 /*
4000 * Get the first variable.
4001 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004002 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 return FAIL;
4004
4005 if ((*arg)[0] == '?')
4006 {
4007 result = FALSE;
4008 if (evaluate)
4009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004010 int error = FALSE;
4011
4012 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004014 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 if (error)
4016 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018
4019 /*
4020 * Get the second variable.
4021 */
4022 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 return FAIL;
4025
4026 /*
4027 * Check for the ":".
4028 */
4029 if ((*arg)[0] != ':')
4030 {
4031 EMSG(_("E109: Missing ':' after '?'"));
4032 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return FAIL;
4035 }
4036
4037 /*
4038 * Get the third variable.
4039 */
4040 *arg = skipwhite(*arg + 1);
4041 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4042 {
4043 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 return FAIL;
4046 }
4047 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050
4051 return OK;
4052}
4053
4054/*
4055 * Handle first level expression:
4056 * expr2 || expr2 || expr2 logical OR
4057 *
4058 * "arg" must point to the first non-white of the expression.
4059 * "arg" is advanced to the next non-white after the recognized expression.
4060 *
4061 * Return OK or FAIL.
4062 */
4063 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 int evaluate;
4068{
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 long result;
4071 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004072 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
4074 /*
4075 * Get the first variable.
4076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return FAIL;
4079
4080 /*
4081 * Repeat until there is no following "||".
4082 */
4083 first = TRUE;
4084 result = FALSE;
4085 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4086 {
4087 if (evaluate && first)
4088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004089 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004092 if (error)
4093 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 first = FALSE;
4095 }
4096
4097 /*
4098 * Get the second variable.
4099 */
4100 *arg = skipwhite(*arg + 2);
4101 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4102 return FAIL;
4103
4104 /*
4105 * Compute the result.
4106 */
4107 if (evaluate && !result)
4108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004112 if (error)
4113 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115 if (evaluate)
4116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 rettv->v_type = VAR_NUMBER;
4118 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120 }
4121
4122 return OK;
4123}
4124
4125/*
4126 * Handle second level expression:
4127 * expr3 && expr3 && expr3 logical AND
4128 *
4129 * "arg" must point to the first non-white of the expression.
4130 * "arg" is advanced to the next non-white after the recognized expression.
4131 *
4132 * Return OK or FAIL.
4133 */
4134 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 int evaluate;
4139{
Bram Moolenaar33570922005-01-25 22:26:29 +00004140 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 long result;
4142 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004143 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 /*
4146 * Get the first variable.
4147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 return FAIL;
4150
4151 /*
4152 * Repeat until there is no following "&&".
4153 */
4154 first = TRUE;
4155 result = TRUE;
4156 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4157 {
4158 if (evaluate && first)
4159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004160 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004162 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004163 if (error)
4164 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 first = FALSE;
4166 }
4167
4168 /*
4169 * Get the second variable.
4170 */
4171 *arg = skipwhite(*arg + 2);
4172 if (eval4(arg, &var2, evaluate && result) == FAIL)
4173 return FAIL;
4174
4175 /*
4176 * Compute the result.
4177 */
4178 if (evaluate && result)
4179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004180 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004183 if (error)
4184 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186 if (evaluate)
4187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 rettv->v_type = VAR_NUMBER;
4189 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 }
4192
4193 return OK;
4194}
4195
4196/*
4197 * Handle third level expression:
4198 * var1 == var2
4199 * var1 =~ var2
4200 * var1 != var2
4201 * var1 !~ var2
4202 * var1 > var2
4203 * var1 >= var2
4204 * var1 < var2
4205 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004206 * var1 is var2
4207 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 *
4209 * "arg" must point to the first non-white of the expression.
4210 * "arg" is advanced to the next non-white after the recognized expression.
4211 *
4212 * Return OK or FAIL.
4213 */
4214 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 int evaluate;
4219{
Bram Moolenaar33570922005-01-25 22:26:29 +00004220 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 char_u *p;
4222 int i;
4223 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004224 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 int len = 2;
4226 long n1, n2;
4227 char_u *s1, *s2;
4228 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4229 regmatch_T regmatch;
4230 int ic;
4231 char_u *save_cpo;
4232
4233 /*
4234 * Get the first variable.
4235 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 return FAIL;
4238
4239 p = *arg;
4240 switch (p[0])
4241 {
4242 case '=': if (p[1] == '=')
4243 type = TYPE_EQUAL;
4244 else if (p[1] == '~')
4245 type = TYPE_MATCH;
4246 break;
4247 case '!': if (p[1] == '=')
4248 type = TYPE_NEQUAL;
4249 else if (p[1] == '~')
4250 type = TYPE_NOMATCH;
4251 break;
4252 case '>': if (p[1] != '=')
4253 {
4254 type = TYPE_GREATER;
4255 len = 1;
4256 }
4257 else
4258 type = TYPE_GEQUAL;
4259 break;
4260 case '<': if (p[1] != '=')
4261 {
4262 type = TYPE_SMALLER;
4263 len = 1;
4264 }
4265 else
4266 type = TYPE_SEQUAL;
4267 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004268 case 'i': if (p[1] == 's')
4269 {
4270 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4271 len = 5;
4272 if (!vim_isIDc(p[len]))
4273 {
4274 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4275 type_is = TRUE;
4276 }
4277 }
4278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280
4281 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004282 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 */
4284 if (type != TYPE_UNKNOWN)
4285 {
4286 /* extra question mark appended: ignore case */
4287 if (p[len] == '?')
4288 {
4289 ic = TRUE;
4290 ++len;
4291 }
4292 /* extra '#' appended: match case */
4293 else if (p[len] == '#')
4294 {
4295 ic = FALSE;
4296 ++len;
4297 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004298 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 else
4300 ic = p_ic;
4301
4302 /*
4303 * Get the second variable.
4304 */
4305 *arg = skipwhite(p + len);
4306 if (eval5(arg, &var2, evaluate) == FAIL)
4307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 return FAIL;
4310 }
4311
4312 if (evaluate)
4313 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004314 if (type_is && rettv->v_type != var2.v_type)
4315 {
4316 /* For "is" a different type always means FALSE, for "notis"
4317 * it means TRUE. */
4318 n1 = (type == TYPE_NEQUAL);
4319 }
4320 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4321 {
4322 if (type_is)
4323 {
4324 n1 = (rettv->v_type == var2.v_type
4325 && rettv->vval.v_list == var2.vval.v_list);
4326 if (type == TYPE_NEQUAL)
4327 n1 = !n1;
4328 }
4329 else if (rettv->v_type != var2.v_type
4330 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4331 {
4332 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004333 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004334 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004335 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004336 clear_tv(rettv);
4337 clear_tv(&var2);
4338 return FAIL;
4339 }
4340 else
4341 {
4342 /* Compare two Lists for being equal or unequal. */
4343 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4344 if (type == TYPE_NEQUAL)
4345 n1 = !n1;
4346 }
4347 }
4348
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004349 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4350 {
4351 if (type_is)
4352 {
4353 n1 = (rettv->v_type == var2.v_type
4354 && rettv->vval.v_dict == var2.vval.v_dict);
4355 if (type == TYPE_NEQUAL)
4356 n1 = !n1;
4357 }
4358 else if (rettv->v_type != var2.v_type
4359 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4360 {
4361 if (rettv->v_type != var2.v_type)
4362 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4363 else
4364 EMSG(_("E736: Invalid operation for Dictionary"));
4365 clear_tv(rettv);
4366 clear_tv(&var2);
4367 return FAIL;
4368 }
4369 else
4370 {
4371 /* Compare two Dictionaries for being equal or unequal. */
4372 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4373 if (type == TYPE_NEQUAL)
4374 n1 = !n1;
4375 }
4376 }
4377
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004378 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4379 {
4380 if (rettv->v_type != var2.v_type
4381 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4382 {
4383 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004384 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004385 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004386 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004387 clear_tv(rettv);
4388 clear_tv(&var2);
4389 return FAIL;
4390 }
4391 else
4392 {
4393 /* Compare two Funcrefs for being equal or unequal. */
4394 if (rettv->vval.v_string == NULL
4395 || var2.vval.v_string == NULL)
4396 n1 = FALSE;
4397 else
4398 n1 = STRCMP(rettv->vval.v_string,
4399 var2.vval.v_string) == 0;
4400 if (type == TYPE_NEQUAL)
4401 n1 = !n1;
4402 }
4403 }
4404
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004405#ifdef FEAT_FLOAT
4406 /*
4407 * If one of the two variables is a float, compare as a float.
4408 * When using "=~" or "!~", always compare as string.
4409 */
4410 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4411 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4412 {
4413 float_T f1, f2;
4414
4415 if (rettv->v_type == VAR_FLOAT)
4416 f1 = rettv->vval.v_float;
4417 else
4418 f1 = get_tv_number(rettv);
4419 if (var2.v_type == VAR_FLOAT)
4420 f2 = var2.vval.v_float;
4421 else
4422 f2 = get_tv_number(&var2);
4423 n1 = FALSE;
4424 switch (type)
4425 {
4426 case TYPE_EQUAL: n1 = (f1 == f2); break;
4427 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4428 case TYPE_GREATER: n1 = (f1 > f2); break;
4429 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4430 case TYPE_SMALLER: n1 = (f1 < f2); break;
4431 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4432 case TYPE_UNKNOWN:
4433 case TYPE_MATCH:
4434 case TYPE_NOMATCH: break; /* avoid gcc warning */
4435 }
4436 }
4437#endif
4438
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 /*
4440 * If one of the two variables is a number, compare as a number.
4441 * When using "=~" or "!~", always compare as string.
4442 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004443 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004446 n1 = get_tv_number(rettv);
4447 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 switch (type)
4449 {
4450 case TYPE_EQUAL: n1 = (n1 == n2); break;
4451 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4452 case TYPE_GREATER: n1 = (n1 > n2); break;
4453 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4454 case TYPE_SMALLER: n1 = (n1 < n2); break;
4455 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4456 case TYPE_UNKNOWN:
4457 case TYPE_MATCH:
4458 case TYPE_NOMATCH: break; /* avoid gcc warning */
4459 }
4460 }
4461 else
4462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004463 s1 = get_tv_string_buf(rettv, buf1);
4464 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4466 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4467 else
4468 i = 0;
4469 n1 = FALSE;
4470 switch (type)
4471 {
4472 case TYPE_EQUAL: n1 = (i == 0); break;
4473 case TYPE_NEQUAL: n1 = (i != 0); break;
4474 case TYPE_GREATER: n1 = (i > 0); break;
4475 case TYPE_GEQUAL: n1 = (i >= 0); break;
4476 case TYPE_SMALLER: n1 = (i < 0); break;
4477 case TYPE_SEQUAL: n1 = (i <= 0); break;
4478
4479 case TYPE_MATCH:
4480 case TYPE_NOMATCH:
4481 /* avoid 'l' flag in 'cpoptions' */
4482 save_cpo = p_cpo;
4483 p_cpo = (char_u *)"";
4484 regmatch.regprog = vim_regcomp(s2,
4485 RE_MAGIC + RE_STRING);
4486 regmatch.rm_ic = ic;
4487 if (regmatch.regprog != NULL)
4488 {
4489 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4490 vim_free(regmatch.regprog);
4491 if (type == TYPE_NOMATCH)
4492 n1 = !n1;
4493 }
4494 p_cpo = save_cpo;
4495 break;
4496
4497 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4498 }
4499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004500 clear_tv(rettv);
4501 clear_tv(&var2);
4502 rettv->v_type = VAR_NUMBER;
4503 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505 }
4506
4507 return OK;
4508}
4509
4510/*
4511 * Handle fourth level expression:
4512 * + number addition
4513 * - number subtraction
4514 * . string concatenation
4515 *
4516 * "arg" must point to the first non-white of the expression.
4517 * "arg" is advanced to the next non-white after the recognized expression.
4518 *
4519 * Return OK or FAIL.
4520 */
4521 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004522eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 int evaluate;
4526{
Bram Moolenaar33570922005-01-25 22:26:29 +00004527 typval_T var2;
4528 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 int op;
4530 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004531#ifdef FEAT_FLOAT
4532 float_T f1 = 0, f2 = 0;
4533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 char_u *s1, *s2;
4535 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4536 char_u *p;
4537
4538 /*
4539 * Get the first variable.
4540 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004541 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 return FAIL;
4543
4544 /*
4545 * Repeat computing, until no '+', '-' or '.' is following.
4546 */
4547 for (;;)
4548 {
4549 op = **arg;
4550 if (op != '+' && op != '-' && op != '.')
4551 break;
4552
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004553 if ((op != '+' || rettv->v_type != VAR_LIST)
4554#ifdef FEAT_FLOAT
4555 && (op == '.' || rettv->v_type != VAR_FLOAT)
4556#endif
4557 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004558 {
4559 /* For "list + ...", an illegal use of the first operand as
4560 * a number cannot be determined before evaluating the 2nd
4561 * operand: if this is also a list, all is ok.
4562 * For "something . ...", "something - ..." or "non-list + ...",
4563 * we know that the first operand needs to be a string or number
4564 * without evaluating the 2nd operand. So check before to avoid
4565 * side effects after an error. */
4566 if (evaluate && get_tv_string_chk(rettv) == NULL)
4567 {
4568 clear_tv(rettv);
4569 return FAIL;
4570 }
4571 }
4572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 /*
4574 * Get the second variable.
4575 */
4576 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004577 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004579 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 return FAIL;
4581 }
4582
4583 if (evaluate)
4584 {
4585 /*
4586 * Compute the result.
4587 */
4588 if (op == '.')
4589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004590 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4591 s2 = get_tv_string_buf_chk(&var2, buf2);
4592 if (s2 == NULL) /* type error ? */
4593 {
4594 clear_tv(rettv);
4595 clear_tv(&var2);
4596 return FAIL;
4597 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004598 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 clear_tv(rettv);
4600 rettv->v_type = VAR_STRING;
4601 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004603 else if (op == '+' && rettv->v_type == VAR_LIST
4604 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004605 {
4606 /* concatenate Lists */
4607 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4608 &var3) == FAIL)
4609 {
4610 clear_tv(rettv);
4611 clear_tv(&var2);
4612 return FAIL;
4613 }
4614 clear_tv(rettv);
4615 *rettv = var3;
4616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 else
4618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004619 int error = FALSE;
4620
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004621#ifdef FEAT_FLOAT
4622 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004623 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004624 f1 = rettv->vval.v_float;
4625 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004626 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004627 else
4628#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004629 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630 n1 = get_tv_number_chk(rettv, &error);
4631 if (error)
4632 {
4633 /* This can only happen for "list + non-list". For
4634 * "non-list + ..." or "something - ...", we returned
4635 * before evaluating the 2nd operand. */
4636 clear_tv(rettv);
4637 return FAIL;
4638 }
4639#ifdef FEAT_FLOAT
4640 if (var2.v_type == VAR_FLOAT)
4641 f1 = n1;
4642#endif
4643 }
4644#ifdef FEAT_FLOAT
4645 if (var2.v_type == VAR_FLOAT)
4646 {
4647 f2 = var2.vval.v_float;
4648 n2 = 0;
4649 }
4650 else
4651#endif
4652 {
4653 n2 = get_tv_number_chk(&var2, &error);
4654 if (error)
4655 {
4656 clear_tv(rettv);
4657 clear_tv(&var2);
4658 return FAIL;
4659 }
4660#ifdef FEAT_FLOAT
4661 if (rettv->v_type == VAR_FLOAT)
4662 f2 = n2;
4663#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004666
4667#ifdef FEAT_FLOAT
4668 /* If there is a float on either side the result is a float. */
4669 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4670 {
4671 if (op == '+')
4672 f1 = f1 + f2;
4673 else
4674 f1 = f1 - f2;
4675 rettv->v_type = VAR_FLOAT;
4676 rettv->vval.v_float = f1;
4677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004679#endif
4680 {
4681 if (op == '+')
4682 n1 = n1 + n2;
4683 else
4684 n1 = n1 - n2;
4685 rettv->v_type = VAR_NUMBER;
4686 rettv->vval.v_number = n1;
4687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004689 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 }
4691 }
4692 return OK;
4693}
4694
4695/*
4696 * Handle fifth level expression:
4697 * * number multiplication
4698 * / number division
4699 * % number modulo
4700 *
4701 * "arg" must point to the first non-white of the expression.
4702 * "arg" is advanced to the next non-white after the recognized expression.
4703 *
4704 * Return OK or FAIL.
4705 */
4706 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004707eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004711 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
Bram Moolenaar33570922005-01-25 22:26:29 +00004713 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 int op;
4715 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716#ifdef FEAT_FLOAT
4717 int use_float = FALSE;
4718 float_T f1 = 0, f2;
4719#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721
4722 /*
4723 * Get the first variable.
4724 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004725 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 return FAIL;
4727
4728 /*
4729 * Repeat computing, until no '*', '/' or '%' is following.
4730 */
4731 for (;;)
4732 {
4733 op = **arg;
4734 if (op != '*' && op != '/' && op != '%')
4735 break;
4736
4737 if (evaluate)
4738 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004739#ifdef FEAT_FLOAT
4740 if (rettv->v_type == VAR_FLOAT)
4741 {
4742 f1 = rettv->vval.v_float;
4743 use_float = TRUE;
4744 n1 = 0;
4745 }
4746 else
4747#endif
4748 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004749 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004750 if (error)
4751 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
4753 else
4754 n1 = 0;
4755
4756 /*
4757 * Get the second variable.
4758 */
4759 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004760 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 return FAIL;
4762
4763 if (evaluate)
4764 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004765#ifdef FEAT_FLOAT
4766 if (var2.v_type == VAR_FLOAT)
4767 {
4768 if (!use_float)
4769 {
4770 f1 = n1;
4771 use_float = TRUE;
4772 }
4773 f2 = var2.vval.v_float;
4774 n2 = 0;
4775 }
4776 else
4777#endif
4778 {
4779 n2 = get_tv_number_chk(&var2, &error);
4780 clear_tv(&var2);
4781 if (error)
4782 return FAIL;
4783#ifdef FEAT_FLOAT
4784 if (use_float)
4785 f2 = n2;
4786#endif
4787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788
4789 /*
4790 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793#ifdef FEAT_FLOAT
4794 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796 if (op == '*')
4797 f1 = f1 * f2;
4798 else if (op == '/')
4799 {
4800 /* We rely on the floating point library to handle divide
4801 * by zero to result in "inf" and not a crash. */
4802 f1 = f1 / f2;
4803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004806 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004807 return FAIL;
4808 }
4809 rettv->v_type = VAR_FLOAT;
4810 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 }
4812 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004815 if (op == '*')
4816 n1 = n1 * n2;
4817 else if (op == '/')
4818 {
4819 if (n2 == 0) /* give an error message? */
4820 {
4821 if (n1 == 0)
4822 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4823 else if (n1 < 0)
4824 n1 = -0x7fffffffL;
4825 else
4826 n1 = 0x7fffffffL;
4827 }
4828 else
4829 n1 = n1 / n2;
4830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004832 {
4833 if (n2 == 0) /* give an error message? */
4834 n1 = 0;
4835 else
4836 n1 = n1 % n2;
4837 }
4838 rettv->v_type = VAR_NUMBER;
4839 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842 }
4843
4844 return OK;
4845}
4846
4847/*
4848 * Handle sixth level expression:
4849 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004850 * "string" string constant
4851 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 * &option-name option value
4853 * @r register contents
4854 * identifier variable value
4855 * function() function call
4856 * $VAR environment variable
4857 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004858 * [expr, expr] List
4859 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 *
4861 * Also handle:
4862 * ! in front logical NOT
4863 * - in front unary minus
4864 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004865 * trailing [] subscript in String or List
4866 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 *
4868 * "arg" must point to the first non-white of the expression.
4869 * "arg" is advanced to the next non-white after the recognized expression.
4870 *
4871 * Return OK or FAIL.
4872 */
4873 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004874eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004878 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 long n;
4881 int len;
4882 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 char_u *start_leader, *end_leader;
4884 int ret = OK;
4885 char_u *alias;
4886
4887 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004888 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004889 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892
4893 /*
4894 * Skip '!' and '-' characters. They are handled later.
4895 */
4896 start_leader = *arg;
4897 while (**arg == '!' || **arg == '-' || **arg == '+')
4898 *arg = skipwhite(*arg + 1);
4899 end_leader = *arg;
4900
4901 switch (**arg)
4902 {
4903 /*
4904 * Number constant.
4905 */
4906 case '0':
4907 case '1':
4908 case '2':
4909 case '3':
4910 case '4':
4911 case '5':
4912 case '6':
4913 case '7':
4914 case '8':
4915 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916 {
4917#ifdef FEAT_FLOAT
4918 char_u *p = skipdigits(*arg + 1);
4919 int get_float = FALSE;
4920
4921 /* We accept a float when the format matches
4922 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004923 * strict to avoid backwards compatibility problems.
4924 * Don't look for a float after the "." operator, so that
4925 * ":let vers = 1.2.3" doesn't fail. */
4926 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928 get_float = TRUE;
4929 p = skipdigits(p + 2);
4930 if (*p == 'e' || *p == 'E')
4931 {
4932 ++p;
4933 if (*p == '-' || *p == '+')
4934 ++p;
4935 if (!vim_isdigit(*p))
4936 get_float = FALSE;
4937 else
4938 p = skipdigits(p + 1);
4939 }
4940 if (ASCII_ISALPHA(*p) || *p == '.')
4941 get_float = FALSE;
4942 }
4943 if (get_float)
4944 {
4945 float_T f;
4946
4947 *arg += string2float(*arg, &f);
4948 if (evaluate)
4949 {
4950 rettv->v_type = VAR_FLOAT;
4951 rettv->vval.v_float = f;
4952 }
4953 }
4954 else
4955#endif
4956 {
4957 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4958 *arg += len;
4959 if (evaluate)
4960 {
4961 rettv->v_type = VAR_NUMBER;
4962 rettv->vval.v_number = n;
4963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967
4968 /*
4969 * String constant: "string".
4970 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004971 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 break;
4973
4974 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004977 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004978 break;
4979
4980 /*
4981 * List: [expr, expr]
4982 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 break;
4985
4986 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 * Dictionary: {key: val, key: val}
4988 */
4989 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4990 break;
4991
4992 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004993 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004995 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 break;
4997
4998 /*
4999 * Environment variable: $VAR.
5000 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005001 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 break;
5003
5004 /*
5005 * Register contents: @r.
5006 */
5007 case '@': ++*arg;
5008 if (evaluate)
5009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005010 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005011 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013 if (**arg != NUL)
5014 ++*arg;
5015 break;
5016
5017 /*
5018 * nested expression: (expression).
5019 */
5020 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005021 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 if (**arg == ')')
5023 ++*arg;
5024 else if (ret == OK)
5025 {
5026 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005027 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 ret = FAIL;
5029 }
5030 break;
5031
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 break;
5034 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035
5036 if (ret == NOTDONE)
5037 {
5038 /*
5039 * Must be a variable or function name.
5040 * Can also be a curly-braces kind of name: {expr}.
5041 */
5042 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005043 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 if (alias != NULL)
5045 s = alias;
5046
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005047 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 ret = FAIL;
5049 else
5050 {
5051 if (**arg == '(') /* recursive! */
5052 {
5053 /* If "s" is the name of a variable of type VAR_FUNC
5054 * use its contents. */
5055 s = deref_func_name(s, &len);
5056
5057 /* Invoke the function. */
5058 ret = get_func_tv(s, len, rettv, arg,
5059 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005060 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 /* Stop the expression evaluation when immediately
5062 * aborting on error, or when an interrupt occurred or
5063 * an exception was thrown but not caught. */
5064 if (aborting())
5065 {
5066 if (ret == OK)
5067 clear_tv(rettv);
5068 ret = FAIL;
5069 }
5070 }
5071 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005072 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005073 else
5074 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 }
5076
5077 if (alias != NULL)
5078 vim_free(alias);
5079 }
5080
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 *arg = skipwhite(*arg);
5082
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005083 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5084 * expr(expr). */
5085 if (ret == OK)
5086 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
5088 /*
5089 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5090 */
5091 if (ret == OK && evaluate && end_leader > start_leader)
5092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005093 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005094 int val = 0;
5095#ifdef FEAT_FLOAT
5096 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005097
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005098 if (rettv->v_type == VAR_FLOAT)
5099 f = rettv->vval.v_float;
5100 else
5101#endif
5102 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005103 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005105 clear_tv(rettv);
5106 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005108 else
5109 {
5110 while (end_leader > start_leader)
5111 {
5112 --end_leader;
5113 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005114 {
5115#ifdef FEAT_FLOAT
5116 if (rettv->v_type == VAR_FLOAT)
5117 f = !f;
5118 else
5119#endif
5120 val = !val;
5121 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005122 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005123 {
5124#ifdef FEAT_FLOAT
5125 if (rettv->v_type == VAR_FLOAT)
5126 f = -f;
5127 else
5128#endif
5129 val = -val;
5130 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005131 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005132#ifdef FEAT_FLOAT
5133 if (rettv->v_type == VAR_FLOAT)
5134 {
5135 clear_tv(rettv);
5136 rettv->vval.v_float = f;
5137 }
5138 else
5139#endif
5140 {
5141 clear_tv(rettv);
5142 rettv->v_type = VAR_NUMBER;
5143 rettv->vval.v_number = val;
5144 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147
5148 return ret;
5149}
5150
5151/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005152 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5153 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5155 */
5156 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005157eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005159 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005160 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005161 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162{
5163 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005164 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 long len = -1;
5167 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005168 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005170
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005171 if (rettv->v_type == VAR_FUNC
5172#ifdef FEAT_FLOAT
5173 || rettv->v_type == VAR_FLOAT
5174#endif
5175 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005177 if (verbose)
5178 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 return FAIL;
5180 }
5181
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 /*
5185 * dict.name
5186 */
5187 key = *arg + 1;
5188 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5189 ;
5190 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193 }
5194 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 /*
5197 * something[idx]
5198 *
5199 * Get the (first) variable from inside the [].
5200 */
5201 *arg = skipwhite(*arg + 1);
5202 if (**arg == ':')
5203 empty1 = TRUE;
5204 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5205 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5207 {
5208 /* not a number or string */
5209 clear_tv(&var1);
5210 return FAIL;
5211 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212
5213 /*
5214 * Get the second variable from inside the [:].
5215 */
5216 if (**arg == ':')
5217 {
5218 range = TRUE;
5219 *arg = skipwhite(*arg + 1);
5220 if (**arg == ']')
5221 empty2 = TRUE;
5222 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005224 if (!empty1)
5225 clear_tv(&var1);
5226 return FAIL;
5227 }
5228 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5229 {
5230 /* not a number or string */
5231 if (!empty1)
5232 clear_tv(&var1);
5233 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 return FAIL;
5235 }
5236 }
5237
5238 /* Check for the ']'. */
5239 if (**arg != ']')
5240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005241 if (verbose)
5242 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243 clear_tv(&var1);
5244 if (range)
5245 clear_tv(&var2);
5246 return FAIL;
5247 }
5248 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249 }
5250
5251 if (evaluate)
5252 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 n1 = 0;
5254 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005256 n1 = get_tv_number(&var1);
5257 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 }
5259 if (range)
5260 {
5261 if (empty2)
5262 n2 = -1;
5263 else
5264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005265 n2 = get_tv_number(&var2);
5266 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 }
5268 }
5269
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005270 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 {
5272 case VAR_NUMBER:
5273 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 len = (long)STRLEN(s);
5276 if (range)
5277 {
5278 /* The resulting variable is a substring. If the indexes
5279 * are out of range the result is empty. */
5280 if (n1 < 0)
5281 {
5282 n1 = len + n1;
5283 if (n1 < 0)
5284 n1 = 0;
5285 }
5286 if (n2 < 0)
5287 n2 = len + n2;
5288 else if (n2 >= len)
5289 n2 = len;
5290 if (n1 >= len || n2 < 0 || n1 > n2)
5291 s = NULL;
5292 else
5293 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5294 }
5295 else
5296 {
5297 /* The resulting variable is a string of a single
5298 * character. If the index is too big or negative the
5299 * result is empty. */
5300 if (n1 >= len || n1 < 0)
5301 s = NULL;
5302 else
5303 s = vim_strnsave(s + n1, 1);
5304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305 clear_tv(rettv);
5306 rettv->v_type = VAR_STRING;
5307 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 break;
5309
5310 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005311 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 if (n1 < 0)
5313 n1 = len + n1;
5314 if (!empty1 && (n1 < 0 || n1 >= len))
5315 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005316 /* For a range we allow invalid values and return an empty
5317 * list. A list index out of range is an error. */
5318 if (!range)
5319 {
5320 if (verbose)
5321 EMSGN(_(e_listidx), n1);
5322 return FAIL;
5323 }
5324 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 }
5326 if (range)
5327 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005328 list_T *l;
5329 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330
5331 if (n2 < 0)
5332 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005333 else if (n2 >= len)
5334 n2 = len - 1;
5335 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005336 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 l = list_alloc();
5338 if (l == NULL)
5339 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005340 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 n1 <= n2; ++n1)
5342 {
5343 if (list_append_tv(l, &item->li_tv) == FAIL)
5344 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005345 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 return FAIL;
5347 }
5348 item = item->li_next;
5349 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005350 clear_tv(rettv);
5351 rettv->v_type = VAR_LIST;
5352 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005353 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 }
5355 else
5356 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005357 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005358 clear_tv(rettv);
5359 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 }
5361 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005362
5363 case VAR_DICT:
5364 if (range)
5365 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005366 if (verbose)
5367 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005368 if (len == -1)
5369 clear_tv(&var1);
5370 return FAIL;
5371 }
5372 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005373 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374
5375 if (len == -1)
5376 {
5377 key = get_tv_string(&var1);
5378 if (*key == NUL)
5379 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005380 if (verbose)
5381 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 clear_tv(&var1);
5383 return FAIL;
5384 }
5385 }
5386
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005387 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005388
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005389 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005390 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 if (len == -1)
5392 clear_tv(&var1);
5393 if (item == NULL)
5394 return FAIL;
5395
5396 copy_tv(&item->di_tv, &var1);
5397 clear_tv(rettv);
5398 *rettv = var1;
5399 }
5400 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 }
5402 }
5403
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 return OK;
5405}
5406
5407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 * Get an option value.
5409 * "arg" points to the '&' or '+' before the option name.
5410 * "arg" is advanced to character after the option name.
5411 * Return OK or FAIL.
5412 */
5413 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005414get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005416 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 int evaluate;
5418{
5419 char_u *option_end;
5420 long numval;
5421 char_u *stringval;
5422 int opt_type;
5423 int c;
5424 int working = (**arg == '+'); /* has("+option") */
5425 int ret = OK;
5426 int opt_flags;
5427
5428 /*
5429 * Isolate the option name and find its value.
5430 */
5431 option_end = find_option_end(arg, &opt_flags);
5432 if (option_end == NULL)
5433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005434 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 EMSG2(_("E112: Option name missing: %s"), *arg);
5436 return FAIL;
5437 }
5438
5439 if (!evaluate)
5440 {
5441 *arg = option_end;
5442 return OK;
5443 }
5444
5445 c = *option_end;
5446 *option_end = NUL;
5447 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450 if (opt_type == -3) /* invalid name */
5451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005452 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 EMSG2(_("E113: Unknown option: %s"), *arg);
5454 ret = FAIL;
5455 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
5458 if (opt_type == -2) /* hidden string option */
5459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 rettv->v_type = VAR_STRING;
5461 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
5463 else if (opt_type == -1) /* hidden number option */
5464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 rettv->v_type = VAR_NUMBER;
5466 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468 else if (opt_type == 1) /* number option */
5469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 rettv->v_type = VAR_NUMBER;
5471 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
5473 else /* string option */
5474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 rettv->v_type = VAR_STRING;
5476 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
5478 }
5479 else if (working && (opt_type == -2 || opt_type == -1))
5480 ret = FAIL;
5481
5482 *option_end = c; /* put back for error messages */
5483 *arg = option_end;
5484
5485 return ret;
5486}
5487
5488/*
5489 * Allocate a variable for a string constant.
5490 * Return OK or FAIL.
5491 */
5492 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 int evaluate;
5497{
5498 char_u *p;
5499 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 int extra = 0;
5501
5502 /*
5503 * Find the end of the string, skipping backslashed characters.
5504 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005505 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
5507 if (*p == '\\' && p[1] != NUL)
5508 {
5509 ++p;
5510 /* A "\<x>" form occupies at least 4 characters, and produces up
5511 * to 6 characters: reserve space for 2 extra */
5512 if (*p == '<')
5513 extra += 2;
5514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 }
5516
5517 if (*p != '"')
5518 {
5519 EMSG2(_("E114: Missing quote: %s"), *arg);
5520 return FAIL;
5521 }
5522
5523 /* If only parsing, set *arg and return here */
5524 if (!evaluate)
5525 {
5526 *arg = p + 1;
5527 return OK;
5528 }
5529
5530 /*
5531 * Copy the string into allocated memory, handling backslashed
5532 * characters.
5533 */
5534 name = alloc((unsigned)(p - *arg + extra));
5535 if (name == NULL)
5536 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005537 rettv->v_type = VAR_STRING;
5538 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
Bram Moolenaar8c711452005-01-14 21:53:12 +00005540 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
5542 if (*p == '\\')
5543 {
5544 switch (*++p)
5545 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005546 case 'b': *name++ = BS; ++p; break;
5547 case 'e': *name++ = ESC; ++p; break;
5548 case 'f': *name++ = FF; ++p; break;
5549 case 'n': *name++ = NL; ++p; break;
5550 case 'r': *name++ = CAR; ++p; break;
5551 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
5553 case 'X': /* hex: "\x1", "\x12" */
5554 case 'x':
5555 case 'u': /* Unicode: "\u0023" */
5556 case 'U':
5557 if (vim_isxdigit(p[1]))
5558 {
5559 int n, nr;
5560 int c = toupper(*p);
5561
5562 if (c == 'X')
5563 n = 2;
5564 else
5565 n = 4;
5566 nr = 0;
5567 while (--n >= 0 && vim_isxdigit(p[1]))
5568 {
5569 ++p;
5570 nr = (nr << 4) + hex2nr(*p);
5571 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573#ifdef FEAT_MBYTE
5574 /* For "\u" store the number according to
5575 * 'encoding'. */
5576 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 else
5579#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 break;
5583
5584 /* octal: "\1", "\12", "\123" */
5585 case '0':
5586 case '1':
5587 case '2':
5588 case '3':
5589 case '4':
5590 case '5':
5591 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 case '7': *name = *p++ - '0';
5593 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 *name = (*name << 3) + *p++ - '0';
5596 if (*p >= '0' && *p <= '7')
5597 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 break;
5601
5602 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 if (extra != 0)
5605 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 break;
5608 }
5609 /* FALLTHROUGH */
5610
Bram Moolenaar8c711452005-01-14 21:53:12 +00005611 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 break;
5613 }
5614 }
5615 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 *arg = p + 1;
5621
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 return OK;
5623}
5624
5625/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 * Return OK or FAIL.
5628 */
5629 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 int evaluate;
5634{
5635 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005636 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005637 int reduce = 0;
5638
5639 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 */
5642 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5643 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005647 break;
5648 ++reduce;
5649 ++p;
5650 }
5651 }
5652
Bram Moolenaar8c711452005-01-14 21:53:12 +00005653 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005654 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005655 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005656 return FAIL;
5657 }
5658
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005660 if (!evaluate)
5661 {
5662 *arg = p + 1;
5663 return OK;
5664 }
5665
5666 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005668 */
5669 str = alloc((unsigned)((p - *arg) - reduce));
5670 if (str == NULL)
5671 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 rettv->v_type = VAR_STRING;
5673 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005674
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005680 break;
5681 ++p;
5682 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005686 *arg = p + 1;
5687
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005688 return OK;
5689}
5690
5691/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 * Allocate a variable for a List and fill it from "*arg".
5693 * Return OK or FAIL.
5694 */
5695 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005696get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005697 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005698 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005699 int evaluate;
5700{
Bram Moolenaar33570922005-01-25 22:26:29 +00005701 list_T *l = NULL;
5702 typval_T tv;
5703 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704
5705 if (evaluate)
5706 {
5707 l = list_alloc();
5708 if (l == NULL)
5709 return FAIL;
5710 }
5711
5712 *arg = skipwhite(*arg + 1);
5713 while (**arg != ']' && **arg != NUL)
5714 {
5715 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5716 goto failret;
5717 if (evaluate)
5718 {
5719 item = listitem_alloc();
5720 if (item != NULL)
5721 {
5722 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005723 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724 list_append(l, item);
5725 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005726 else
5727 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728 }
5729
5730 if (**arg == ']')
5731 break;
5732 if (**arg != ',')
5733 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005735 goto failret;
5736 }
5737 *arg = skipwhite(*arg + 1);
5738 }
5739
5740 if (**arg != ']')
5741 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743failret:
5744 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005745 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746 return FAIL;
5747 }
5748
5749 *arg = skipwhite(*arg + 1);
5750 if (evaluate)
5751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005752 rettv->v_type = VAR_LIST;
5753 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754 ++l->lv_refcount;
5755 }
5756
5757 return OK;
5758}
5759
5760/*
5761 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005762 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005764 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005765list_alloc()
5766{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005767 list_T *l;
5768
5769 l = (list_T *)alloc_clear(sizeof(list_T));
5770 if (l != NULL)
5771 {
5772 /* Prepend the list to the list of lists for garbage collection. */
5773 if (first_list != NULL)
5774 first_list->lv_used_prev = l;
5775 l->lv_used_prev = NULL;
5776 l->lv_used_next = first_list;
5777 first_list = l;
5778 }
5779 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780}
5781
5782/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005783 * Allocate an empty list for a return value.
5784 * Returns OK or FAIL.
5785 */
5786 static int
5787rettv_list_alloc(rettv)
5788 typval_T *rettv;
5789{
5790 list_T *l = list_alloc();
5791
5792 if (l == NULL)
5793 return FAIL;
5794
5795 rettv->vval.v_list = l;
5796 rettv->v_type = VAR_LIST;
5797 ++l->lv_refcount;
5798 return OK;
5799}
5800
5801/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802 * Unreference a list: decrement the reference count and free it when it
5803 * becomes zero.
5804 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005805 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005807 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005809 if (l != NULL && --l->lv_refcount <= 0)
5810 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811}
5812
5813/*
5814 * Free a list, including all items it points to.
5815 * Ignores the reference count.
5816 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005817 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005818list_free(l, recurse)
5819 list_T *l;
5820 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821{
Bram Moolenaar33570922005-01-25 22:26:29 +00005822 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005824 /* Remove the list from the list of lists for garbage collection. */
5825 if (l->lv_used_prev == NULL)
5826 first_list = l->lv_used_next;
5827 else
5828 l->lv_used_prev->lv_used_next = l->lv_used_next;
5829 if (l->lv_used_next != NULL)
5830 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5831
Bram Moolenaard9fba312005-06-26 22:34:35 +00005832 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005834 /* Remove the item before deleting it. */
5835 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005836 if (recurse || (item->li_tv.v_type != VAR_LIST
5837 && item->li_tv.v_type != VAR_DICT))
5838 clear_tv(&item->li_tv);
5839 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840 }
5841 vim_free(l);
5842}
5843
5844/*
5845 * Allocate a list item.
5846 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005847 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848listitem_alloc()
5849{
Bram Moolenaar33570922005-01-25 22:26:29 +00005850 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851}
5852
5853/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005854 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855 */
5856 static void
5857listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005858 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005860 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 vim_free(item);
5862}
5863
5864/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005865 * Remove a list item from a List and free it. Also clears the value.
5866 */
5867 static void
5868listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005869 list_T *l;
5870 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005871{
5872 list_remove(l, item, item);
5873 listitem_free(item);
5874}
5875
5876/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877 * Get the number of items in a list.
5878 */
5879 static long
5880list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 if (l == NULL)
5884 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005885 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886}
5887
5888/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005889 * Return TRUE when two lists have exactly the same values.
5890 */
5891 static int
5892list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 list_T *l1;
5894 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005895 int ic; /* ignore case for strings */
5896{
Bram Moolenaar33570922005-01-25 22:26:29 +00005897 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005898
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005899 if (l1 == NULL || l2 == NULL)
5900 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005901 if (l1 == l2)
5902 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005903 if (list_len(l1) != list_len(l2))
5904 return FALSE;
5905
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005906 for (item1 = l1->lv_first, item2 = l2->lv_first;
5907 item1 != NULL && item2 != NULL;
5908 item1 = item1->li_next, item2 = item2->li_next)
5909 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5910 return FALSE;
5911 return item1 == NULL && item2 == NULL;
5912}
5913
Bram Moolenaar3fac56e2010-02-24 15:48:04 +01005914#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
5915 || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005916/*
5917 * Return the dictitem that an entry in a hashtable points to.
5918 */
5919 dictitem_T *
5920dict_lookup(hi)
5921 hashitem_T *hi;
5922{
5923 return HI2DI(hi);
5924}
5925#endif
5926
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005927/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005928 * Return TRUE when two dictionaries have exactly the same key/values.
5929 */
5930 static int
5931dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 dict_T *d1;
5933 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005934 int ic; /* ignore case for strings */
5935{
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 hashitem_T *hi;
5937 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005938 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005939
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005940 if (d1 == NULL || d2 == NULL)
5941 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005942 if (d1 == d2)
5943 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005944 if (dict_len(d1) != dict_len(d2))
5945 return FALSE;
5946
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005947 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005949 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005950 if (!HASHITEM_EMPTY(hi))
5951 {
5952 item2 = dict_find(d2, hi->hi_key, -1);
5953 if (item2 == NULL)
5954 return FALSE;
5955 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5956 return FALSE;
5957 --todo;
5958 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005959 }
5960 return TRUE;
5961}
5962
5963/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005964 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005965 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005966 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005967 */
5968 static int
5969tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005970 typval_T *tv1;
5971 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005972 int ic; /* ignore case */
5973{
5974 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005975 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005976 static int recursive = 0; /* cach recursive loops */
5977 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005978
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005979 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005980 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005981 /* Catch lists and dicts that have an endless loop by limiting
5982 * recursiveness to 1000. We guess they are equal then. */
5983 if (recursive >= 1000)
5984 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005985
5986 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005987 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005988 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005989 ++recursive;
5990 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5991 --recursive;
5992 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005993
5994 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005995 ++recursive;
5996 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5997 --recursive;
5998 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005999
6000 case VAR_FUNC:
6001 return (tv1->vval.v_string != NULL
6002 && tv2->vval.v_string != NULL
6003 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6004
6005 case VAR_NUMBER:
6006 return tv1->vval.v_number == tv2->vval.v_number;
6007
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006008#ifdef FEAT_FLOAT
6009 case VAR_FLOAT:
6010 return tv1->vval.v_float == tv2->vval.v_float;
6011#endif
6012
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006013 case VAR_STRING:
6014 s1 = get_tv_string_buf(tv1, buf1);
6015 s2 = get_tv_string_buf(tv2, buf2);
6016 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006017 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006018
6019 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006020 return TRUE;
6021}
6022
6023/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 * Locate item with index "n" in list "l" and return it.
6025 * A negative index is counted from the end; -1 is the last item.
6026 * Returns NULL when "n" is out of range.
6027 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006030 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031 long n;
6032{
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 long idx;
6035
6036 if (l == NULL)
6037 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006038
6039 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006041 n = l->lv_len + n;
6042
6043 /* Check for index out of range. */
6044 if (n < 0 || n >= l->lv_len)
6045 return NULL;
6046
6047 /* When there is a cached index may start search from there. */
6048 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006050 if (n < l->lv_idx / 2)
6051 {
6052 /* closest to the start of the list */
6053 item = l->lv_first;
6054 idx = 0;
6055 }
6056 else if (n > (l->lv_idx + l->lv_len) / 2)
6057 {
6058 /* closest to the end of the list */
6059 item = l->lv_last;
6060 idx = l->lv_len - 1;
6061 }
6062 else
6063 {
6064 /* closest to the cached index */
6065 item = l->lv_idx_item;
6066 idx = l->lv_idx;
6067 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 }
6069 else
6070 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006071 if (n < l->lv_len / 2)
6072 {
6073 /* closest to the start of the list */
6074 item = l->lv_first;
6075 idx = 0;
6076 }
6077 else
6078 {
6079 /* closest to the end of the list */
6080 item = l->lv_last;
6081 idx = l->lv_len - 1;
6082 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006084
6085 while (n > idx)
6086 {
6087 /* search forward */
6088 item = item->li_next;
6089 ++idx;
6090 }
6091 while (n < idx)
6092 {
6093 /* search backward */
6094 item = item->li_prev;
6095 --idx;
6096 }
6097
6098 /* cache the used index */
6099 l->lv_idx = idx;
6100 l->lv_idx_item = item;
6101
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102 return item;
6103}
6104
6105/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006106 * Get list item "l[idx]" as a number.
6107 */
6108 static long
6109list_find_nr(l, idx, errorp)
6110 list_T *l;
6111 long idx;
6112 int *errorp; /* set to TRUE when something wrong */
6113{
6114 listitem_T *li;
6115
6116 li = list_find(l, idx);
6117 if (li == NULL)
6118 {
6119 if (errorp != NULL)
6120 *errorp = TRUE;
6121 return -1L;
6122 }
6123 return get_tv_number_chk(&li->li_tv, errorp);
6124}
6125
6126/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006127 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6128 */
6129 char_u *
6130list_find_str(l, idx)
6131 list_T *l;
6132 long idx;
6133{
6134 listitem_T *li;
6135
6136 li = list_find(l, idx - 1);
6137 if (li == NULL)
6138 {
6139 EMSGN(_(e_listidx), idx);
6140 return NULL;
6141 }
6142 return get_tv_string(&li->li_tv);
6143}
6144
6145/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006146 * Locate "item" list "l" and return its index.
6147 * Returns -1 when "item" is not in the list.
6148 */
6149 static long
6150list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006151 list_T *l;
6152 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006153{
6154 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006156
6157 if (l == NULL)
6158 return -1;
6159 idx = 0;
6160 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6161 ++idx;
6162 if (li == NULL)
6163 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006164 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006165}
6166
6167/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 * Append item "item" to the end of list "l".
6169 */
6170 static void
6171list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 list_T *l;
6173 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174{
6175 if (l->lv_last == NULL)
6176 {
6177 /* empty list */
6178 l->lv_first = item;
6179 l->lv_last = item;
6180 item->li_prev = NULL;
6181 }
6182 else
6183 {
6184 l->lv_last->li_next = item;
6185 item->li_prev = l->lv_last;
6186 l->lv_last = item;
6187 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006188 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189 item->li_next = NULL;
6190}
6191
6192/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006194 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006196 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006198 list_T *l;
6199 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006201 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202
Bram Moolenaar05159a02005-02-26 23:04:13 +00006203 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006204 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006205 copy_tv(tv, &li->li_tv);
6206 list_append(l, li);
6207 return OK;
6208}
6209
6210/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006211 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006212 * Return FAIL when out of memory.
6213 */
6214 int
6215list_append_dict(list, dict)
6216 list_T *list;
6217 dict_T *dict;
6218{
6219 listitem_T *li = listitem_alloc();
6220
6221 if (li == NULL)
6222 return FAIL;
6223 li->li_tv.v_type = VAR_DICT;
6224 li->li_tv.v_lock = 0;
6225 li->li_tv.vval.v_dict = dict;
6226 list_append(list, li);
6227 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228 return OK;
6229}
6230
6231/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006232 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006233 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006234 * Returns FAIL when out of memory.
6235 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006236 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006237list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006238 list_T *l;
6239 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006240 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006241{
6242 listitem_T *li = listitem_alloc();
6243
6244 if (li == NULL)
6245 return FAIL;
6246 list_append(l, li);
6247 li->li_tv.v_type = VAR_STRING;
6248 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006249 if (str == NULL)
6250 li->li_tv.vval.v_string = NULL;
6251 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006252 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006253 return FAIL;
6254 return OK;
6255}
6256
6257/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006258 * Append "n" to list "l".
6259 * Returns FAIL when out of memory.
6260 */
6261 static int
6262list_append_number(l, n)
6263 list_T *l;
6264 varnumber_T n;
6265{
6266 listitem_T *li;
6267
6268 li = listitem_alloc();
6269 if (li == NULL)
6270 return FAIL;
6271 li->li_tv.v_type = VAR_NUMBER;
6272 li->li_tv.v_lock = 0;
6273 li->li_tv.vval.v_number = n;
6274 list_append(l, li);
6275 return OK;
6276}
6277
6278/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006280 * If "item" is NULL append at the end.
6281 * Return FAIL when out of memory.
6282 */
6283 static int
6284list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 list_T *l;
6286 typval_T *tv;
6287 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006288{
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006290
6291 if (ni == NULL)
6292 return FAIL;
6293 copy_tv(tv, &ni->li_tv);
6294 if (item == NULL)
6295 /* Append new item at end of list. */
6296 list_append(l, ni);
6297 else
6298 {
6299 /* Insert new item before existing item. */
6300 ni->li_prev = item->li_prev;
6301 ni->li_next = item;
6302 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006303 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006304 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006305 ++l->lv_idx;
6306 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006307 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006308 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310 l->lv_idx_item = NULL;
6311 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006312 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006314 }
6315 return OK;
6316}
6317
6318/*
6319 * Extend "l1" with "l2".
6320 * If "bef" is NULL append at the end, otherwise insert before this item.
6321 * Returns FAIL when out of memory.
6322 */
6323 static int
6324list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 list_T *l1;
6326 list_T *l2;
6327 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328{
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006330 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006331
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006332 /* We also quit the loop when we have inserted the original item count of
6333 * the list, avoid a hang when we extend a list with itself. */
6334 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006335 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6336 return FAIL;
6337 return OK;
6338}
6339
6340/*
6341 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6342 * Return FAIL when out of memory.
6343 */
6344 static int
6345list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 list_T *l1;
6347 list_T *l2;
6348 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006349{
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006352 if (l1 == NULL || l2 == NULL)
6353 return FAIL;
6354
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006355 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006356 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006357 if (l == NULL)
6358 return FAIL;
6359 tv->v_type = VAR_LIST;
6360 tv->vval.v_list = l;
6361
6362 /* append all items from the second list */
6363 return list_extend(l, l2, NULL);
6364}
6365
6366/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006367 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006369 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006370 * Returns NULL when out of memory.
6371 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006373list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006376 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377{
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 list_T *copy;
6379 listitem_T *item;
6380 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381
6382 if (orig == NULL)
6383 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384
6385 copy = list_alloc();
6386 if (copy != NULL)
6387 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006388 if (copyID != 0)
6389 {
6390 /* Do this before adding the items, because one of the items may
6391 * refer back to this list. */
6392 orig->lv_copyID = copyID;
6393 orig->lv_copylist = copy;
6394 }
6395 for (item = orig->lv_first; item != NULL && !got_int;
6396 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 {
6398 ni = listitem_alloc();
6399 if (ni == NULL)
6400 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006401 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006402 {
6403 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6404 {
6405 vim_free(ni);
6406 break;
6407 }
6408 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006410 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 list_append(copy, ni);
6412 }
6413 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006414 if (item != NULL)
6415 {
6416 list_unref(copy);
6417 copy = NULL;
6418 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419 }
6420
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421 return copy;
6422}
6423
6424/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006426 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006429list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 list_T *l;
6431 listitem_T *item;
6432 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433{
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 /* notify watchers */
6437 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006439 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440 list_fix_watch(l, ip);
6441 if (ip == item2)
6442 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006444
6445 if (item2->li_next == NULL)
6446 l->lv_last = item->li_prev;
6447 else
6448 item2->li_next->li_prev = item->li_prev;
6449 if (item->li_prev == NULL)
6450 l->lv_first = item2->li_next;
6451 else
6452 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006453 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006454}
6455
6456/*
6457 * Return an allocated string with the string representation of a list.
6458 * May return NULL.
6459 */
6460 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006461list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006462 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006463 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006464{
6465 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006466
6467 if (tv->vval.v_list == NULL)
6468 return NULL;
6469 ga_init2(&ga, (int)sizeof(char), 80);
6470 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006471 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006472 {
6473 vim_free(ga.ga_data);
6474 return NULL;
6475 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006476 ga_append(&ga, ']');
6477 ga_append(&ga, NUL);
6478 return (char_u *)ga.ga_data;
6479}
6480
6481/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006482 * Join list "l" into a string in "*gap", using separator "sep".
6483 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006485 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006487list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006488 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006490 char_u *sep;
6491 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006492 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006493{
6494 int first = TRUE;
6495 char_u *tofree;
6496 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006498 char_u *s;
6499
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006501 {
6502 if (first)
6503 first = FALSE;
6504 else
6505 ga_concat(gap, sep);
6506
6507 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006508 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006509 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006510 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006511 if (s != NULL)
6512 ga_concat(gap, s);
6513 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006514 if (s == NULL)
6515 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006516 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006517 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006518 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006519}
6520
6521/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006522 * Garbage collection for lists and dictionaries.
6523 *
6524 * We use reference counts to be able to free most items right away when they
6525 * are no longer used. But for composite items it's possible that it becomes
6526 * unused while the reference count is > 0: When there is a recursive
6527 * reference. Example:
6528 * :let l = [1, 2, 3]
6529 * :let d = {9: l}
6530 * :let l[1] = d
6531 *
6532 * Since this is quite unusual we handle this with garbage collection: every
6533 * once in a while find out which lists and dicts are not referenced from any
6534 * variable.
6535 *
6536 * Here is a good reference text about garbage collection (refers to Python
6537 * but it applies to all reference-counting mechanisms):
6538 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006539 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006540
6541/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006542 * Do garbage collection for lists and dicts.
6543 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006544 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006545 int
6546garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006547{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006548 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006549 buf_T *buf;
6550 win_T *wp;
6551 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006552 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006553 int did_free;
6554 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006555#ifdef FEAT_WINDOWS
6556 tabpage_T *tp;
6557#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006558
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006559 /* Only do this once. */
6560 want_garbage_collect = FALSE;
6561 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006562 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006563
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006564 /* We advance by two because we add one for items referenced through
6565 * previous_funccal. */
6566 current_copyID += COPYID_INC;
6567 copyID = current_copyID;
6568
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006569 /*
6570 * 1. Go through all accessible variables and mark all lists and dicts
6571 * with copyID.
6572 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006573
6574 /* Don't free variables in the previous_funccal list unless they are only
6575 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006576 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006577 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6578 {
6579 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6580 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6581 }
6582
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006583 /* script-local variables */
6584 for (i = 1; i <= ga_scripts.ga_len; ++i)
6585 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6586
6587 /* buffer-local variables */
6588 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6589 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6590
6591 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006592 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006593 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6594
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006595#ifdef FEAT_WINDOWS
6596 /* tabpage-local variables */
6597 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6598 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6599#endif
6600
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006601 /* global variables */
6602 set_ref_in_ht(&globvarht, copyID);
6603
6604 /* function-local variables */
6605 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6606 {
6607 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6608 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6609 }
6610
Bram Moolenaard812df62008-11-09 12:46:09 +00006611 /* v: vars */
6612 set_ref_in_ht(&vimvarht, copyID);
6613
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006614 /*
6615 * 2. Free lists and dictionaries that are not referenced.
6616 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006617 did_free = free_unref_items(copyID);
6618
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006619 /*
6620 * 3. Check if any funccal can be freed now.
6621 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006622 for (pfc = &previous_funccal; *pfc != NULL; )
6623 {
6624 if (can_free_funccal(*pfc, copyID))
6625 {
6626 fc = *pfc;
6627 *pfc = fc->caller;
6628 free_funccal(fc, TRUE);
6629 did_free = TRUE;
6630 did_free_funccal = TRUE;
6631 }
6632 else
6633 pfc = &(*pfc)->caller;
6634 }
6635 if (did_free_funccal)
6636 /* When a funccal was freed some more items might be garbage
6637 * collected, so run again. */
6638 (void)garbage_collect();
6639
6640 return did_free;
6641}
6642
6643/*
6644 * Free lists and dictionaries that are no longer referenced.
6645 */
6646 static int
6647free_unref_items(copyID)
6648 int copyID;
6649{
6650 dict_T *dd;
6651 list_T *ll;
6652 int did_free = FALSE;
6653
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006654 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006655 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006656 */
6657 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006658 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006659 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006660 /* Free the Dictionary and ordinary items it contains, but don't
6661 * recurse into Lists and Dictionaries, they will be in the list
6662 * of dicts or list of lists. */
6663 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006664 did_free = TRUE;
6665
6666 /* restart, next dict may also have been freed */
6667 dd = first_dict;
6668 }
6669 else
6670 dd = dd->dv_used_next;
6671
6672 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006673 * Go through the list of lists and free items without the copyID.
6674 * But don't free a list that has a watcher (used in a for loop), these
6675 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006676 */
6677 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006678 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6679 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006680 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006681 /* Free the List and ordinary items it contains, but don't recurse
6682 * into Lists and Dictionaries, they will be in the list of dicts
6683 * or list of lists. */
6684 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006685 did_free = TRUE;
6686
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006687 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006688 ll = first_list;
6689 }
6690 else
6691 ll = ll->lv_used_next;
6692
6693 return did_free;
6694}
6695
6696/*
6697 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6698 */
6699 static void
6700set_ref_in_ht(ht, copyID)
6701 hashtab_T *ht;
6702 int copyID;
6703{
6704 int todo;
6705 hashitem_T *hi;
6706
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006707 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006708 for (hi = ht->ht_array; todo > 0; ++hi)
6709 if (!HASHITEM_EMPTY(hi))
6710 {
6711 --todo;
6712 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6713 }
6714}
6715
6716/*
6717 * Mark all lists and dicts referenced through list "l" with "copyID".
6718 */
6719 static void
6720set_ref_in_list(l, copyID)
6721 list_T *l;
6722 int copyID;
6723{
6724 listitem_T *li;
6725
6726 for (li = l->lv_first; li != NULL; li = li->li_next)
6727 set_ref_in_item(&li->li_tv, copyID);
6728}
6729
6730/*
6731 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6732 */
6733 static void
6734set_ref_in_item(tv, copyID)
6735 typval_T *tv;
6736 int copyID;
6737{
6738 dict_T *dd;
6739 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006740
6741 switch (tv->v_type)
6742 {
6743 case VAR_DICT:
6744 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006745 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006746 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747 /* Didn't see this dict yet. */
6748 dd->dv_copyID = copyID;
6749 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006750 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006751 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006752
6753 case VAR_LIST:
6754 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006755 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006756 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757 /* Didn't see this list yet. */
6758 ll->lv_copyID = copyID;
6759 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006760 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006761 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006762 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006763 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764}
6765
6766/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006767 * Allocate an empty header for a dictionary.
6768 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006769 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006770dict_alloc()
6771{
Bram Moolenaar33570922005-01-25 22:26:29 +00006772 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006773
Bram Moolenaar33570922005-01-25 22:26:29 +00006774 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006775 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006776 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006777 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006778 if (first_dict != NULL)
6779 first_dict->dv_used_prev = d;
6780 d->dv_used_next = first_dict;
6781 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006782 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006783
Bram Moolenaar33570922005-01-25 22:26:29 +00006784 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006785 d->dv_lock = 0;
6786 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006787 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006788 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006789 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006790}
6791
6792/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006793 * Allocate an empty dict for a return value.
6794 * Returns OK or FAIL.
6795 */
6796 static int
6797rettv_dict_alloc(rettv)
6798 typval_T *rettv;
6799{
6800 dict_T *d = dict_alloc();
6801
6802 if (d == NULL)
6803 return FAIL;
6804
6805 rettv->vval.v_dict = d;
6806 rettv->v_type = VAR_DICT;
6807 ++d->dv_refcount;
6808 return OK;
6809}
6810
6811
6812/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006813 * Unreference a Dictionary: decrement the reference count and free it when it
6814 * becomes zero.
6815 */
6816 static void
6817dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006818 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006819{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006820 if (d != NULL && --d->dv_refcount <= 0)
6821 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006822}
6823
6824/*
6825 * Free a Dictionary, including all items it contains.
6826 * Ignores the reference count.
6827 */
6828 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006829dict_free(d, recurse)
6830 dict_T *d;
6831 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006832{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006833 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006834 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006835 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006836
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 /* Remove the dict from the list of dicts for garbage collection. */
6838 if (d->dv_used_prev == NULL)
6839 first_dict = d->dv_used_next;
6840 else
6841 d->dv_used_prev->dv_used_next = d->dv_used_next;
6842 if (d->dv_used_next != NULL)
6843 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6844
6845 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006846 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006847 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006848 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006849 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006850 if (!HASHITEM_EMPTY(hi))
6851 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006852 /* Remove the item before deleting it, just in case there is
6853 * something recursive causing trouble. */
6854 di = HI2DI(hi);
6855 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006856 if (recurse || (di->di_tv.v_type != VAR_LIST
6857 && di->di_tv.v_type != VAR_DICT))
6858 clear_tv(&di->di_tv);
6859 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006860 --todo;
6861 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006862 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006863 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006864 vim_free(d);
6865}
6866
6867/*
6868 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006869 * The "key" is copied to the new item.
6870 * Note that the value of the item "di_tv" still needs to be initialized!
6871 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006872 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006873 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006874dictitem_alloc(key)
6875 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006876{
Bram Moolenaar33570922005-01-25 22:26:29 +00006877 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006878
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006879 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006880 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006881 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006882 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006883 di->di_flags = 0;
6884 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006885 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006886}
6887
6888/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006889 * Make a copy of a Dictionary item.
6890 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006891 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006892dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006893 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006894{
Bram Moolenaar33570922005-01-25 22:26:29 +00006895 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006896
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006897 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6898 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899 if (di != NULL)
6900 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006901 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006902 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006903 copy_tv(&org->di_tv, &di->di_tv);
6904 }
6905 return di;
6906}
6907
6908/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006909 * Remove item "item" from Dictionary "dict" and free it.
6910 */
6911 static void
6912dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006913 dict_T *dict;
6914 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006915{
Bram Moolenaar33570922005-01-25 22:26:29 +00006916 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006917
Bram Moolenaar33570922005-01-25 22:26:29 +00006918 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006919 if (HASHITEM_EMPTY(hi))
6920 EMSG2(_(e_intern2), "dictitem_remove()");
6921 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006922 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006923 dictitem_free(item);
6924}
6925
6926/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006927 * Free a dict item. Also clears the value.
6928 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006929 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006930dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006931 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006932{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006933 clear_tv(&item->di_tv);
6934 vim_free(item);
6935}
6936
6937/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006938 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6939 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006940 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006941 * Returns NULL when out of memory.
6942 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006944dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006945 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006946 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006947 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006948{
Bram Moolenaar33570922005-01-25 22:26:29 +00006949 dict_T *copy;
6950 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006951 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006952 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006953
6954 if (orig == NULL)
6955 return NULL;
6956
6957 copy = dict_alloc();
6958 if (copy != NULL)
6959 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006960 if (copyID != 0)
6961 {
6962 orig->dv_copyID = copyID;
6963 orig->dv_copydict = copy;
6964 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006965 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006966 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006967 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006968 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006969 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006970 --todo;
6971
6972 di = dictitem_alloc(hi->hi_key);
6973 if (di == NULL)
6974 break;
6975 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006976 {
6977 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6978 copyID) == FAIL)
6979 {
6980 vim_free(di);
6981 break;
6982 }
6983 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006984 else
6985 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6986 if (dict_add(copy, di) == FAIL)
6987 {
6988 dictitem_free(di);
6989 break;
6990 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006991 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006992 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006993
Bram Moolenaare9a41262005-01-15 22:18:47 +00006994 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006995 if (todo > 0)
6996 {
6997 dict_unref(copy);
6998 copy = NULL;
6999 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007000 }
7001
7002 return copy;
7003}
7004
7005/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007006 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007007 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007008 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007009 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 dict_T *d;
7012 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013{
Bram Moolenaar33570922005-01-25 22:26:29 +00007014 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015}
7016
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007018 * Add a number or string entry to dictionary "d".
7019 * When "str" is NULL use number "nr", otherwise use "str".
7020 * Returns FAIL when out of memory and when key already exists.
7021 */
7022 int
7023dict_add_nr_str(d, key, nr, str)
7024 dict_T *d;
7025 char *key;
7026 long nr;
7027 char_u *str;
7028{
7029 dictitem_T *item;
7030
7031 item = dictitem_alloc((char_u *)key);
7032 if (item == NULL)
7033 return FAIL;
7034 item->di_tv.v_lock = 0;
7035 if (str == NULL)
7036 {
7037 item->di_tv.v_type = VAR_NUMBER;
7038 item->di_tv.vval.v_number = nr;
7039 }
7040 else
7041 {
7042 item->di_tv.v_type = VAR_STRING;
7043 item->di_tv.vval.v_string = vim_strsave(str);
7044 }
7045 if (dict_add(d, item) == FAIL)
7046 {
7047 dictitem_free(item);
7048 return FAIL;
7049 }
7050 return OK;
7051}
7052
7053/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007054 * Add a list entry to dictionary "d".
7055 * Returns FAIL when out of memory and when key already exists.
7056 */
7057 int
7058dict_add_list(d, key, list)
7059 dict_T *d;
7060 char *key;
7061 list_T *list;
7062{
7063 dictitem_T *item;
7064
7065 item = dictitem_alloc((char_u *)key);
7066 if (item == NULL)
7067 return FAIL;
7068 item->di_tv.v_lock = 0;
7069 item->di_tv.v_type = VAR_LIST;
7070 item->di_tv.vval.v_list = list;
7071 if (dict_add(d, item) == FAIL)
7072 {
7073 dictitem_free(item);
7074 return FAIL;
7075 }
7076 return OK;
7077}
7078
7079/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007080 * Get the number of items in a Dictionary.
7081 */
7082 static long
7083dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007084 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007085{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007086 if (d == NULL)
7087 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007088 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089}
7090
7091/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092 * Find item "key[len]" in Dictionary "d".
7093 * If "len" is negative use strlen(key).
7094 * Returns NULL when not found.
7095 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007096 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007097dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007098 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099 char_u *key;
7100 int len;
7101{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007102#define AKEYLEN 200
7103 char_u buf[AKEYLEN];
7104 char_u *akey;
7105 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007107
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007108 if (len < 0)
7109 akey = key;
7110 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007111 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 tofree = akey = vim_strnsave(key, len);
7113 if (akey == NULL)
7114 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007115 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116 else
7117 {
7118 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007119 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 akey = buf;
7121 }
7122
Bram Moolenaar33570922005-01-25 22:26:29 +00007123 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 vim_free(tofree);
7125 if (HASHITEM_EMPTY(hi))
7126 return NULL;
7127 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128}
7129
7130/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007131 * Get a string item from a dictionary.
7132 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007133 * Returns NULL if the entry doesn't exist or out of memory.
7134 */
7135 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007136get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007137 dict_T *d;
7138 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007139 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007140{
7141 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007142 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007143
7144 di = dict_find(d, key, -1);
7145 if (di == NULL)
7146 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007147 s = get_tv_string(&di->di_tv);
7148 if (save && s != NULL)
7149 s = vim_strsave(s);
7150 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007151}
7152
7153/*
7154 * Get a number item from a dictionary.
7155 * Returns 0 if the entry doesn't exist or out of memory.
7156 */
7157 long
7158get_dict_number(d, key)
7159 dict_T *d;
7160 char_u *key;
7161{
7162 dictitem_T *di;
7163
7164 di = dict_find(d, key, -1);
7165 if (di == NULL)
7166 return 0;
7167 return get_tv_number(&di->di_tv);
7168}
7169
7170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 * Return an allocated string with the string representation of a Dictionary.
7172 * May return NULL.
7173 */
7174 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007175dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007177 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007178{
7179 garray_T ga;
7180 int first = TRUE;
7181 char_u *tofree;
7182 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007185 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007186 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007187
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189 return NULL;
7190 ga_init2(&ga, (int)sizeof(char), 80);
7191 ga_append(&ga, '{');
7192
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007193 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007194 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007195 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007198 --todo;
7199
7200 if (first)
7201 first = FALSE;
7202 else
7203 ga_concat(&ga, (char_u *)", ");
7204
7205 tofree = string_quote(hi->hi_key, FALSE);
7206 if (tofree != NULL)
7207 {
7208 ga_concat(&ga, tofree);
7209 vim_free(tofree);
7210 }
7211 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007212 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213 if (s != NULL)
7214 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007216 if (s == NULL)
7217 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007220 if (todo > 0)
7221 {
7222 vim_free(ga.ga_data);
7223 return NULL;
7224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225
7226 ga_append(&ga, '}');
7227 ga_append(&ga, NUL);
7228 return (char_u *)ga.ga_data;
7229}
7230
7231/*
7232 * Allocate a variable for a Dictionary and fill it from "*arg".
7233 * Return OK or FAIL. Returns NOTDONE for {expr}.
7234 */
7235 static int
7236get_dict_tv(arg, rettv, evaluate)
7237 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007238 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239 int evaluate;
7240{
Bram Moolenaar33570922005-01-25 22:26:29 +00007241 dict_T *d = NULL;
7242 typval_T tvkey;
7243 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007244 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007245 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248
7249 /*
7250 * First check if it's not a curly-braces thing: {expr}.
7251 * Must do this without evaluating, otherwise a function may be called
7252 * twice. Unfortunately this means we need to call eval1() twice for the
7253 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007254 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007256 if (*start != '}')
7257 {
7258 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7259 return FAIL;
7260 if (*start == '}')
7261 return NOTDONE;
7262 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263
7264 if (evaluate)
7265 {
7266 d = dict_alloc();
7267 if (d == NULL)
7268 return FAIL;
7269 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 tvkey.v_type = VAR_UNKNOWN;
7271 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272
7273 *arg = skipwhite(*arg + 1);
7274 while (**arg != '}' && **arg != NUL)
7275 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277 goto failret;
7278 if (**arg != ':')
7279 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007280 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007281 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282 goto failret;
7283 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007284 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007286 key = get_tv_string_buf_chk(&tvkey, buf);
7287 if (key == NULL || *key == NUL)
7288 {
7289 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7290 if (key != NULL)
7291 EMSG(_(e_emptykey));
7292 clear_tv(&tvkey);
7293 goto failret;
7294 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296
7297 *arg = skipwhite(*arg + 1);
7298 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7299 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007300 if (evaluate)
7301 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 goto failret;
7303 }
7304 if (evaluate)
7305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007307 if (item != NULL)
7308 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007309 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 clear_tv(&tv);
7312 goto failret;
7313 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 item = dictitem_alloc(key);
7315 clear_tv(&tvkey);
7316 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007317 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007318 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007319 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (dict_add(d, item) == FAIL)
7321 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322 }
7323 }
7324
7325 if (**arg == '}')
7326 break;
7327 if (**arg != ',')
7328 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007329 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007330 goto failret;
7331 }
7332 *arg = skipwhite(*arg + 1);
7333 }
7334
7335 if (**arg != '}')
7336 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007337 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338failret:
7339 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007340 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341 return FAIL;
7342 }
7343
7344 *arg = skipwhite(*arg + 1);
7345 if (evaluate)
7346 {
7347 rettv->v_type = VAR_DICT;
7348 rettv->vval.v_dict = d;
7349 ++d->dv_refcount;
7350 }
7351
7352 return OK;
7353}
7354
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007356 * Return a string with the string representation of a variable.
7357 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007358 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007359 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007360 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007361 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007362 */
7363 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007364echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007366 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007367 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007368 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007369{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 static int recurse = 0;
7371 char_u *r = NULL;
7372
Bram Moolenaar33570922005-01-25 22:26:29 +00007373 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007375 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 *tofree = NULL;
7377 return NULL;
7378 }
7379 ++recurse;
7380
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007381 switch (tv->v_type)
7382 {
7383 case VAR_FUNC:
7384 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385 r = tv->vval.v_string;
7386 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007387
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007388 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007389 if (tv->vval.v_list == NULL)
7390 {
7391 *tofree = NULL;
7392 r = NULL;
7393 }
7394 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7395 {
7396 *tofree = NULL;
7397 r = (char_u *)"[...]";
7398 }
7399 else
7400 {
7401 tv->vval.v_list->lv_copyID = copyID;
7402 *tofree = list2string(tv, copyID);
7403 r = *tofree;
7404 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007405 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007406
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007408 if (tv->vval.v_dict == NULL)
7409 {
7410 *tofree = NULL;
7411 r = NULL;
7412 }
7413 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7414 {
7415 *tofree = NULL;
7416 r = (char_u *)"{...}";
7417 }
7418 else
7419 {
7420 tv->vval.v_dict->dv_copyID = copyID;
7421 *tofree = dict2string(tv, copyID);
7422 r = *tofree;
7423 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007424 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426 case VAR_STRING:
7427 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 *tofree = NULL;
7429 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007430 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007431
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007432#ifdef FEAT_FLOAT
7433 case VAR_FLOAT:
7434 *tofree = NULL;
7435 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7436 r = numbuf;
7437 break;
7438#endif
7439
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007440 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007441 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007442 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007443 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007444
7445 --recurse;
7446 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007447}
7448
7449/*
7450 * Return a string with the string representation of a variable.
7451 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7452 * "numbuf" is used for a number.
7453 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007454 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007455 */
7456 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007457tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007459 char_u **tofree;
7460 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007461 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007462{
7463 switch (tv->v_type)
7464 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007465 case VAR_FUNC:
7466 *tofree = string_quote(tv->vval.v_string, TRUE);
7467 return *tofree;
7468 case VAR_STRING:
7469 *tofree = string_quote(tv->vval.v_string, FALSE);
7470 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007471#ifdef FEAT_FLOAT
7472 case VAR_FLOAT:
7473 *tofree = NULL;
7474 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7475 return numbuf;
7476#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007478 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007481 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007482 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007483 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007484 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007485}
7486
7487/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 * Return string "str" in ' quotes, doubling ' characters.
7489 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007491 */
7492 static char_u *
7493string_quote(str, function)
7494 char_u *str;
7495 int function;
7496{
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007498 char_u *p, *r, *s;
7499
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 len = (function ? 13 : 3);
7501 if (str != NULL)
7502 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007503 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 for (p = str; *p != NUL; mb_ptr_adv(p))
7505 if (*p == '\'')
7506 ++len;
7507 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007508 s = r = alloc(len);
7509 if (r != NULL)
7510 {
7511 if (function)
7512 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007514 r += 10;
7515 }
7516 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007518 if (str != NULL)
7519 for (p = str; *p != NUL; )
7520 {
7521 if (*p == '\'')
7522 *r++ = '\'';
7523 MB_COPY_CHAR(p, r);
7524 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007526 if (function)
7527 *r++ = ')';
7528 *r++ = NUL;
7529 }
7530 return s;
7531}
7532
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007533#ifdef FEAT_FLOAT
7534/*
7535 * Convert the string "text" to a floating point number.
7536 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7537 * this always uses a decimal point.
7538 * Returns the length of the text that was consumed.
7539 */
7540 static int
7541string2float(text, value)
7542 char_u *text;
7543 float_T *value; /* result stored here */
7544{
7545 char *s = (char *)text;
7546 float_T f;
7547
7548 f = strtod(s, &s);
7549 *value = f;
7550 return (int)((char_u *)s - text);
7551}
7552#endif
7553
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 * Get the value of an environment variable.
7556 * "arg" is pointing to the '$'. It is advanced to after the name.
7557 * If the environment variable was not set, silently assume it is empty.
7558 * Always return OK.
7559 */
7560 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007561get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 int evaluate;
7565{
7566 char_u *string = NULL;
7567 int len;
7568 int cc;
7569 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007570 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571
7572 ++*arg;
7573 name = *arg;
7574 len = get_env_len(arg);
7575 if (evaluate)
7576 {
7577 if (len != 0)
7578 {
7579 cc = name[len];
7580 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007581 /* first try vim_getenv(), fast for normal environment vars */
7582 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007584 {
7585 if (!mustfree)
7586 string = vim_strsave(string);
7587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 else
7589 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007590 if (mustfree)
7591 vim_free(string);
7592
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 /* next try expanding things like $VIM and ${HOME} */
7594 string = expand_env_save(name - 1);
7595 if (string != NULL && *string == '$')
7596 {
7597 vim_free(string);
7598 string = NULL;
7599 }
7600 }
7601 name[len] = cc;
7602 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007603 rettv->v_type = VAR_STRING;
7604 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 }
7606
7607 return OK;
7608}
7609
7610/*
7611 * Array with names and number of arguments of all internal functions
7612 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7613 */
7614static struct fst
7615{
7616 char *f_name; /* function name */
7617 char f_min_argc; /* minimal number of arguments */
7618 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007620 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621} functions[] =
7622{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007623#ifdef FEAT_FLOAT
7624 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007625 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007626#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007627 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 {"append", 2, 2, f_append},
7629 {"argc", 0, 0, f_argc},
7630 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007631 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007632#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007633 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007634 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007635 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007638 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 {"bufexists", 1, 1, f_bufexists},
7640 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7641 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7642 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7643 {"buflisted", 1, 1, f_buflisted},
7644 {"bufloaded", 1, 1, f_bufloaded},
7645 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007646 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {"bufwinnr", 1, 1, f_bufwinnr},
7648 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007649 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007650 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007651#ifdef FEAT_FLOAT
7652 {"ceil", 1, 1, f_ceil},
7653#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007654 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {"char2nr", 1, 1, f_char2nr},
7656 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007657 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007659#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007660 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007661 {"complete_add", 1, 1, f_complete_add},
7662 {"complete_check", 0, 0, f_complete_check},
7663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007665 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007666#ifdef FEAT_FLOAT
7667 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007668 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007669#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007670 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007672 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007673 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 {"delete", 1, 1, f_delete},
7675 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007676 {"diff_filler", 1, 1, f_diff_filler},
7677 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007678 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007680 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {"eventhandler", 0, 0, f_eventhandler},
7682 {"executable", 1, 1, f_executable},
7683 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007684#ifdef FEAT_FLOAT
7685 {"exp", 1, 1, f_exp},
7686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007688 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007689 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7691 {"filereadable", 1, 1, f_filereadable},
7692 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007694 {"finddir", 1, 3, f_finddir},
7695 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007696#ifdef FEAT_FLOAT
7697 {"float2nr", 1, 1, f_float2nr},
7698 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007699 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007700#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007701 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {"fnamemodify", 2, 2, f_fnamemodify},
7703 {"foldclosed", 1, 1, f_foldclosed},
7704 {"foldclosedend", 1, 1, f_foldclosedend},
7705 {"foldlevel", 1, 1, f_foldlevel},
7706 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007707 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007709 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007710 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007711 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007712 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {"getbufvar", 2, 2, f_getbufvar},
7714 {"getchar", 0, 1, f_getchar},
7715 {"getcharmod", 0, 0, f_getcharmod},
7716 {"getcmdline", 0, 0, f_getcmdline},
7717 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007718 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007720 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007721 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {"getfsize", 1, 1, f_getfsize},
7723 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007724 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007725 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007726 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007727 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007728 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007729 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007730 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007731 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007733 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007734 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 {"getwinposx", 0, 0, f_getwinposx},
7736 {"getwinposy", 0, 0, f_getwinposy},
7737 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007738 {"glob", 1, 2, f_glob},
7739 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007741 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007742 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007743 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7745 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7746 {"histadd", 2, 2, f_histadd},
7747 {"histdel", 1, 2, f_histdel},
7748 {"histget", 1, 2, f_histget},
7749 {"histnr", 1, 1, f_histnr},
7750 {"hlID", 1, 1, f_hlID},
7751 {"hlexists", 1, 1, f_hlexists},
7752 {"hostname", 0, 0, f_hostname},
7753 {"iconv", 3, 3, f_iconv},
7754 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007755 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007756 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007758 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 {"inputrestore", 0, 0, f_inputrestore},
7760 {"inputsave", 0, 0, f_inputsave},
7761 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007762 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007764 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007766 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007767 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007769 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 {"libcall", 3, 3, f_libcall},
7771 {"libcallnr", 3, 3, f_libcallnr},
7772 {"line", 1, 1, f_line},
7773 {"line2byte", 1, 1, f_line2byte},
7774 {"lispindent", 1, 1, f_lispindent},
7775 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007776#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007777 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007778 {"log10", 1, 1, f_log10},
7779#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007780 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007781 {"maparg", 1, 3, f_maparg},
7782 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007783 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007784 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007785 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007786 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007787 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007788 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007789 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007790 {"max", 1, 1, f_max},
7791 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007792#ifdef vim_mkdir
7793 {"mkdir", 1, 3, f_mkdir},
7794#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007795 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007796#ifdef FEAT_MZSCHEME
7797 {"mzeval", 1, 1, f_mzeval},
7798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {"nextnonblank", 1, 1, f_nextnonblank},
7800 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007801 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007802#ifdef FEAT_FLOAT
7803 {"pow", 2, 2, f_pow},
7804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007806 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007807 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007808 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007809 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007810 {"reltime", 0, 2, f_reltime},
7811 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 {"remote_expr", 2, 3, f_remote_expr},
7813 {"remote_foreground", 1, 1, f_remote_foreground},
7814 {"remote_peek", 1, 2, f_remote_peek},
7815 {"remote_read", 1, 1, f_remote_read},
7816 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007817 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007819 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007821 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007822#ifdef FEAT_FLOAT
7823 {"round", 1, 1, f_round},
7824#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007825 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007826 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007827 {"searchpair", 3, 7, f_searchpair},
7828 {"searchpairpos", 3, 7, f_searchpairpos},
7829 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {"server2client", 2, 2, f_server2client},
7831 {"serverlist", 0, 0, f_serverlist},
7832 {"setbufvar", 3, 3, f_setbufvar},
7833 {"setcmdpos", 1, 1, f_setcmdpos},
7834 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007835 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007836 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007837 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007838 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007840 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007841 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007843 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845#ifdef FEAT_FLOAT
7846 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007847 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007848#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007849 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007850 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007851 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007852 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007853 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007854#ifdef FEAT_FLOAT
7855 {"sqrt", 1, 1, f_sqrt},
7856 {"str2float", 1, 1, f_str2float},
7857#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007858 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859#ifdef HAVE_STRFTIME
7860 {"strftime", 1, 2, f_strftime},
7861#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007863 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {"strlen", 1, 1, f_strlen},
7865 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007866 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {"strtrans", 1, 1, f_strtrans},
7868 {"submatch", 1, 1, f_submatch},
7869 {"substitute", 4, 4, f_substitute},
7870 {"synID", 3, 3, f_synID},
7871 {"synIDattr", 2, 3, f_synIDattr},
7872 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007873 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007874 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007875 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007876 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007877 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007878 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007879 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007880#ifdef FEAT_FLOAT
7881 {"tan", 1, 1, f_tan},
7882 {"tanh", 1, 1, f_tanh},
7883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007885 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {"tolower", 1, 1, f_tolower},
7887 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007888 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007889#ifdef FEAT_FLOAT
7890 {"trunc", 1, 1, f_trunc},
7891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007893 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007894 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007895 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {"virtcol", 1, 1, f_virtcol},
7897 {"visualmode", 0, 1, f_visualmode},
7898 {"winbufnr", 1, 1, f_winbufnr},
7899 {"wincol", 0, 0, f_wincol},
7900 {"winheight", 1, 1, f_winheight},
7901 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007902 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007904 {"winrestview", 1, 1, f_winrestview},
7905 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007907 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908};
7909
7910#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7911
7912/*
7913 * Function given to ExpandGeneric() to obtain the list of internal
7914 * or user defined function names.
7915 */
7916 char_u *
7917get_function_name(xp, idx)
7918 expand_T *xp;
7919 int idx;
7920{
7921 static int intidx = -1;
7922 char_u *name;
7923
7924 if (idx == 0)
7925 intidx = -1;
7926 if (intidx < 0)
7927 {
7928 name = get_user_func_name(xp, idx);
7929 if (name != NULL)
7930 return name;
7931 }
7932 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7933 {
7934 STRCPY(IObuff, functions[intidx].f_name);
7935 STRCAT(IObuff, "(");
7936 if (functions[intidx].f_max_argc == 0)
7937 STRCAT(IObuff, ")");
7938 return IObuff;
7939 }
7940
7941 return NULL;
7942}
7943
7944/*
7945 * Function given to ExpandGeneric() to obtain the list of internal or
7946 * user defined variable or function names.
7947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 char_u *
7949get_expr_name(xp, idx)
7950 expand_T *xp;
7951 int idx;
7952{
7953 static int intidx = -1;
7954 char_u *name;
7955
7956 if (idx == 0)
7957 intidx = -1;
7958 if (intidx < 0)
7959 {
7960 name = get_function_name(xp, idx);
7961 if (name != NULL)
7962 return name;
7963 }
7964 return get_user_var_name(xp, ++intidx);
7965}
7966
7967#endif /* FEAT_CMDL_COMPL */
7968
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007969#if defined(EBCDIC) || defined(PROTO)
7970/*
7971 * Compare struct fst by function name.
7972 */
7973 static int
7974compare_func_name(s1, s2)
7975 const void *s1;
7976 const void *s2;
7977{
7978 struct fst *p1 = (struct fst *)s1;
7979 struct fst *p2 = (struct fst *)s2;
7980
7981 return STRCMP(p1->f_name, p2->f_name);
7982}
7983
7984/*
7985 * Sort the function table by function name.
7986 * The sorting of the table above is ASCII dependant.
7987 * On machines using EBCDIC we have to sort it.
7988 */
7989 static void
7990sortFunctions()
7991{
7992 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7993
7994 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
7995}
7996#endif
7997
7998
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999/*
8000 * Find internal function in table above.
8001 * Return index, or -1 if not found
8002 */
8003 static int
8004find_internal_func(name)
8005 char_u *name; /* name of the function */
8006{
8007 int first = 0;
8008 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8009 int cmp;
8010 int x;
8011
8012 /*
8013 * Find the function name in the table. Binary search.
8014 */
8015 while (first <= last)
8016 {
8017 x = first + ((unsigned)(last - first) >> 1);
8018 cmp = STRCMP(name, functions[x].f_name);
8019 if (cmp < 0)
8020 last = x - 1;
8021 else if (cmp > 0)
8022 first = x + 1;
8023 else
8024 return x;
8025 }
8026 return -1;
8027}
8028
8029/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008030 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8031 * name it contains, otherwise return "name".
8032 */
8033 static char_u *
8034deref_func_name(name, lenp)
8035 char_u *name;
8036 int *lenp;
8037{
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008039 int cc;
8040
8041 cc = name[*lenp];
8042 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008043 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008044 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008046 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008047 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008048 {
8049 *lenp = 0;
8050 return (char_u *)""; /* just in case */
8051 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008052 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008054 }
8055
8056 return name;
8057}
8058
8059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 * Allocate a variable for the result of a function.
8061 * Return OK or FAIL.
8062 */
8063 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008064get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8065 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 char_u *name; /* name of the function */
8067 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 char_u **arg; /* argument, pointing to the '(' */
8070 linenr_T firstline; /* first line of range */
8071 linenr_T lastline; /* last line of range */
8072 int *doesrange; /* return: function handled range */
8073 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008074 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076 char_u *argp;
8077 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008078 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 int argcount = 0; /* number of arguments found */
8080
8081 /*
8082 * Get the arguments.
8083 */
8084 argp = *arg;
8085 while (argcount < MAX_FUNC_ARGS)
8086 {
8087 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8088 if (*argp == ')' || *argp == ',' || *argp == NUL)
8089 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8091 {
8092 ret = FAIL;
8093 break;
8094 }
8095 ++argcount;
8096 if (*argp != ',')
8097 break;
8098 }
8099 if (*argp == ')')
8100 ++argp;
8101 else
8102 ret = FAIL;
8103
8104 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008105 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008106 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008108 {
8109 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008110 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008111 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008112 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114
8115 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008116 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117
8118 *arg = skipwhite(argp);
8119 return ret;
8120}
8121
8122
8123/*
8124 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008125 * Return OK when the function can't be called, FAIL otherwise.
8126 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 */
8128 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008129call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008130 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008131 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008135 typval_T *argvars; /* vars for arguments, must have "argcount"
8136 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 linenr_T firstline; /* first line of range */
8138 linenr_T lastline; /* last line of range */
8139 int *doesrange; /* return: function handled range */
8140 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008141 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142{
8143 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144#define ERROR_UNKNOWN 0
8145#define ERROR_TOOMANY 1
8146#define ERROR_TOOFEW 2
8147#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008148#define ERROR_DICT 4
8149#define ERROR_NONE 5
8150#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 int error = ERROR_NONE;
8152 int i;
8153 int llen;
8154 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155#define FLEN_FIXED 40
8156 char_u fname_buf[FLEN_FIXED + 1];
8157 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008158 char_u *name;
8159
8160 /* Make a copy of the name, if it comes from a funcref variable it could
8161 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008162 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008163 if (name == NULL)
8164 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165
8166 /*
8167 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8168 * Change <SNR>123_name() to K_SNR 123_name().
8169 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 llen = eval_fname_script(name);
8172 if (llen > 0)
8173 {
8174 fname_buf[0] = K_SPECIAL;
8175 fname_buf[1] = KS_EXTRA;
8176 fname_buf[2] = (int)KE_SNR;
8177 i = 3;
8178 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8179 {
8180 if (current_SID <= 0)
8181 error = ERROR_SCRIPT;
8182 else
8183 {
8184 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8185 i = (int)STRLEN(fname_buf);
8186 }
8187 }
8188 if (i + STRLEN(name + llen) < FLEN_FIXED)
8189 {
8190 STRCPY(fname_buf + i, name + llen);
8191 fname = fname_buf;
8192 }
8193 else
8194 {
8195 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8196 if (fname == NULL)
8197 error = ERROR_OTHER;
8198 else
8199 {
8200 mch_memmove(fname, fname_buf, (size_t)i);
8201 STRCPY(fname + i, name + llen);
8202 }
8203 }
8204 }
8205 else
8206 fname = name;
8207
8208 *doesrange = FALSE;
8209
8210
8211 /* execute the function if no errors detected and executing */
8212 if (evaluate && error == ERROR_NONE)
8213 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008214 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8215 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 error = ERROR_UNKNOWN;
8217
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008218 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {
8220 /*
8221 * User defined function.
8222 */
8223 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008224
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008226 /* Trigger FuncUndefined event, may load the function. */
8227 if (fp == NULL
8228 && apply_autocmds(EVENT_FUNCUNDEFINED,
8229 fname, fname, TRUE, NULL)
8230 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008232 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 fp = find_func(fname);
8234 }
8235#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008236 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008237 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008238 {
8239 /* loaded a package, search for the function again */
8240 fp = find_func(fname);
8241 }
8242
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 if (fp != NULL)
8244 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008245 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008247 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008249 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008251 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008252 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 else
8254 {
8255 /*
8256 * Call the user function.
8257 * Save and restore search patterns, script variables and
8258 * redo buffer.
8259 */
8260 save_search_patterns();
8261 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008262 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008263 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008264 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008265 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8266 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8267 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008268 /* Function was unreferenced while being used, free it
8269 * now. */
8270 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 restoreRedobuff();
8272 restore_search_patterns();
8273 error = ERROR_NONE;
8274 }
8275 }
8276 }
8277 else
8278 {
8279 /*
8280 * Find the function name in the table, call its implementation.
8281 */
8282 i = find_internal_func(fname);
8283 if (i >= 0)
8284 {
8285 if (argcount < functions[i].f_min_argc)
8286 error = ERROR_TOOFEW;
8287 else if (argcount > functions[i].f_max_argc)
8288 error = ERROR_TOOMANY;
8289 else
8290 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008291 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 error = ERROR_NONE;
8294 }
8295 }
8296 }
8297 /*
8298 * The function call (or "FuncUndefined" autocommand sequence) might
8299 * have been aborted by an error, an interrupt, or an explicitly thrown
8300 * exception that has not been caught so far. This situation can be
8301 * tested for by calling aborting(). For an error in an internal
8302 * function or for the "E132" error in call_user_func(), however, the
8303 * throw point at which the "force_abort" flag (temporarily reset by
8304 * emsg()) is normally updated has not been reached yet. We need to
8305 * update that flag first to make aborting() reliable.
8306 */
8307 update_force_abort();
8308 }
8309 if (error == ERROR_NONE)
8310 ret = OK;
8311
8312 /*
8313 * Report an error unless the argument evaluation or function call has been
8314 * cancelled due to an aborting error, an interrupt, or an exception.
8315 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008316 if (!aborting())
8317 {
8318 switch (error)
8319 {
8320 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008321 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008322 break;
8323 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008324 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008325 break;
8326 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008327 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008328 name);
8329 break;
8330 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008331 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008332 name);
8333 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008334 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008335 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008336 name);
8337 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008338 }
8339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 if (fname != name && fname != fname_buf)
8342 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008343 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
8345 return ret;
8346}
8347
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008348/*
8349 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008350 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008351 */
8352 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008353emsg_funcname(ermsg, name)
8354 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008355 char_u *name;
8356{
8357 char_u *p;
8358
8359 if (*name == K_SPECIAL)
8360 p = concat_str((char_u *)"<SNR>", name + 3);
8361 else
8362 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008363 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008364 if (p != name)
8365 vim_free(p);
8366}
8367
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008368/*
8369 * Return TRUE for a non-zero Number and a non-empty String.
8370 */
8371 static int
8372non_zero_arg(argvars)
8373 typval_T *argvars;
8374{
8375 return ((argvars[0].v_type == VAR_NUMBER
8376 && argvars[0].vval.v_number != 0)
8377 || (argvars[0].v_type == VAR_STRING
8378 && argvars[0].vval.v_string != NULL
8379 && *argvars[0].vval.v_string != NUL));
8380}
8381
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382/*********************************************
8383 * Implementation of the built-in functions
8384 */
8385
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008386#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008387static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8388
8389/*
8390 * Get the float value of "argvars[0]" into "f".
8391 * Returns FAIL when the argument is not a Number or Float.
8392 */
8393 static int
8394get_float_arg(argvars, f)
8395 typval_T *argvars;
8396 float_T *f;
8397{
8398 if (argvars[0].v_type == VAR_FLOAT)
8399 {
8400 *f = argvars[0].vval.v_float;
8401 return OK;
8402 }
8403 if (argvars[0].v_type == VAR_NUMBER)
8404 {
8405 *f = (float_T)argvars[0].vval.v_number;
8406 return OK;
8407 }
8408 EMSG(_("E808: Number or Float required"));
8409 return FAIL;
8410}
8411
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008412/*
8413 * "abs(expr)" function
8414 */
8415 static void
8416f_abs(argvars, rettv)
8417 typval_T *argvars;
8418 typval_T *rettv;
8419{
8420 if (argvars[0].v_type == VAR_FLOAT)
8421 {
8422 rettv->v_type = VAR_FLOAT;
8423 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8424 }
8425 else
8426 {
8427 varnumber_T n;
8428 int error = FALSE;
8429
8430 n = get_tv_number_chk(&argvars[0], &error);
8431 if (error)
8432 rettv->vval.v_number = -1;
8433 else if (n > 0)
8434 rettv->vval.v_number = n;
8435 else
8436 rettv->vval.v_number = -n;
8437 }
8438}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008439
8440/*
8441 * "acos()" function
8442 */
8443 static void
8444f_acos(argvars, rettv)
8445 typval_T *argvars;
8446 typval_T *rettv;
8447{
8448 float_T f;
8449
8450 rettv->v_type = VAR_FLOAT;
8451 if (get_float_arg(argvars, &f) == OK)
8452 rettv->vval.v_float = acos(f);
8453 else
8454 rettv->vval.v_float = 0.0;
8455}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008456#endif
8457
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008459 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 */
8461 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008462f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008463 typval_T *argvars;
8464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465{
Bram Moolenaar33570922005-01-25 22:26:29 +00008466 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008468 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008469 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008471 if ((l = argvars[0].vval.v_list) != NULL
8472 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8473 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008474 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008475 }
8476 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008477 EMSG(_(e_listreq));
8478}
8479
8480/*
8481 * "append(lnum, string/list)" function
8482 */
8483 static void
8484f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008485 typval_T *argvars;
8486 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008487{
8488 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008489 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008490 list_T *l = NULL;
8491 listitem_T *li = NULL;
8492 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008493 long added = 0;
8494
Bram Moolenaar0d660222005-01-07 21:51:51 +00008495 lnum = get_tv_lnum(argvars);
8496 if (lnum >= 0
8497 && lnum <= curbuf->b_ml.ml_line_count
8498 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008499 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008500 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008502 l = argvars[1].vval.v_list;
8503 if (l == NULL)
8504 return;
8505 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008506 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008507 for (;;)
8508 {
8509 if (l == NULL)
8510 tv = &argvars[1]; /* append a string */
8511 else if (li == NULL)
8512 break; /* end of list */
8513 else
8514 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008515 line = get_tv_string_chk(tv);
8516 if (line == NULL) /* type error */
8517 {
8518 rettv->vval.v_number = 1; /* Failed */
8519 break;
8520 }
8521 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008522 ++added;
8523 if (l == NULL)
8524 break;
8525 li = li->li_next;
8526 }
8527
8528 appended_lines_mark(lnum, added);
8529 if (curwin->w_cursor.lnum > lnum)
8530 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008532 else
8533 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534}
8535
8536/*
8537 * "argc()" function
8538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008541 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008544 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545}
8546
8547/*
8548 * "argidx()" function
8549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008551f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008552 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008555 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556}
8557
8558/*
8559 * "argv(nr)" function
8560 */
8561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008563 typval_T *argvars;
8564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565{
8566 int idx;
8567
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008568 if (argvars[0].v_type != VAR_UNKNOWN)
8569 {
8570 idx = get_tv_number_chk(&argvars[0], NULL);
8571 if (idx >= 0 && idx < ARGCOUNT)
8572 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8573 else
8574 rettv->vval.v_string = NULL;
8575 rettv->v_type = VAR_STRING;
8576 }
8577 else if (rettv_list_alloc(rettv) == OK)
8578 for (idx = 0; idx < ARGCOUNT; ++idx)
8579 list_append_string(rettv->vval.v_list,
8580 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581}
8582
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008583#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008584/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008585 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008586 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008587 static void
8588f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008589 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008590 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008591{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008592 float_T f;
8593
8594 rettv->v_type = VAR_FLOAT;
8595 if (get_float_arg(argvars, &f) == OK)
8596 rettv->vval.v_float = asin(f);
8597 else
8598 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008599}
8600
8601/*
8602 * "atan()" function
8603 */
8604 static void
8605f_atan(argvars, rettv)
8606 typval_T *argvars;
8607 typval_T *rettv;
8608{
8609 float_T f;
8610
8611 rettv->v_type = VAR_FLOAT;
8612 if (get_float_arg(argvars, &f) == OK)
8613 rettv->vval.v_float = atan(f);
8614 else
8615 rettv->vval.v_float = 0.0;
8616}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008617
8618/*
8619 * "atan2()" function
8620 */
8621 static void
8622f_atan2(argvars, rettv)
8623 typval_T *argvars;
8624 typval_T *rettv;
8625{
8626 float_T fx, fy;
8627
8628 rettv->v_type = VAR_FLOAT;
8629 if (get_float_arg(argvars, &fx) == OK
8630 && get_float_arg(&argvars[1], &fy) == OK)
8631 rettv->vval.v_float = atan2(fx, fy);
8632 else
8633 rettv->vval.v_float = 0.0;
8634}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008635#endif
8636
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637/*
8638 * "browse(save, title, initdir, default)" function
8639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008641f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008642 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644{
8645#ifdef FEAT_BROWSE
8646 int save;
8647 char_u *title;
8648 char_u *initdir;
8649 char_u *defname;
8650 char_u buf[NUMBUFLEN];
8651 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008652 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008654 save = get_tv_number_chk(&argvars[0], &error);
8655 title = get_tv_string_chk(&argvars[1]);
8656 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8657 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008659 if (error || title == NULL || initdir == NULL || defname == NULL)
8660 rettv->vval.v_string = NULL;
8661 else
8662 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008663 do_browse(save ? BROWSE_SAVE : 0,
8664 title, defname, NULL, initdir, NULL, curbuf);
8665#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008666 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008667#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008669}
8670
8671/*
8672 * "browsedir(title, initdir)" function
8673 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008676 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008677 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008678{
8679#ifdef FEAT_BROWSE
8680 char_u *title;
8681 char_u *initdir;
8682 char_u buf[NUMBUFLEN];
8683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008684 title = get_tv_string_chk(&argvars[0]);
8685 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008687 if (title == NULL || initdir == NULL)
8688 rettv->vval.v_string = NULL;
8689 else
8690 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008691 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008693 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696}
8697
Bram Moolenaar33570922005-01-25 22:26:29 +00008698static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008699
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700/*
8701 * Find a buffer by number or exact name.
8702 */
8703 static buf_T *
8704find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008705 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706{
8707 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008709 if (avar->v_type == VAR_NUMBER)
8710 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008711 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008713 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008714 if (buf == NULL)
8715 {
8716 /* No full path name match, try a match with a URL or a "nofile"
8717 * buffer, these don't use the full path. */
8718 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8719 if (buf->b_fname != NULL
8720 && (path_with_url(buf->b_fname)
8721#ifdef FEAT_QUICKFIX
8722 || bt_nofile(buf)
8723#endif
8724 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008725 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008726 break;
8727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 }
8729 return buf;
8730}
8731
8732/*
8733 * "bufexists(expr)" function
8734 */
8735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008736f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008737 typval_T *argvars;
8738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008740 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741}
8742
8743/*
8744 * "buflisted(expr)" function
8745 */
8746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008748 typval_T *argvars;
8749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
8751 buf_T *buf;
8752
8753 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755}
8756
8757/*
8758 * "bufloaded(expr)" function
8759 */
8760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 typval_T *argvars;
8763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
8765 buf_T *buf;
8766
8767 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008768 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769}
8770
Bram Moolenaar33570922005-01-25 22:26:29 +00008771static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008772
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773/*
8774 * Get buffer by number or pattern.
8775 */
8776 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008778 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 int save_magic;
8782 char_u *save_cpo;
8783 buf_T *buf;
8784
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785 if (tv->v_type == VAR_NUMBER)
8786 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008787 if (tv->v_type != VAR_STRING)
8788 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 if (name == NULL || *name == NUL)
8790 return curbuf;
8791 if (name[0] == '$' && name[1] == NUL)
8792 return lastbuf;
8793
8794 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8795 save_magic = p_magic;
8796 p_magic = TRUE;
8797 save_cpo = p_cpo;
8798 p_cpo = (char_u *)"";
8799
8800 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8801 TRUE, FALSE));
8802
8803 p_magic = save_magic;
8804 p_cpo = save_cpo;
8805
8806 /* If not found, try expanding the name, like done for bufexists(). */
8807 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809
8810 return buf;
8811}
8812
8813/*
8814 * "bufname(expr)" function
8815 */
8816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008817f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008818 typval_T *argvars;
8819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 buf_T *buf;
8822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008823 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 buf = get_buf_tv(&argvars[0]);
8826 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008828 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 --emsg_off;
8832}
8833
8834/*
8835 * "bufnr(expr)" function
8836 */
8837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008839 typval_T *argvars;
8840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
8842 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008843 int error = FALSE;
8844 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008846 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008849 --emsg_off;
8850
8851 /* If the buffer isn't found and the second argument is not zero create a
8852 * new buffer. */
8853 if (buf == NULL
8854 && argvars[1].v_type != VAR_UNKNOWN
8855 && get_tv_number_chk(&argvars[1], &error) != 0
8856 && !error
8857 && (name = get_tv_string_chk(&argvars[0])) != NULL
8858 && !error)
8859 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8860
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865}
8866
8867/*
8868 * "bufwinnr(nr)" function
8869 */
8870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 typval_T *argvars;
8873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874{
8875#ifdef FEAT_WINDOWS
8876 win_T *wp;
8877 int winnr = 0;
8878#endif
8879 buf_T *buf;
8880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008881 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008883 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884#ifdef FEAT_WINDOWS
8885 for (wp = firstwin; wp; wp = wp->w_next)
8886 {
8887 ++winnr;
8888 if (wp->w_buffer == buf)
8889 break;
8890 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894#endif
8895 --emsg_off;
8896}
8897
8898/*
8899 * "byte2line(byte)" function
8900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008903 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
8906#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908#else
8909 long boff = 0;
8910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 (linenr_T)0, &boff);
8917#endif
8918}
8919
8920/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008921 * "byteidx()" function
8922 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008925 typval_T *argvars;
8926 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008927{
8928#ifdef FEAT_MBYTE
8929 char_u *t;
8930#endif
8931 char_u *str;
8932 long idx;
8933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 str = get_tv_string_chk(&argvars[0]);
8935 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008937 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008938 return;
8939
8940#ifdef FEAT_MBYTE
8941 t = str;
8942 for ( ; idx > 0; idx--)
8943 {
8944 if (*t == NUL) /* EOL reached */
8945 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008946 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008947 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008948 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008949#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008950 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008951 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008952#endif
8953}
8954
8955/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008956 * "call(func, arglist)" function
8957 */
8958 static void
8959f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008960 typval_T *argvars;
8961 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008962{
8963 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008964 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008965 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008966 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008967 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008969
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008970 if (argvars[1].v_type != VAR_LIST)
8971 {
8972 EMSG(_(e_listreq));
8973 return;
8974 }
8975 if (argvars[1].vval.v_list == NULL)
8976 return;
8977
8978 if (argvars[0].v_type == VAR_FUNC)
8979 func = argvars[0].vval.v_string;
8980 else
8981 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008982 if (*func == NUL)
8983 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008984
Bram Moolenaare9a41262005-01-15 22:18:47 +00008985 if (argvars[2].v_type != VAR_UNKNOWN)
8986 {
8987 if (argvars[2].v_type != VAR_DICT)
8988 {
8989 EMSG(_(e_dictreq));
8990 return;
8991 }
8992 selfdict = argvars[2].vval.v_dict;
8993 }
8994
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008995 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8996 item = item->li_next)
8997 {
8998 if (argc == MAX_FUNC_ARGS)
8999 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009000 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009001 break;
9002 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009003 /* Make a copy of each argument. This is needed to be able to set
9004 * v_lock to VAR_FIXED in the copy without changing the original list.
9005 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009006 copy_tv(&item->li_tv, &argv[argc++]);
9007 }
9008
9009 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009010 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009011 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9012 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009013
9014 /* Free the arguments. */
9015 while (argc > 0)
9016 clear_tv(&argv[--argc]);
9017}
9018
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009019#ifdef FEAT_FLOAT
9020/*
9021 * "ceil({float})" function
9022 */
9023 static void
9024f_ceil(argvars, rettv)
9025 typval_T *argvars;
9026 typval_T *rettv;
9027{
9028 float_T f;
9029
9030 rettv->v_type = VAR_FLOAT;
9031 if (get_float_arg(argvars, &f) == OK)
9032 rettv->vval.v_float = ceil(f);
9033 else
9034 rettv->vval.v_float = 0.0;
9035}
9036#endif
9037
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009038/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009039 * "changenr()" function
9040 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009041 static void
9042f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009043 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009044 typval_T *rettv;
9045{
9046 rettv->vval.v_number = curbuf->b_u_seq_cur;
9047}
9048
9049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 * "char2nr(string)" function
9051 */
9052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009053f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 typval_T *argvars;
9055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
9057#ifdef FEAT_MBYTE
9058 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009059 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 else
9061#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063}
9064
9065/*
9066 * "cindent(lnum)" function
9067 */
9068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009070 typval_T *argvars;
9071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072{
9073#ifdef FEAT_CINDENT
9074 pos_T pos;
9075 linenr_T lnum;
9076
9077 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9080 {
9081 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 curwin->w_cursor = pos;
9084 }
9085 else
9086#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088}
9089
9090/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009091 * "clearmatches()" function
9092 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009093 static void
9094f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009095 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009096 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009097{
9098#ifdef FEAT_SEARCH_EXTRA
9099 clear_matches(curwin);
9100#endif
9101}
9102
9103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 * "col(string)" function
9105 */
9106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009108 typval_T *argvars;
9109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110{
9111 colnr_T col = 0;
9112 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009113 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009115 fp = var2fpos(&argvars[0], FALSE, &fnum);
9116 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 {
9118 if (fp->col == MAXCOL)
9119 {
9120 /* '> can be MAXCOL, get the length of the line then */
9121 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009122 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 else
9124 col = MAXCOL;
9125 }
9126 else
9127 {
9128 col = fp->col + 1;
9129#ifdef FEAT_VIRTUALEDIT
9130 /* col(".") when the cursor is on the NUL at the end of the line
9131 * because of "coladd" can be seen as an extra column. */
9132 if (virtual_active() && fp == &curwin->w_cursor)
9133 {
9134 char_u *p = ml_get_cursor();
9135
9136 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9137 curwin->w_virtcol - curwin->w_cursor.coladd))
9138 {
9139# ifdef FEAT_MBYTE
9140 int l;
9141
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009142 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 col += l;
9144# else
9145 if (*p != NUL && p[1] == NUL)
9146 ++col;
9147# endif
9148 }
9149 }
9150#endif
9151 }
9152 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154}
9155
Bram Moolenaar572cb562005-08-05 21:35:02 +00009156#if defined(FEAT_INS_EXPAND)
9157/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009158 * "complete()" function
9159 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009160 static void
9161f_complete(argvars, rettv)
9162 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009163 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009164{
9165 int startcol;
9166
9167 if ((State & INSERT) == 0)
9168 {
9169 EMSG(_("E785: complete() can only be used in Insert mode"));
9170 return;
9171 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009172
9173 /* Check for undo allowed here, because if something was already inserted
9174 * the line was already saved for undo and this check isn't done. */
9175 if (!undo_allowed())
9176 return;
9177
Bram Moolenaarade00832006-03-10 21:46:58 +00009178 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9179 {
9180 EMSG(_(e_invarg));
9181 return;
9182 }
9183
9184 startcol = get_tv_number_chk(&argvars[0], NULL);
9185 if (startcol <= 0)
9186 return;
9187
9188 set_completion(startcol - 1, argvars[1].vval.v_list);
9189}
9190
9191/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009192 * "complete_add()" function
9193 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009194 static void
9195f_complete_add(argvars, rettv)
9196 typval_T *argvars;
9197 typval_T *rettv;
9198{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009199 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009200}
9201
9202/*
9203 * "complete_check()" function
9204 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009205 static void
9206f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009207 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009208 typval_T *rettv;
9209{
9210 int saved = RedrawingDisabled;
9211
9212 RedrawingDisabled = 0;
9213 ins_compl_check_keys(0);
9214 rettv->vval.v_number = compl_interrupted;
9215 RedrawingDisabled = saved;
9216}
9217#endif
9218
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219/*
9220 * "confirm(message, buttons[, default [, type]])" function
9221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009223f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009224 typval_T *argvars UNUSED;
9225 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226{
9227#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9228 char_u *message;
9229 char_u *buttons = NULL;
9230 char_u buf[NUMBUFLEN];
9231 char_u buf2[NUMBUFLEN];
9232 int def = 1;
9233 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234 char_u *typestr;
9235 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 message = get_tv_string_chk(&argvars[0]);
9238 if (message == NULL)
9239 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009240 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9243 if (buttons == NULL)
9244 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009245 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009247 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009248 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9251 if (typestr == NULL)
9252 error = TRUE;
9253 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255 switch (TOUPPER_ASC(*typestr))
9256 {
9257 case 'E': type = VIM_ERROR; break;
9258 case 'Q': type = VIM_QUESTION; break;
9259 case 'I': type = VIM_INFO; break;
9260 case 'W': type = VIM_WARNING; break;
9261 case 'G': type = VIM_GENERIC; break;
9262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 }
9264 }
9265 }
9266 }
9267
9268 if (buttons == NULL || *buttons == NUL)
9269 buttons = (char_u *)_("&Ok");
9270
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009271 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009272 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274#endif
9275}
9276
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009277/*
9278 * "copy()" function
9279 */
9280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009281f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009282 typval_T *argvars;
9283 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009284{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009285 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009286}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009288#ifdef FEAT_FLOAT
9289/*
9290 * "cos()" function
9291 */
9292 static void
9293f_cos(argvars, rettv)
9294 typval_T *argvars;
9295 typval_T *rettv;
9296{
9297 float_T f;
9298
9299 rettv->v_type = VAR_FLOAT;
9300 if (get_float_arg(argvars, &f) == OK)
9301 rettv->vval.v_float = cos(f);
9302 else
9303 rettv->vval.v_float = 0.0;
9304}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009305
9306/*
9307 * "cosh()" function
9308 */
9309 static void
9310f_cosh(argvars, rettv)
9311 typval_T *argvars;
9312 typval_T *rettv;
9313{
9314 float_T f;
9315
9316 rettv->v_type = VAR_FLOAT;
9317 if (get_float_arg(argvars, &f) == OK)
9318 rettv->vval.v_float = cosh(f);
9319 else
9320 rettv->vval.v_float = 0.0;
9321}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009322#endif
9323
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009325 * "count()" function
9326 */
9327 static void
9328f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009329 typval_T *argvars;
9330 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009331{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009332 long n = 0;
9333 int ic = FALSE;
9334
Bram Moolenaare9a41262005-01-15 22:18:47 +00009335 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009336 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009337 listitem_T *li;
9338 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009339 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009340
Bram Moolenaare9a41262005-01-15 22:18:47 +00009341 if ((l = argvars[0].vval.v_list) != NULL)
9342 {
9343 li = l->lv_first;
9344 if (argvars[2].v_type != VAR_UNKNOWN)
9345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009346 int error = FALSE;
9347
9348 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009349 if (argvars[3].v_type != VAR_UNKNOWN)
9350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009351 idx = get_tv_number_chk(&argvars[3], &error);
9352 if (!error)
9353 {
9354 li = list_find(l, idx);
9355 if (li == NULL)
9356 EMSGN(_(e_listidx), idx);
9357 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009358 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009359 if (error)
9360 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009361 }
9362
9363 for ( ; li != NULL; li = li->li_next)
9364 if (tv_equal(&li->li_tv, &argvars[1], ic))
9365 ++n;
9366 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009367 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009368 else if (argvars[0].v_type == VAR_DICT)
9369 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009370 int todo;
9371 dict_T *d;
9372 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009373
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009374 if ((d = argvars[0].vval.v_dict) != NULL)
9375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009376 int error = FALSE;
9377
Bram Moolenaare9a41262005-01-15 22:18:47 +00009378 if (argvars[2].v_type != VAR_UNKNOWN)
9379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009380 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009381 if (argvars[3].v_type != VAR_UNKNOWN)
9382 EMSG(_(e_invarg));
9383 }
9384
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009385 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009386 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009387 {
9388 if (!HASHITEM_EMPTY(hi))
9389 {
9390 --todo;
9391 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9392 ++n;
9393 }
9394 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009395 }
9396 }
9397 else
9398 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009399 rettv->vval.v_number = n;
9400}
9401
9402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9404 *
9405 * Checks the existence of a cscope connection.
9406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009409 typval_T *argvars UNUSED;
9410 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411{
9412#ifdef FEAT_CSCOPE
9413 int num = 0;
9414 char_u *dbpath = NULL;
9415 char_u *prepend = NULL;
9416 char_u buf[NUMBUFLEN];
9417
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009418 if (argvars[0].v_type != VAR_UNKNOWN
9419 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421 num = (int)get_tv_number(&argvars[0]);
9422 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009423 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 }
9426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428#endif
9429}
9430
9431/*
9432 * "cursor(lnum, col)" function
9433 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009434 * Moves the cursor to the specified line and column.
9435 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 typval_T *argvars;
9440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441{
9442 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009443#ifdef FEAT_VIRTUALEDIT
9444 long coladd = 0;
9445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009447 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009448 if (argvars[1].v_type == VAR_UNKNOWN)
9449 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009450 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009451
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009452 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009453 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009454 line = pos.lnum;
9455 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009456#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009457 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009458#endif
9459 }
9460 else
9461 {
9462 line = get_tv_lnum(argvars);
9463 col = get_tv_number_chk(&argvars[1], NULL);
9464#ifdef FEAT_VIRTUALEDIT
9465 if (argvars[2].v_type != VAR_UNKNOWN)
9466 coladd = get_tv_number_chk(&argvars[2], NULL);
9467#endif
9468 }
9469 if (line < 0 || col < 0
9470#ifdef FEAT_VIRTUALEDIT
9471 || coladd < 0
9472#endif
9473 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 if (line > 0)
9476 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 if (col > 0)
9478 curwin->w_cursor.col = col - 1;
9479#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009480 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481#endif
9482
9483 /* Make sure the cursor is in a valid position. */
9484 check_cursor();
9485#ifdef FEAT_MBYTE
9486 /* Correct cursor for multi-byte character. */
9487 if (has_mbyte)
9488 mb_adjust_cursor();
9489#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009490
9491 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009492 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493}
9494
9495/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009496 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 */
9498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009500 typval_T *argvars;
9501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009503 int noref = 0;
9504
9505 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009506 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009507 if (noref < 0 || noref > 1)
9508 EMSG(_(e_invarg));
9509 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009510 {
9511 current_copyID += COPYID_INC;
9512 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514}
9515
9516/*
9517 * "delete()" function
9518 */
9519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009520f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009521 typval_T *argvars;
9522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
9524 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528}
9529
9530/*
9531 * "did_filetype()" function
9532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009535 typval_T *argvars UNUSED;
9536 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537{
9538#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540#endif
9541}
9542
9543/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009544 * "diff_filler()" function
9545 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009548 typval_T *argvars UNUSED;
9549 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009550{
9551#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009553#endif
9554}
9555
9556/*
9557 * "diff_hlID()" function
9558 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009561 typval_T *argvars UNUSED;
9562 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009563{
9564#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009566 static linenr_T prev_lnum = 0;
9567 static int changedtick = 0;
9568 static int fnum = 0;
9569 static int change_start = 0;
9570 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009571 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009572 int filler_lines;
9573 int col;
9574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575 if (lnum < 0) /* ignore type error in {lnum} arg */
9576 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009577 if (lnum != prev_lnum
9578 || changedtick != curbuf->b_changedtick
9579 || fnum != curbuf->b_fnum)
9580 {
9581 /* New line, buffer, change: need to get the values. */
9582 filler_lines = diff_check(curwin, lnum);
9583 if (filler_lines < 0)
9584 {
9585 if (filler_lines == -1)
9586 {
9587 change_start = MAXCOL;
9588 change_end = -1;
9589 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9590 hlID = HLF_ADD; /* added line */
9591 else
9592 hlID = HLF_CHD; /* changed line */
9593 }
9594 else
9595 hlID = HLF_ADD; /* added line */
9596 }
9597 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009598 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009599 prev_lnum = lnum;
9600 changedtick = curbuf->b_changedtick;
9601 fnum = curbuf->b_fnum;
9602 }
9603
9604 if (hlID == HLF_CHD || hlID == HLF_TXD)
9605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009606 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009607 if (col >= change_start && col <= change_end)
9608 hlID = HLF_TXD; /* changed text */
9609 else
9610 hlID = HLF_CHD; /* changed line */
9611 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009612 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009613#endif
9614}
9615
9616/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009617 * "empty({expr})" function
9618 */
9619 static void
9620f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 typval_T *argvars;
9622 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009623{
9624 int n;
9625
9626 switch (argvars[0].v_type)
9627 {
9628 case VAR_STRING:
9629 case VAR_FUNC:
9630 n = argvars[0].vval.v_string == NULL
9631 || *argvars[0].vval.v_string == NUL;
9632 break;
9633 case VAR_NUMBER:
9634 n = argvars[0].vval.v_number == 0;
9635 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009636#ifdef FEAT_FLOAT
9637 case VAR_FLOAT:
9638 n = argvars[0].vval.v_float == 0.0;
9639 break;
9640#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009641 case VAR_LIST:
9642 n = argvars[0].vval.v_list == NULL
9643 || argvars[0].vval.v_list->lv_first == NULL;
9644 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 case VAR_DICT:
9646 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009647 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009648 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009649 default:
9650 EMSG2(_(e_intern2), "f_empty()");
9651 n = 0;
9652 }
9653
9654 rettv->vval.v_number = n;
9655}
9656
9657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 * "escape({string}, {chars})" function
9659 */
9660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009662 typval_T *argvars;
9663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665 char_u buf[NUMBUFLEN];
9666
Bram Moolenaar758711c2005-02-02 23:11:38 +00009667 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9668 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670}
9671
9672/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009673 * "eval()" function
9674 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009675 static void
9676f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009677 typval_T *argvars;
9678 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009679{
9680 char_u *s;
9681
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009682 s = get_tv_string_chk(&argvars[0]);
9683 if (s != NULL)
9684 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009685
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009686 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9687 {
9688 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009689 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009690 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009691 else if (*s != NUL)
9692 EMSG(_(e_trailing));
9693}
9694
9695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 * "eventhandler()" function
9697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009700 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704}
9705
9706/*
9707 * "executable()" function
9708 */
9709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009711 typval_T *argvars;
9712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715}
9716
9717/*
9718 * "exists()" function
9719 */
9720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009722 typval_T *argvars;
9723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725 char_u *p;
9726 char_u *name;
9727 int n = FALSE;
9728 int len = 0;
9729
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009730 no_autoload = TRUE;
9731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 if (*p == '$') /* environment variable */
9734 {
9735 /* first try "normal" environment variables (fast) */
9736 if (mch_getenv(p + 1) != NULL)
9737 n = TRUE;
9738 else
9739 {
9740 /* try expanding things like $VIM and ${HOME} */
9741 p = expand_env_save(p);
9742 if (p != NULL && *p != '$')
9743 n = TRUE;
9744 vim_free(p);
9745 }
9746 }
9747 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009750 if (*skipwhite(p) != NUL)
9751 n = FALSE; /* trailing garbage */
9752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 else if (*p == '*') /* internal or user defined function */
9754 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009755 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 }
9757 else if (*p == ':')
9758 {
9759 n = cmd_exists(p + 1);
9760 }
9761 else if (*p == '#')
9762 {
9763#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009764 if (p[1] == '#')
9765 n = autocmd_supported(p + 2);
9766 else
9767 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768#endif
9769 }
9770 else /* internal variable */
9771 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009772 char_u *tofree;
9773 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009775 /* get_name_len() takes care of expanding curly braces */
9776 name = p;
9777 len = get_name_len(&p, &tofree, TRUE, FALSE);
9778 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009780 if (tofree != NULL)
9781 name = tofree;
9782 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9783 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009785 /* handle d.key, l[idx], f(expr) */
9786 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9787 if (n)
9788 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 }
9790 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009791 if (*p != NUL)
9792 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009794 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 }
9796
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009798
9799 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800}
9801
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009802#ifdef FEAT_FLOAT
9803/*
9804 * "exp()" function
9805 */
9806 static void
9807f_exp(argvars, rettv)
9808 typval_T *argvars;
9809 typval_T *rettv;
9810{
9811 float_T f;
9812
9813 rettv->v_type = VAR_FLOAT;
9814 if (get_float_arg(argvars, &f) == OK)
9815 rettv->vval.v_float = exp(f);
9816 else
9817 rettv->vval.v_float = 0.0;
9818}
9819#endif
9820
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821/*
9822 * "expand()" function
9823 */
9824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009826 typval_T *argvars;
9827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828{
9829 char_u *s;
9830 int len;
9831 char_u *errormsg;
9832 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9833 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009834 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 rettv->v_type = VAR_STRING;
9837 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 if (*s == '%' || *s == '#' || *s == '<')
9839 {
9840 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009841 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 --emsg_off;
9843 }
9844 else
9845 {
9846 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009847 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009848 if (argvars[1].v_type != VAR_UNKNOWN
9849 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009851 if (!error)
9852 {
9853 ExpandInit(&xpc);
9854 xpc.xp_context = EXPAND_FILES;
9855 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 }
9857 else
9858 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 }
9860}
9861
9862/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009863 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009864 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009865 */
9866 static void
9867f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009868 typval_T *argvars;
9869 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009870{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009871 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009872 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009873 list_T *l1, *l2;
9874 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009875 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009876 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009877
Bram Moolenaare9a41262005-01-15 22:18:47 +00009878 l1 = argvars[0].vval.v_list;
9879 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009880 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9881 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009882 {
9883 if (argvars[2].v_type != VAR_UNKNOWN)
9884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009885 before = get_tv_number_chk(&argvars[2], &error);
9886 if (error)
9887 return; /* type error; errmsg already given */
9888
Bram Moolenaar758711c2005-02-02 23:11:38 +00009889 if (before == l1->lv_len)
9890 item = NULL;
9891 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009892 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009893 item = list_find(l1, before);
9894 if (item == NULL)
9895 {
9896 EMSGN(_(e_listidx), before);
9897 return;
9898 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009899 }
9900 }
9901 else
9902 item = NULL;
9903 list_extend(l1, l2, item);
9904
Bram Moolenaare9a41262005-01-15 22:18:47 +00009905 copy_tv(&argvars[0], rettv);
9906 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009907 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009908 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9909 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009910 dict_T *d1, *d2;
9911 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009912 char_u *action;
9913 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009914 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009915 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009916
9917 d1 = argvars[0].vval.v_dict;
9918 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009919 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9920 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 {
9922 /* Check the third argument. */
9923 if (argvars[2].v_type != VAR_UNKNOWN)
9924 {
9925 static char *(av[]) = {"keep", "force", "error"};
9926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009927 action = get_tv_string_chk(&argvars[2]);
9928 if (action == NULL)
9929 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009930 for (i = 0; i < 3; ++i)
9931 if (STRCMP(action, av[i]) == 0)
9932 break;
9933 if (i == 3)
9934 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009935 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009936 return;
9937 }
9938 }
9939 else
9940 action = (char_u *)"force";
9941
9942 /* Go over all entries in the second dict and add them to the
9943 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009944 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009945 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009946 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009947 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009948 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009949 --todo;
9950 di1 = dict_find(d1, hi2->hi_key, -1);
9951 if (di1 == NULL)
9952 {
9953 di1 = dictitem_copy(HI2DI(hi2));
9954 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9955 dictitem_free(di1);
9956 }
9957 else if (*action == 'e')
9958 {
9959 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9960 break;
9961 }
9962 else if (*action == 'f')
9963 {
9964 clear_tv(&di1->di_tv);
9965 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9966 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009967 }
9968 }
9969
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 copy_tv(&argvars[0], rettv);
9971 }
9972 }
9973 else
9974 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009975}
9976
9977/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009978 * "feedkeys()" function
9979 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009980 static void
9981f_feedkeys(argvars, rettv)
9982 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009983 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009984{
9985 int remap = TRUE;
9986 char_u *keys, *flags;
9987 char_u nbuf[NUMBUFLEN];
9988 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009989 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009990
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009991 /* This is not allowed in the sandbox. If the commands would still be
9992 * executed in the sandbox it would be OK, but it probably happens later,
9993 * when "sandbox" is no longer set. */
9994 if (check_secure())
9995 return;
9996
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009997 keys = get_tv_string(&argvars[0]);
9998 if (*keys != NUL)
9999 {
10000 if (argvars[1].v_type != VAR_UNKNOWN)
10001 {
10002 flags = get_tv_string_buf(&argvars[1], nbuf);
10003 for ( ; *flags != NUL; ++flags)
10004 {
10005 switch (*flags)
10006 {
10007 case 'n': remap = FALSE; break;
10008 case 'm': remap = TRUE; break;
10009 case 't': typed = TRUE; break;
10010 }
10011 }
10012 }
10013
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010014 /* Need to escape K_SPECIAL and CSI before putting the string in the
10015 * typeahead buffer. */
10016 keys_esc = vim_strsave_escape_csi(keys);
10017 if (keys_esc != NULL)
10018 {
10019 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010020 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010021 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010022 if (vgetc_busy)
10023 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010024 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010025 }
10026}
10027
10028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 * "filereadable()" function
10030 */
10031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010033 typval_T *argvars;
10034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010036 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 char_u *p;
10038 int n;
10039
Bram Moolenaarc236c162008-07-13 17:41:49 +000010040#ifndef O_NONBLOCK
10041# define O_NONBLOCK 0
10042#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010044 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10045 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 {
10047 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010048 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 }
10050 else
10051 n = FALSE;
10052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054}
10055
10056/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010057 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 * rights to write into.
10059 */
10060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010065 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066}
10067
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010068static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010069
10070 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010071findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010072 typval_T *argvars;
10073 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010074 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010075{
10076#ifdef FEAT_SEARCHPATH
10077 char_u *fname;
10078 char_u *fresult = NULL;
10079 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10080 char_u *p;
10081 char_u pathbuf[NUMBUFLEN];
10082 int count = 1;
10083 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010084 int error = FALSE;
10085#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010086
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010087 rettv->vval.v_string = NULL;
10088 rettv->v_type = VAR_STRING;
10089
10090#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010092
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010093 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010095 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10096 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010097 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 else
10099 {
10100 if (*p != NUL)
10101 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010103 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010104 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010105 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010106 }
10107
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010108 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10109 error = TRUE;
10110
10111 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010113 do
10114 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010115 if (rettv->v_type == VAR_STRING)
10116 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010117 fresult = find_file_in_path_option(first ? fname : NULL,
10118 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010119 0, first, path,
10120 find_what,
10121 curbuf->b_ffname,
10122 find_what == FINDFILE_DIR
10123 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010124 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010125
10126 if (fresult != NULL && rettv->v_type == VAR_LIST)
10127 list_append_string(rettv->vval.v_list, fresult, -1);
10128
10129 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010130 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010131
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010132 if (rettv->v_type == VAR_STRING)
10133 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010134#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010135}
10136
Bram Moolenaar33570922005-01-25 22:26:29 +000010137static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10138static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010139
10140/*
10141 * Implementation of map() and filter().
10142 */
10143 static void
10144filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010145 typval_T *argvars;
10146 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010147 int map;
10148{
10149 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010150 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010151 listitem_T *li, *nli;
10152 list_T *l = NULL;
10153 dictitem_T *di;
10154 hashtab_T *ht;
10155 hashitem_T *hi;
10156 dict_T *d = NULL;
10157 typval_T save_val;
10158 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010159 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010160 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010161 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010162 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010163 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010164
Bram Moolenaare9a41262005-01-15 22:18:47 +000010165 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010166 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010167 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010168 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010169 return;
10170 }
10171 else if (argvars[0].v_type == VAR_DICT)
10172 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010173 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010174 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010175 return;
10176 }
10177 else
10178 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010179 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010180 return;
10181 }
10182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010183 expr = get_tv_string_buf_chk(&argvars[1], buf);
10184 /* On type errors, the preceding call has already displayed an error
10185 * message. Avoid a misleading error message for an empty string that
10186 * was not passed as argument. */
10187 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 prepare_vimvar(VV_VAL, &save_val);
10190 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010191
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010192 /* We reset "did_emsg" to be able to detect whether an error
10193 * occurred during evaluation of the expression. */
10194 save_did_emsg = did_emsg;
10195 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010196
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010197 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010200 vimvars[VV_KEY].vv_type = VAR_STRING;
10201
10202 ht = &d->dv_hashtab;
10203 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010204 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010205 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010207 if (!HASHITEM_EMPTY(hi))
10208 {
10209 --todo;
10210 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010211 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 break;
10213 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010214 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010215 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010216 break;
10217 if (!map && rem)
10218 dictitem_remove(d, di);
10219 clear_tv(&vimvars[VV_KEY].vv_tv);
10220 }
10221 }
10222 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 }
10224 else
10225 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010226 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10227
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 for (li = l->lv_first; li != NULL; li = nli)
10229 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010230 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010231 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010232 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010233 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010234 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010235 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010236 break;
10237 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010238 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010239 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010240 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010241 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010242
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010243 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010244 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010245
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010246 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010247 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248
10249 copy_tv(&argvars[0], rettv);
10250}
10251
10252 static int
10253filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010254 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010255 char_u *expr;
10256 int map;
10257 int *remp;
10258{
Bram Moolenaar33570922005-01-25 22:26:29 +000010259 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010260 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010261 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010262
Bram Moolenaar33570922005-01-25 22:26:29 +000010263 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010264 s = expr;
10265 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010266 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010267 if (*s != NUL) /* check for trailing chars after expr */
10268 {
10269 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010270 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271 }
10272 if (map)
10273 {
10274 /* map(): replace the list item value */
10275 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010276 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 *tv = rettv;
10278 }
10279 else
10280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010281 int error = FALSE;
10282
Bram Moolenaare9a41262005-01-15 22:18:47 +000010283 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010284 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010285 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010286 /* On type error, nothing has been removed; return FAIL to stop the
10287 * loop. The error message was given by get_tv_number_chk(). */
10288 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010289 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010290 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010291 retval = OK;
10292theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010294 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010295}
10296
10297/*
10298 * "filter()" function
10299 */
10300 static void
10301f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010302 typval_T *argvars;
10303 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010304{
10305 filter_map(argvars, rettv, FALSE);
10306}
10307
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010308/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010309 * "finddir({fname}[, {path}[, {count}]])" function
10310 */
10311 static void
10312f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010313 typval_T *argvars;
10314 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010315{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010316 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010317}
10318
10319/*
10320 * "findfile({fname}[, {path}[, {count}]])" function
10321 */
10322 static void
10323f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010324 typval_T *argvars;
10325 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010326{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010327 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010328}
10329
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010330#ifdef FEAT_FLOAT
10331/*
10332 * "float2nr({float})" function
10333 */
10334 static void
10335f_float2nr(argvars, rettv)
10336 typval_T *argvars;
10337 typval_T *rettv;
10338{
10339 float_T f;
10340
10341 if (get_float_arg(argvars, &f) == OK)
10342 {
10343 if (f < -0x7fffffff)
10344 rettv->vval.v_number = -0x7fffffff;
10345 else if (f > 0x7fffffff)
10346 rettv->vval.v_number = 0x7fffffff;
10347 else
10348 rettv->vval.v_number = (varnumber_T)f;
10349 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010350}
10351
10352/*
10353 * "floor({float})" function
10354 */
10355 static void
10356f_floor(argvars, rettv)
10357 typval_T *argvars;
10358 typval_T *rettv;
10359{
10360 float_T f;
10361
10362 rettv->v_type = VAR_FLOAT;
10363 if (get_float_arg(argvars, &f) == OK)
10364 rettv->vval.v_float = floor(f);
10365 else
10366 rettv->vval.v_float = 0.0;
10367}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010368
10369/*
10370 * "fmod()" function
10371 */
10372 static void
10373f_fmod(argvars, rettv)
10374 typval_T *argvars;
10375 typval_T *rettv;
10376{
10377 float_T fx, fy;
10378
10379 rettv->v_type = VAR_FLOAT;
10380 if (get_float_arg(argvars, &fx) == OK
10381 && get_float_arg(&argvars[1], &fy) == OK)
10382 rettv->vval.v_float = fmod(fx, fy);
10383 else
10384 rettv->vval.v_float = 0.0;
10385}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010386#endif
10387
Bram Moolenaar0d660222005-01-07 21:51:51 +000010388/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010389 * "fnameescape({string})" function
10390 */
10391 static void
10392f_fnameescape(argvars, rettv)
10393 typval_T *argvars;
10394 typval_T *rettv;
10395{
10396 rettv->vval.v_string = vim_strsave_fnameescape(
10397 get_tv_string(&argvars[0]), FALSE);
10398 rettv->v_type = VAR_STRING;
10399}
10400
10401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 * "fnamemodify({fname}, {mods})" function
10403 */
10404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 typval_T *argvars;
10407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
10409 char_u *fname;
10410 char_u *mods;
10411 int usedlen = 0;
10412 int len;
10413 char_u *fbuf = NULL;
10414 char_u buf[NUMBUFLEN];
10415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010416 fname = get_tv_string_chk(&argvars[0]);
10417 mods = get_tv_string_buf_chk(&argvars[1], buf);
10418 if (fname == NULL || mods == NULL)
10419 fname = NULL;
10420 else
10421 {
10422 len = (int)STRLEN(fname);
10423 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010426 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010428 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010430 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 vim_free(fbuf);
10432}
10433
Bram Moolenaar33570922005-01-25 22:26:29 +000010434static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435
10436/*
10437 * "foldclosed()" function
10438 */
10439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010440foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010441 typval_T *argvars;
10442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 int end;
10444{
10445#ifdef FEAT_FOLDING
10446 linenr_T lnum;
10447 linenr_T first, last;
10448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010449 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10451 {
10452 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10453 {
10454 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 return;
10459 }
10460 }
10461#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463}
10464
10465/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010466 * "foldclosed()" function
10467 */
10468 static void
10469f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010470 typval_T *argvars;
10471 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010472{
10473 foldclosed_both(argvars, rettv, FALSE);
10474}
10475
10476/*
10477 * "foldclosedend()" function
10478 */
10479 static void
10480f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010481 typval_T *argvars;
10482 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010483{
10484 foldclosed_both(argvars, rettv, TRUE);
10485}
10486
10487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 * "foldlevel()" function
10489 */
10490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010491f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010492 typval_T *argvars;
10493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494{
10495#ifdef FEAT_FOLDING
10496 linenr_T lnum;
10497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010498 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502}
10503
10504/*
10505 * "foldtext()" function
10506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010509 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511{
10512#ifdef FEAT_FOLDING
10513 linenr_T lnum;
10514 char_u *s;
10515 char_u *r;
10516 int len;
10517 char *txt;
10518#endif
10519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520 rettv->v_type = VAR_STRING;
10521 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010523 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10524 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10525 <= curbuf->b_ml.ml_line_count
10526 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 {
10528 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010529 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10530 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 {
10532 if (!linewhite(lnum))
10533 break;
10534 ++lnum;
10535 }
10536
10537 /* Find interesting text in this line. */
10538 s = skipwhite(ml_get(lnum));
10539 /* skip C comment-start */
10540 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010543 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010544 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010545 {
10546 s = skipwhite(ml_get(lnum + 1));
10547 if (*s == '*')
10548 s = skipwhite(s + 1);
10549 }
10550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 txt = _("+-%s%3ld lines: ");
10552 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010553 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 + 20 /* for %3ld */
10555 + STRLEN(s))); /* concatenated */
10556 if (r != NULL)
10557 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010558 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10559 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10560 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 len = (int)STRLEN(r);
10562 STRCAT(r, s);
10563 /* remove 'foldmarker' and 'commentstring' */
10564 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 }
10567 }
10568#endif
10569}
10570
10571/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010572 * "foldtextresult(lnum)" function
10573 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010576 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010577 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010578{
10579#ifdef FEAT_FOLDING
10580 linenr_T lnum;
10581 char_u *text;
10582 char_u buf[51];
10583 foldinfo_T foldinfo;
10584 int fold_count;
10585#endif
10586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010587 rettv->v_type = VAR_STRING;
10588 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010589#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010590 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010591 /* treat illegal types and illegal string values for {lnum} the same */
10592 if (lnum < 0)
10593 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010594 fold_count = foldedCount(curwin, lnum, &foldinfo);
10595 if (fold_count > 0)
10596 {
10597 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10598 &foldinfo, buf);
10599 if (text == buf)
10600 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010601 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010602 }
10603#endif
10604}
10605
10606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 * "foreground()" function
10608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010610f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010611 typval_T *argvars UNUSED;
10612 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614#ifdef FEAT_GUI
10615 if (gui.in_use)
10616 gui_mch_set_foreground();
10617#else
10618# ifdef WIN32
10619 win32_set_foreground();
10620# endif
10621#endif
10622}
10623
10624/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010625 * "function()" function
10626 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010628f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010629 typval_T *argvars;
10630 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010631{
10632 char_u *s;
10633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010635 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010636 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010637 /* Don't check an autoload name for existence here. */
10638 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010639 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010640 else
10641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010642 rettv->vval.v_string = vim_strsave(s);
10643 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010644 }
10645}
10646
10647/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010648 * "garbagecollect()" function
10649 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010650 static void
10651f_garbagecollect(argvars, rettv)
10652 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010653 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010654{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010655 /* This is postponed until we are back at the toplevel, because we may be
10656 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10657 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010658
10659 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10660 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010661}
10662
10663/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010664 * "get()" function
10665 */
10666 static void
10667f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010668 typval_T *argvars;
10669 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010670{
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 listitem_T *li;
10672 list_T *l;
10673 dictitem_T *di;
10674 dict_T *d;
10675 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010676
Bram Moolenaare9a41262005-01-15 22:18:47 +000010677 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010678 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010679 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010681 int error = FALSE;
10682
10683 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10684 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010685 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010686 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010687 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010688 else if (argvars[0].v_type == VAR_DICT)
10689 {
10690 if ((d = argvars[0].vval.v_dict) != NULL)
10691 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010692 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010693 if (di != NULL)
10694 tv = &di->di_tv;
10695 }
10696 }
10697 else
10698 EMSG2(_(e_listdictarg), "get()");
10699
10700 if (tv == NULL)
10701 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010702 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010703 copy_tv(&argvars[2], rettv);
10704 }
10705 else
10706 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010707}
10708
Bram Moolenaar342337a2005-07-21 21:11:17 +000010709static 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 +000010710
10711/*
10712 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010713 * Return a range (from start to end) of lines in rettv from the specified
10714 * buffer.
10715 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010716 */
10717 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010718get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010719 buf_T *buf;
10720 linenr_T start;
10721 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010722 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010723 typval_T *rettv;
10724{
10725 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010726
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010727 if (retlist && rettv_list_alloc(rettv) == FAIL)
10728 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010729
10730 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10731 return;
10732
10733 if (!retlist)
10734 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010735 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10736 p = ml_get_buf(buf, start, FALSE);
10737 else
10738 p = (char_u *)"";
10739
10740 rettv->v_type = VAR_STRING;
10741 rettv->vval.v_string = vim_strsave(p);
10742 }
10743 else
10744 {
10745 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010746 return;
10747
10748 if (start < 1)
10749 start = 1;
10750 if (end > buf->b_ml.ml_line_count)
10751 end = buf->b_ml.ml_line_count;
10752 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010753 if (list_append_string(rettv->vval.v_list,
10754 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010755 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010756 }
10757}
10758
10759/*
10760 * "getbufline()" function
10761 */
10762 static void
10763f_getbufline(argvars, rettv)
10764 typval_T *argvars;
10765 typval_T *rettv;
10766{
10767 linenr_T lnum;
10768 linenr_T end;
10769 buf_T *buf;
10770
10771 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10772 ++emsg_off;
10773 buf = get_buf_tv(&argvars[0]);
10774 --emsg_off;
10775
Bram Moolenaar661b1822005-07-28 22:36:45 +000010776 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010777 if (argvars[2].v_type == VAR_UNKNOWN)
10778 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010779 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010780 end = get_tv_lnum_buf(&argvars[2], buf);
10781
Bram Moolenaar342337a2005-07-21 21:11:17 +000010782 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010783}
10784
Bram Moolenaar0d660222005-01-07 21:51:51 +000010785/*
10786 * "getbufvar()" function
10787 */
10788 static void
10789f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010790 typval_T *argvars;
10791 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010792{
10793 buf_T *buf;
10794 buf_T *save_curbuf;
10795 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010796 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10799 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010800 ++emsg_off;
10801 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010802
10803 rettv->v_type = VAR_STRING;
10804 rettv->vval.v_string = NULL;
10805
10806 if (buf != NULL && varname != NULL)
10807 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010808 /* set curbuf to be our buf, temporarily */
10809 save_curbuf = curbuf;
10810 curbuf = buf;
10811
Bram Moolenaar0d660222005-01-07 21:51:51 +000010812 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010814 else
10815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010816 if (*varname == NUL)
10817 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10818 * scope prefix before the NUL byte is required by
10819 * find_var_in_ht(). */
10820 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010821 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010822 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010823 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010824 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010825 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010826
10827 /* restore previous notion of curbuf */
10828 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010829 }
10830
10831 --emsg_off;
10832}
10833
10834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 * "getchar()" function
10836 */
10837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010839 typval_T *argvars;
10840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841{
10842 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010843 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010845 /* Position the cursor. Needed after a message that ends in a space. */
10846 windgoto(msg_row, msg_col);
10847
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 ++no_mapping;
10849 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010850 for (;;)
10851 {
10852 if (argvars[0].v_type == VAR_UNKNOWN)
10853 /* getchar(): blocking wait. */
10854 n = safe_vgetc();
10855 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10856 /* getchar(1): only check if char avail */
10857 n = vpeekc();
10858 else if (error || vpeekc() == NUL)
10859 /* illegal argument or getchar(0) and no char avail: return zero */
10860 n = 0;
10861 else
10862 /* getchar(0) and char avail: return char */
10863 n = safe_vgetc();
10864 if (n == K_IGNORE)
10865 continue;
10866 break;
10867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 --no_mapping;
10869 --allow_keys;
10870
Bram Moolenaar219b8702006-11-01 14:32:36 +000010871 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10872 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10873 vimvars[VV_MOUSE_COL].vv_nr = 0;
10874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 if (IS_SPECIAL(n) || mod_mask != 0)
10877 {
10878 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10879 int i = 0;
10880
10881 /* Turn a special key into three bytes, plus modifier. */
10882 if (mod_mask != 0)
10883 {
10884 temp[i++] = K_SPECIAL;
10885 temp[i++] = KS_MODIFIER;
10886 temp[i++] = mod_mask;
10887 }
10888 if (IS_SPECIAL(n))
10889 {
10890 temp[i++] = K_SPECIAL;
10891 temp[i++] = K_SECOND(n);
10892 temp[i++] = K_THIRD(n);
10893 }
10894#ifdef FEAT_MBYTE
10895 else if (has_mbyte)
10896 i += (*mb_char2bytes)(n, temp + i);
10897#endif
10898 else
10899 temp[i++] = n;
10900 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010901 rettv->v_type = VAR_STRING;
10902 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010903
10904#ifdef FEAT_MOUSE
10905 if (n == K_LEFTMOUSE
10906 || n == K_LEFTMOUSE_NM
10907 || n == K_LEFTDRAG
10908 || n == K_LEFTRELEASE
10909 || n == K_LEFTRELEASE_NM
10910 || n == K_MIDDLEMOUSE
10911 || n == K_MIDDLEDRAG
10912 || n == K_MIDDLERELEASE
10913 || n == K_RIGHTMOUSE
10914 || n == K_RIGHTDRAG
10915 || n == K_RIGHTRELEASE
10916 || n == K_X1MOUSE
10917 || n == K_X1DRAG
10918 || n == K_X1RELEASE
10919 || n == K_X2MOUSE
10920 || n == K_X2DRAG
10921 || n == K_X2RELEASE
10922 || n == K_MOUSEDOWN
10923 || n == K_MOUSEUP)
10924 {
10925 int row = mouse_row;
10926 int col = mouse_col;
10927 win_T *win;
10928 linenr_T lnum;
10929# ifdef FEAT_WINDOWS
10930 win_T *wp;
10931# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010932 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010933
10934 if (row >= 0 && col >= 0)
10935 {
10936 /* Find the window at the mouse coordinates and compute the
10937 * text position. */
10938 win = mouse_find_win(&row, &col);
10939 (void)mouse_comp_pos(win, &row, &col, &lnum);
10940# ifdef FEAT_WINDOWS
10941 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010942 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010943# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010944 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010945 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10946 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10947 }
10948 }
10949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 }
10951}
10952
10953/*
10954 * "getcharmod()" function
10955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010958 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010961 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962}
10963
10964/*
10965 * "getcmdline()" function
10966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010968f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010969 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010972 rettv->v_type = VAR_STRING;
10973 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974}
10975
10976/*
10977 * "getcmdpos()" function
10978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010980f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010981 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985}
10986
10987/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010988 * "getcmdtype()" function
10989 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010990 static void
10991f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010992 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010993 typval_T *rettv;
10994{
10995 rettv->v_type = VAR_STRING;
10996 rettv->vval.v_string = alloc(2);
10997 if (rettv->vval.v_string != NULL)
10998 {
10999 rettv->vval.v_string[0] = get_cmdline_type();
11000 rettv->vval.v_string[1] = NUL;
11001 }
11002}
11003
11004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 * "getcwd()" function
11006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011008f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011009 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011{
11012 char_u cwd[MAXPATHL];
11013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 else
11018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011019 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011021 if (rettv->vval.v_string != NULL)
11022 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023#endif
11024 }
11025}
11026
11027/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011028 * "getfontname()" function
11029 */
11030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011031f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011032 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011033 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011034{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035 rettv->v_type = VAR_STRING;
11036 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011037#ifdef FEAT_GUI
11038 if (gui.in_use)
11039 {
11040 GuiFont font;
11041 char_u *name = NULL;
11042
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011043 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011044 {
11045 /* Get the "Normal" font. Either the name saved by
11046 * hl_set_font_name() or from the font ID. */
11047 font = gui.norm_font;
11048 name = hl_get_font_name();
11049 }
11050 else
11051 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011053 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11054 return;
11055 font = gui_mch_get_font(name, FALSE);
11056 if (font == NOFONT)
11057 return; /* Invalid font name, return empty string. */
11058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011060 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011061 gui_mch_free_font(font);
11062 }
11063#endif
11064}
11065
11066/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011067 * "getfperm({fname})" function
11068 */
11069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011071 typval_T *argvars;
11072 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011073{
11074 char_u *fname;
11075 struct stat st;
11076 char_u *perm = NULL;
11077 char_u flags[] = "rwx";
11078 int i;
11079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011080 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011082 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011083 if (mch_stat((char *)fname, &st) >= 0)
11084 {
11085 perm = vim_strsave((char_u *)"---------");
11086 if (perm != NULL)
11087 {
11088 for (i = 0; i < 9; i++)
11089 {
11090 if (st.st_mode & (1 << (8 - i)))
11091 perm[i] = flags[i % 3];
11092 }
11093 }
11094 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011096}
11097
11098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 * "getfsize({fname})" function
11100 */
11101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011102f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 typval_T *argvars;
11104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105{
11106 char_u *fname;
11107 struct stat st;
11108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011111 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112
11113 if (mch_stat((char *)fname, &st) >= 0)
11114 {
11115 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011118 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011120
11121 /* non-perfect check for overflow */
11122 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11123 rettv->vval.v_number = -2;
11124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 }
11126 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128}
11129
11130/*
11131 * "getftime({fname})" function
11132 */
11133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 typval_T *argvars;
11136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
11138 char_u *fname;
11139 struct stat st;
11140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142
11143 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147}
11148
11149/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011150 * "getftype({fname})" function
11151 */
11152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011153f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011154 typval_T *argvars;
11155 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011156{
11157 char_u *fname;
11158 struct stat st;
11159 char_u *type = NULL;
11160 char *t;
11161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011162 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011165 if (mch_lstat((char *)fname, &st) >= 0)
11166 {
11167#ifdef S_ISREG
11168 if (S_ISREG(st.st_mode))
11169 t = "file";
11170 else if (S_ISDIR(st.st_mode))
11171 t = "dir";
11172# ifdef S_ISLNK
11173 else if (S_ISLNK(st.st_mode))
11174 t = "link";
11175# endif
11176# ifdef S_ISBLK
11177 else if (S_ISBLK(st.st_mode))
11178 t = "bdev";
11179# endif
11180# ifdef S_ISCHR
11181 else if (S_ISCHR(st.st_mode))
11182 t = "cdev";
11183# endif
11184# ifdef S_ISFIFO
11185 else if (S_ISFIFO(st.st_mode))
11186 t = "fifo";
11187# endif
11188# ifdef S_ISSOCK
11189 else if (S_ISSOCK(st.st_mode))
11190 t = "fifo";
11191# endif
11192 else
11193 t = "other";
11194#else
11195# ifdef S_IFMT
11196 switch (st.st_mode & S_IFMT)
11197 {
11198 case S_IFREG: t = "file"; break;
11199 case S_IFDIR: t = "dir"; break;
11200# ifdef S_IFLNK
11201 case S_IFLNK: t = "link"; break;
11202# endif
11203# ifdef S_IFBLK
11204 case S_IFBLK: t = "bdev"; break;
11205# endif
11206# ifdef S_IFCHR
11207 case S_IFCHR: t = "cdev"; break;
11208# endif
11209# ifdef S_IFIFO
11210 case S_IFIFO: t = "fifo"; break;
11211# endif
11212# ifdef S_IFSOCK
11213 case S_IFSOCK: t = "socket"; break;
11214# endif
11215 default: t = "other";
11216 }
11217# else
11218 if (mch_isdir(fname))
11219 t = "dir";
11220 else
11221 t = "file";
11222# endif
11223#endif
11224 type = vim_strsave((char_u *)t);
11225 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011227}
11228
11229/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011230 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011231 */
11232 static void
11233f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011234 typval_T *argvars;
11235 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011236{
11237 linenr_T lnum;
11238 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011239 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011240
11241 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011242 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011243 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011244 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011245 retlist = FALSE;
11246 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011247 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011248 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011249 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011250 retlist = TRUE;
11251 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011252
Bram Moolenaar342337a2005-07-21 21:11:17 +000011253 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011254}
11255
11256/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011257 * "getmatches()" function
11258 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011259 static void
11260f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011261 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011262 typval_T *rettv;
11263{
11264#ifdef FEAT_SEARCH_EXTRA
11265 dict_T *dict;
11266 matchitem_T *cur = curwin->w_match_head;
11267
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011268 if (rettv_list_alloc(rettv) == OK)
11269 {
11270 while (cur != NULL)
11271 {
11272 dict = dict_alloc();
11273 if (dict == NULL)
11274 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011275 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11276 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11277 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11278 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11279 list_append_dict(rettv->vval.v_list, dict);
11280 cur = cur->next;
11281 }
11282 }
11283#endif
11284}
11285
11286/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011287 * "getpid()" function
11288 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011289 static void
11290f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011291 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011292 typval_T *rettv;
11293{
11294 rettv->vval.v_number = mch_get_pid();
11295}
11296
11297/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011298 * "getpos(string)" function
11299 */
11300 static void
11301f_getpos(argvars, rettv)
11302 typval_T *argvars;
11303 typval_T *rettv;
11304{
11305 pos_T *fp;
11306 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011307 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011308
11309 if (rettv_list_alloc(rettv) == OK)
11310 {
11311 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011312 fp = var2fpos(&argvars[0], TRUE, &fnum);
11313 if (fnum != -1)
11314 list_append_number(l, (varnumber_T)fnum);
11315 else
11316 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011317 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11318 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011319 list_append_number(l, (fp != NULL)
11320 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011321 : (varnumber_T)0);
11322 list_append_number(l,
11323#ifdef FEAT_VIRTUALEDIT
11324 (fp != NULL) ? (varnumber_T)fp->coladd :
11325#endif
11326 (varnumber_T)0);
11327 }
11328 else
11329 rettv->vval.v_number = FALSE;
11330}
11331
11332/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011333 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011334 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011335 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011336f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011337 typval_T *argvars UNUSED;
11338 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011339{
11340#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011341 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011342#endif
11343
Bram Moolenaar2641f772005-03-25 21:58:17 +000011344#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011345 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011346 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011347 wp = NULL;
11348 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11349 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011350 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011351 if (wp == NULL)
11352 return;
11353 }
11354
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011355 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011356 }
11357#endif
11358}
11359
11360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 * "getreg()" function
11362 */
11363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011365 typval_T *argvars;
11366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367{
11368 char_u *strregname;
11369 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011370 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011373 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011375 strregname = get_tv_string_chk(&argvars[0]);
11376 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011377 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011378 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011381 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 regname = (strregname == NULL ? '"' : *strregname);
11383 if (regname == 0)
11384 regname = '"';
11385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011387 rettv->vval.v_string = error ? NULL :
11388 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389}
11390
11391/*
11392 * "getregtype()" function
11393 */
11394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011396 typval_T *argvars;
11397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398{
11399 char_u *strregname;
11400 int regname;
11401 char_u buf[NUMBUFLEN + 2];
11402 long reglen = 0;
11403
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011404 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 {
11406 strregname = get_tv_string_chk(&argvars[0]);
11407 if (strregname == NULL) /* type error; errmsg already given */
11408 {
11409 rettv->v_type = VAR_STRING;
11410 rettv->vval.v_string = NULL;
11411 return;
11412 }
11413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 else
11415 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011416 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417
11418 regname = (strregname == NULL ? '"' : *strregname);
11419 if (regname == 0)
11420 regname = '"';
11421
11422 buf[0] = NUL;
11423 buf[1] = NUL;
11424 switch (get_reg_type(regname, &reglen))
11425 {
11426 case MLINE: buf[0] = 'V'; break;
11427 case MCHAR: buf[0] = 'v'; break;
11428#ifdef FEAT_VISUAL
11429 case MBLOCK:
11430 buf[0] = Ctrl_V;
11431 sprintf((char *)buf + 1, "%ld", reglen + 1);
11432 break;
11433#endif
11434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 rettv->v_type = VAR_STRING;
11436 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437}
11438
11439/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011440 * "gettabvar()" function
11441 */
11442 static void
11443f_gettabvar(argvars, rettv)
11444 typval_T *argvars;
11445 typval_T *rettv;
11446{
11447 tabpage_T *tp;
11448 dictitem_T *v;
11449 char_u *varname;
11450
11451 rettv->v_type = VAR_STRING;
11452 rettv->vval.v_string = NULL;
11453
11454 varname = get_tv_string_chk(&argvars[1]);
11455 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11456 if (tp != NULL && varname != NULL)
11457 {
11458 /* look up the variable */
11459 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11460 if (v != NULL)
11461 copy_tv(&v->di_tv, rettv);
11462 }
11463}
11464
11465/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011466 * "gettabwinvar()" function
11467 */
11468 static void
11469f_gettabwinvar(argvars, rettv)
11470 typval_T *argvars;
11471 typval_T *rettv;
11472{
11473 getwinvar(argvars, rettv, 1);
11474}
11475
11476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 * "getwinposx()" function
11478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011481 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485#ifdef FEAT_GUI
11486 if (gui.in_use)
11487 {
11488 int x, y;
11489
11490 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 }
11493#endif
11494}
11495
11496/*
11497 * "getwinposy()" function
11498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011501 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505#ifdef FEAT_GUI
11506 if (gui.in_use)
11507 {
11508 int x, y;
11509
11510 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 }
11513#endif
11514}
11515
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011516/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011517 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011518 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011519 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011520find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011521 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011522 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011523{
11524#ifdef FEAT_WINDOWS
11525 win_T *wp;
11526#endif
11527 int nr;
11528
11529 nr = get_tv_number_chk(vp, NULL);
11530
11531#ifdef FEAT_WINDOWS
11532 if (nr < 0)
11533 return NULL;
11534 if (nr == 0)
11535 return curwin;
11536
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011537 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11538 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011539 if (--nr <= 0)
11540 break;
11541 return wp;
11542#else
11543 if (nr == 0 || nr == 1)
11544 return curwin;
11545 return NULL;
11546#endif
11547}
11548
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549/*
11550 * "getwinvar()" function
11551 */
11552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011554 typval_T *argvars;
11555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011557 getwinvar(argvars, rettv, 0);
11558}
11559
11560/*
11561 * getwinvar() and gettabwinvar()
11562 */
11563 static void
11564getwinvar(argvars, rettv, off)
11565 typval_T *argvars;
11566 typval_T *rettv;
11567 int off; /* 1 for gettabwinvar() */
11568{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 win_T *win, *oldcurwin;
11570 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011572 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011574#ifdef FEAT_WINDOWS
11575 if (off == 1)
11576 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11577 else
11578 tp = curtab;
11579#endif
11580 win = find_win_by_nr(&argvars[off], tp);
11581 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011582 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 rettv->v_type = VAR_STRING;
11585 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586
11587 if (win != NULL && varname != NULL)
11588 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011589 /* Set curwin to be our win, temporarily. Also set curbuf, so
11590 * that we can get buffer-local options. */
11591 oldcurwin = curwin;
11592 curwin = win;
11593 curbuf = win->w_buffer;
11594
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 else
11598 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011599 if (*varname == NUL)
11600 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11601 * scope prefix before the NUL byte is required by
11602 * find_var_in_ht(). */
11603 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011605 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011607 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011609
11610 /* restore previous notion of curwin */
11611 curwin = oldcurwin;
11612 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 }
11614
11615 --emsg_off;
11616}
11617
11618/*
11619 * "glob()" function
11620 */
11621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011623 typval_T *argvars;
11624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011626 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011628 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011630 /* When the optional second argument is non-zero, don't remove matches
11631 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11632 if (argvars[1].v_type != VAR_UNKNOWN
11633 && get_tv_number_chk(&argvars[1], &error))
11634 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011636 if (!error)
11637 {
11638 ExpandInit(&xpc);
11639 xpc.xp_context = EXPAND_FILES;
11640 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11641 NULL, flags, WILD_ALL);
11642 }
11643 else
11644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645}
11646
11647/*
11648 * "globpath()" function
11649 */
11650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011652 typval_T *argvars;
11653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011655 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011657 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011658 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011660 /* When the optional second argument is non-zero, don't remove matches
11661 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11662 if (argvars[2].v_type != VAR_UNKNOWN
11663 && get_tv_number_chk(&argvars[2], &error))
11664 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011666 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 rettv->vval.v_string = NULL;
11668 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011669 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11670 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671}
11672
11673/*
11674 * "has()" function
11675 */
11676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011677f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011678 typval_T *argvars;
11679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680{
11681 int i;
11682 char_u *name;
11683 int n = FALSE;
11684 static char *(has_list[]) =
11685 {
11686#ifdef AMIGA
11687 "amiga",
11688# ifdef FEAT_ARP
11689 "arp",
11690# endif
11691#endif
11692#ifdef __BEOS__
11693 "beos",
11694#endif
11695#ifdef MSDOS
11696# ifdef DJGPP
11697 "dos32",
11698# else
11699 "dos16",
11700# endif
11701#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011702#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 "mac",
11704#endif
11705#if defined(MACOS_X_UNIX)
11706 "macunix",
11707#endif
11708#ifdef OS2
11709 "os2",
11710#endif
11711#ifdef __QNX__
11712 "qnx",
11713#endif
11714#ifdef RISCOS
11715 "riscos",
11716#endif
11717#ifdef UNIX
11718 "unix",
11719#endif
11720#ifdef VMS
11721 "vms",
11722#endif
11723#ifdef WIN16
11724 "win16",
11725#endif
11726#ifdef WIN32
11727 "win32",
11728#endif
11729#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11730 "win32unix",
11731#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011732#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 "win64",
11734#endif
11735#ifdef EBCDIC
11736 "ebcdic",
11737#endif
11738#ifndef CASE_INSENSITIVE_FILENAME
11739 "fname_case",
11740#endif
11741#ifdef FEAT_ARABIC
11742 "arabic",
11743#endif
11744#ifdef FEAT_AUTOCMD
11745 "autocmd",
11746#endif
11747#ifdef FEAT_BEVAL
11748 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011749# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11750 "balloon_multiline",
11751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752#endif
11753#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11754 "builtin_terms",
11755# ifdef ALL_BUILTIN_TCAPS
11756 "all_builtin_terms",
11757# endif
11758#endif
11759#ifdef FEAT_BYTEOFF
11760 "byte_offset",
11761#endif
11762#ifdef FEAT_CINDENT
11763 "cindent",
11764#endif
11765#ifdef FEAT_CLIENTSERVER
11766 "clientserver",
11767#endif
11768#ifdef FEAT_CLIPBOARD
11769 "clipboard",
11770#endif
11771#ifdef FEAT_CMDL_COMPL
11772 "cmdline_compl",
11773#endif
11774#ifdef FEAT_CMDHIST
11775 "cmdline_hist",
11776#endif
11777#ifdef FEAT_COMMENTS
11778 "comments",
11779#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011780#ifdef FEAT_CONCEAL
11781 "conceal",
11782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783#ifdef FEAT_CRYPT
11784 "cryptv",
11785#endif
11786#ifdef FEAT_CSCOPE
11787 "cscope",
11788#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011789#ifdef FEAT_CURSORBIND
11790 "cursorbind",
11791#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011792#ifdef CURSOR_SHAPE
11793 "cursorshape",
11794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795#ifdef DEBUG
11796 "debug",
11797#endif
11798#ifdef FEAT_CON_DIALOG
11799 "dialog_con",
11800#endif
11801#ifdef FEAT_GUI_DIALOG
11802 "dialog_gui",
11803#endif
11804#ifdef FEAT_DIFF
11805 "diff",
11806#endif
11807#ifdef FEAT_DIGRAPHS
11808 "digraphs",
11809#endif
11810#ifdef FEAT_DND
11811 "dnd",
11812#endif
11813#ifdef FEAT_EMACS_TAGS
11814 "emacs_tags",
11815#endif
11816 "eval", /* always present, of course! */
11817#ifdef FEAT_EX_EXTRA
11818 "ex_extra",
11819#endif
11820#ifdef FEAT_SEARCH_EXTRA
11821 "extra_search",
11822#endif
11823#ifdef FEAT_FKMAP
11824 "farsi",
11825#endif
11826#ifdef FEAT_SEARCHPATH
11827 "file_in_path",
11828#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011829#if defined(UNIX) && !defined(USE_SYSTEM)
11830 "filterpipe",
11831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832#ifdef FEAT_FIND_ID
11833 "find_in_path",
11834#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011835#ifdef FEAT_FLOAT
11836 "float",
11837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838#ifdef FEAT_FOLDING
11839 "folding",
11840#endif
11841#ifdef FEAT_FOOTER
11842 "footer",
11843#endif
11844#if !defined(USE_SYSTEM) && defined(UNIX)
11845 "fork",
11846#endif
11847#ifdef FEAT_GETTEXT
11848 "gettext",
11849#endif
11850#ifdef FEAT_GUI
11851 "gui",
11852#endif
11853#ifdef FEAT_GUI_ATHENA
11854# ifdef FEAT_GUI_NEXTAW
11855 "gui_neXtaw",
11856# else
11857 "gui_athena",
11858# endif
11859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860#ifdef FEAT_GUI_GTK
11861 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011864#ifdef FEAT_GUI_GNOME
11865 "gui_gnome",
11866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867#ifdef FEAT_GUI_MAC
11868 "gui_mac",
11869#endif
11870#ifdef FEAT_GUI_MOTIF
11871 "gui_motif",
11872#endif
11873#ifdef FEAT_GUI_PHOTON
11874 "gui_photon",
11875#endif
11876#ifdef FEAT_GUI_W16
11877 "gui_win16",
11878#endif
11879#ifdef FEAT_GUI_W32
11880 "gui_win32",
11881#endif
11882#ifdef FEAT_HANGULIN
11883 "hangul_input",
11884#endif
11885#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11886 "iconv",
11887#endif
11888#ifdef FEAT_INS_EXPAND
11889 "insert_expand",
11890#endif
11891#ifdef FEAT_JUMPLIST
11892 "jumplist",
11893#endif
11894#ifdef FEAT_KEYMAP
11895 "keymap",
11896#endif
11897#ifdef FEAT_LANGMAP
11898 "langmap",
11899#endif
11900#ifdef FEAT_LIBCALL
11901 "libcall",
11902#endif
11903#ifdef FEAT_LINEBREAK
11904 "linebreak",
11905#endif
11906#ifdef FEAT_LISP
11907 "lispindent",
11908#endif
11909#ifdef FEAT_LISTCMDS
11910 "listcmds",
11911#endif
11912#ifdef FEAT_LOCALMAP
11913 "localmap",
11914#endif
11915#ifdef FEAT_MENU
11916 "menu",
11917#endif
11918#ifdef FEAT_SESSION
11919 "mksession",
11920#endif
11921#ifdef FEAT_MODIFY_FNAME
11922 "modify_fname",
11923#endif
11924#ifdef FEAT_MOUSE
11925 "mouse",
11926#endif
11927#ifdef FEAT_MOUSESHAPE
11928 "mouseshape",
11929#endif
11930#if defined(UNIX) || defined(VMS)
11931# ifdef FEAT_MOUSE_DEC
11932 "mouse_dec",
11933# endif
11934# ifdef FEAT_MOUSE_GPM
11935 "mouse_gpm",
11936# endif
11937# ifdef FEAT_MOUSE_JSB
11938 "mouse_jsbterm",
11939# endif
11940# ifdef FEAT_MOUSE_NET
11941 "mouse_netterm",
11942# endif
11943# ifdef FEAT_MOUSE_PTERM
11944 "mouse_pterm",
11945# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011946# ifdef FEAT_SYSMOUSE
11947 "mouse_sysmouse",
11948# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949# ifdef FEAT_MOUSE_XTERM
11950 "mouse_xterm",
11951# endif
11952#endif
11953#ifdef FEAT_MBYTE
11954 "multi_byte",
11955#endif
11956#ifdef FEAT_MBYTE_IME
11957 "multi_byte_ime",
11958#endif
11959#ifdef FEAT_MULTI_LANG
11960 "multi_lang",
11961#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011962#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011963#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011964 "mzscheme",
11965#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967#ifdef FEAT_OLE
11968 "ole",
11969#endif
11970#ifdef FEAT_OSFILETYPE
11971 "osfiletype",
11972#endif
11973#ifdef FEAT_PATH_EXTRA
11974 "path_extra",
11975#endif
11976#ifdef FEAT_PERL
11977#ifndef DYNAMIC_PERL
11978 "perl",
11979#endif
11980#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011981#ifdef FEAT_PERSISTENT_UNDO
11982 "persistent_undo",
11983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984#ifdef FEAT_PYTHON
11985#ifndef DYNAMIC_PYTHON
11986 "python",
11987#endif
11988#endif
11989#ifdef FEAT_POSTSCRIPT
11990 "postscript",
11991#endif
11992#ifdef FEAT_PRINTER
11993 "printer",
11994#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000011995#ifdef FEAT_PROFILE
11996 "profile",
11997#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000011998#ifdef FEAT_RELTIME
11999 "reltime",
12000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001#ifdef FEAT_QUICKFIX
12002 "quickfix",
12003#endif
12004#ifdef FEAT_RIGHTLEFT
12005 "rightleft",
12006#endif
12007#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12008 "ruby",
12009#endif
12010#ifdef FEAT_SCROLLBIND
12011 "scrollbind",
12012#endif
12013#ifdef FEAT_CMDL_INFO
12014 "showcmd",
12015 "cmdline_info",
12016#endif
12017#ifdef FEAT_SIGNS
12018 "signs",
12019#endif
12020#ifdef FEAT_SMARTINDENT
12021 "smartindent",
12022#endif
12023#ifdef FEAT_SNIFF
12024 "sniff",
12025#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012026#ifdef STARTUPTIME
12027 "startuptime",
12028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029#ifdef FEAT_STL_OPT
12030 "statusline",
12031#endif
12032#ifdef FEAT_SUN_WORKSHOP
12033 "sun_workshop",
12034#endif
12035#ifdef FEAT_NETBEANS_INTG
12036 "netbeans_intg",
12037#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012038#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012039 "spell",
12040#endif
12041#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 "syntax",
12043#endif
12044#if defined(USE_SYSTEM) || !defined(UNIX)
12045 "system",
12046#endif
12047#ifdef FEAT_TAG_BINS
12048 "tag_binary",
12049#endif
12050#ifdef FEAT_TAG_OLDSTATIC
12051 "tag_old_static",
12052#endif
12053#ifdef FEAT_TAG_ANYWHITE
12054 "tag_any_white",
12055#endif
12056#ifdef FEAT_TCL
12057# ifndef DYNAMIC_TCL
12058 "tcl",
12059# endif
12060#endif
12061#ifdef TERMINFO
12062 "terminfo",
12063#endif
12064#ifdef FEAT_TERMRESPONSE
12065 "termresponse",
12066#endif
12067#ifdef FEAT_TEXTOBJ
12068 "textobjects",
12069#endif
12070#ifdef HAVE_TGETENT
12071 "tgetent",
12072#endif
12073#ifdef FEAT_TITLE
12074 "title",
12075#endif
12076#ifdef FEAT_TOOLBAR
12077 "toolbar",
12078#endif
12079#ifdef FEAT_USR_CMDS
12080 "user-commands", /* was accidentally included in 5.4 */
12081 "user_commands",
12082#endif
12083#ifdef FEAT_VIMINFO
12084 "viminfo",
12085#endif
12086#ifdef FEAT_VERTSPLIT
12087 "vertsplit",
12088#endif
12089#ifdef FEAT_VIRTUALEDIT
12090 "virtualedit",
12091#endif
12092#ifdef FEAT_VISUAL
12093 "visual",
12094#endif
12095#ifdef FEAT_VISUALEXTRA
12096 "visualextra",
12097#endif
12098#ifdef FEAT_VREPLACE
12099 "vreplace",
12100#endif
12101#ifdef FEAT_WILDIGN
12102 "wildignore",
12103#endif
12104#ifdef FEAT_WILDMENU
12105 "wildmenu",
12106#endif
12107#ifdef FEAT_WINDOWS
12108 "windows",
12109#endif
12110#ifdef FEAT_WAK
12111 "winaltkeys",
12112#endif
12113#ifdef FEAT_WRITEBACKUP
12114 "writebackup",
12115#endif
12116#ifdef FEAT_XIM
12117 "xim",
12118#endif
12119#ifdef FEAT_XFONTSET
12120 "xfontset",
12121#endif
12122#ifdef USE_XSMP
12123 "xsmp",
12124#endif
12125#ifdef USE_XSMP_INTERACT
12126 "xsmp_interact",
12127#endif
12128#ifdef FEAT_XCLIPBOARD
12129 "xterm_clipboard",
12130#endif
12131#ifdef FEAT_XTERM_SAVE
12132 "xterm_save",
12133#endif
12134#if defined(UNIX) && defined(FEAT_X11)
12135 "X11",
12136#endif
12137 NULL
12138 };
12139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 for (i = 0; has_list[i] != NULL; ++i)
12142 if (STRICMP(name, has_list[i]) == 0)
12143 {
12144 n = TRUE;
12145 break;
12146 }
12147
12148 if (n == FALSE)
12149 {
12150 if (STRNICMP(name, "patch", 5) == 0)
12151 n = has_patch(atoi((char *)name + 5));
12152 else if (STRICMP(name, "vim_starting") == 0)
12153 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012154#ifdef FEAT_MBYTE
12155 else if (STRICMP(name, "multi_byte_encoding") == 0)
12156 n = has_mbyte;
12157#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012158#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12159 else if (STRICMP(name, "balloon_multiline") == 0)
12160 n = multiline_balloon_available();
12161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162#ifdef DYNAMIC_TCL
12163 else if (STRICMP(name, "tcl") == 0)
12164 n = tcl_enabled(FALSE);
12165#endif
12166#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12167 else if (STRICMP(name, "iconv") == 0)
12168 n = iconv_enabled(FALSE);
12169#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012170#ifdef DYNAMIC_MZSCHEME
12171 else if (STRICMP(name, "mzscheme") == 0)
12172 n = mzscheme_enabled(FALSE);
12173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174#ifdef DYNAMIC_RUBY
12175 else if (STRICMP(name, "ruby") == 0)
12176 n = ruby_enabled(FALSE);
12177#endif
12178#ifdef DYNAMIC_PYTHON
12179 else if (STRICMP(name, "python") == 0)
12180 n = python_enabled(FALSE);
12181#endif
12182#ifdef DYNAMIC_PERL
12183 else if (STRICMP(name, "perl") == 0)
12184 n = perl_enabled(FALSE);
12185#endif
12186#ifdef FEAT_GUI
12187 else if (STRICMP(name, "gui_running") == 0)
12188 n = (gui.in_use || gui.starting);
12189# ifdef FEAT_GUI_W32
12190 else if (STRICMP(name, "gui_win32s") == 0)
12191 n = gui_is_win32s();
12192# endif
12193# ifdef FEAT_BROWSE
12194 else if (STRICMP(name, "browse") == 0)
12195 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12196# endif
12197#endif
12198#ifdef FEAT_SYN_HL
12199 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012200 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201#endif
12202#if defined(WIN3264)
12203 else if (STRICMP(name, "win95") == 0)
12204 n = mch_windows95();
12205#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012206#ifdef FEAT_NETBEANS_INTG
12207 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012208 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012210 }
12211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012212 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213}
12214
12215/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012216 * "has_key()" function
12217 */
12218 static void
12219f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012220 typval_T *argvars;
12221 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012222{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012223 if (argvars[0].v_type != VAR_DICT)
12224 {
12225 EMSG(_(e_dictreq));
12226 return;
12227 }
12228 if (argvars[0].vval.v_dict == NULL)
12229 return;
12230
12231 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012232 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012233}
12234
12235/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012236 * "haslocaldir()" function
12237 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012238 static void
12239f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012240 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012241 typval_T *rettv;
12242{
12243 rettv->vval.v_number = (curwin->w_localdir != NULL);
12244}
12245
12246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247 * "hasmapto()" function
12248 */
12249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012250f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012251 typval_T *argvars;
12252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253{
12254 char_u *name;
12255 char_u *mode;
12256 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012257 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012259 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012260 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 mode = (char_u *)"nvo";
12262 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012265 if (argvars[2].v_type != VAR_UNKNOWN)
12266 abbr = get_tv_number(&argvars[2]);
12267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268
Bram Moolenaar2c932302006-03-18 21:42:09 +000012269 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273}
12274
12275/*
12276 * "histadd()" function
12277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012279f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012280 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282{
12283#ifdef FEAT_CMDHIST
12284 int histype;
12285 char_u *str;
12286 char_u buf[NUMBUFLEN];
12287#endif
12288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012289 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 if (check_restricted() || check_secure())
12291 return;
12292#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012293 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12294 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 if (histype >= 0)
12296 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298 if (*str != NUL)
12299 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012300 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012302 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 return;
12304 }
12305 }
12306#endif
12307}
12308
12309/*
12310 * "histdel()" function
12311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012313f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012314 typval_T *argvars UNUSED;
12315 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316{
12317#ifdef FEAT_CMDHIST
12318 int n;
12319 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012320 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012322 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12323 if (str == NULL)
12324 n = 0;
12325 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012327 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012328 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012330 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012331 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332 else
12333 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012334 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012335 get_tv_string_buf(&argvars[1], buf));
12336 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337#endif
12338}
12339
12340/*
12341 * "histget()" function
12342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012345 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347{
12348#ifdef FEAT_CMDHIST
12349 int type;
12350 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012351 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012353 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12354 if (str == NULL)
12355 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012357 {
12358 type = get_histtype(str);
12359 if (argvars[1].v_type == VAR_UNKNOWN)
12360 idx = get_history_idx(type);
12361 else
12362 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12363 /* -1 on type error */
12364 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012367 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370}
12371
12372/*
12373 * "histnr()" function
12374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012377 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379{
12380 int i;
12381
12382#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012383 char_u *history = get_tv_string_chk(&argvars[0]);
12384
12385 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386 if (i >= HIST_CMD && i < HIST_COUNT)
12387 i = get_history_idx(i);
12388 else
12389#endif
12390 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392}
12393
12394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395 * "highlightID(name)" function
12396 */
12397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012398f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012399 typval_T *argvars;
12400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012402 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403}
12404
12405/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012406 * "highlight_exists()" function
12407 */
12408 static void
12409f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012410 typval_T *argvars;
12411 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012412{
12413 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12414}
12415
12416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417 * "hostname()" function
12418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012421 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423{
12424 char_u hostname[256];
12425
12426 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012427 rettv->v_type = VAR_STRING;
12428 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429}
12430
12431/*
12432 * iconv() function
12433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012435f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012436 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438{
12439#ifdef FEAT_MBYTE
12440 char_u buf1[NUMBUFLEN];
12441 char_u buf2[NUMBUFLEN];
12442 char_u *from, *to, *str;
12443 vimconv_T vimconv;
12444#endif
12445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012446 rettv->v_type = VAR_STRING;
12447 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448
12449#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012450 str = get_tv_string(&argvars[0]);
12451 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12452 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453 vimconv.vc_type = CONV_NONE;
12454 convert_setup(&vimconv, from, to);
12455
12456 /* If the encodings are equal, no conversion needed. */
12457 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012460 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461
12462 convert_setup(&vimconv, NULL, NULL);
12463 vim_free(from);
12464 vim_free(to);
12465#endif
12466}
12467
12468/*
12469 * "indent()" function
12470 */
12471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012472f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012473 typval_T *argvars;
12474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475{
12476 linenr_T lnum;
12477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012478 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012480 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012482 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483}
12484
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012485/*
12486 * "index()" function
12487 */
12488 static void
12489f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012490 typval_T *argvars;
12491 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012492{
Bram Moolenaar33570922005-01-25 22:26:29 +000012493 list_T *l;
12494 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012495 long idx = 0;
12496 int ic = FALSE;
12497
12498 rettv->vval.v_number = -1;
12499 if (argvars[0].v_type != VAR_LIST)
12500 {
12501 EMSG(_(e_listreq));
12502 return;
12503 }
12504 l = argvars[0].vval.v_list;
12505 if (l != NULL)
12506 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012507 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012508 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012510 int error = FALSE;
12511
Bram Moolenaar758711c2005-02-02 23:11:38 +000012512 /* Start at specified item. Use the cached index that list_find()
12513 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012514 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012515 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012516 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517 ic = get_tv_number_chk(&argvars[3], &error);
12518 if (error)
12519 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012520 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012521
Bram Moolenaar758711c2005-02-02 23:11:38 +000012522 for ( ; item != NULL; item = item->li_next, ++idx)
12523 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012524 {
12525 rettv->vval.v_number = idx;
12526 break;
12527 }
12528 }
12529}
12530
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531static int inputsecret_flag = 0;
12532
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012533static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12534
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012536 * This function is used by f_input() and f_inputdialog() functions. The third
12537 * argument to f_input() specifies the type of completion to use at the
12538 * prompt. The third argument to f_inputdialog() specifies the value to return
12539 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 */
12541 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012542get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012543 typval_T *argvars;
12544 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012545 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012547 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548 char_u *p = NULL;
12549 int c;
12550 char_u buf[NUMBUFLEN];
12551 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012552 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012553 int xp_type = EXPAND_NOTHING;
12554 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012556 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012557 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558
12559#ifdef NO_CONSOLE_INPUT
12560 /* While starting up, there is no place to enter text. */
12561 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563#endif
12564
12565 cmd_silent = FALSE; /* Want to see the prompt. */
12566 if (prompt != NULL)
12567 {
12568 /* Only the part of the message after the last NL is considered as
12569 * prompt for the command line */
12570 p = vim_strrchr(prompt, '\n');
12571 if (p == NULL)
12572 p = prompt;
12573 else
12574 {
12575 ++p;
12576 c = *p;
12577 *p = NUL;
12578 msg_start();
12579 msg_clr_eos();
12580 msg_puts_attr(prompt, echo_attr);
12581 msg_didout = FALSE;
12582 msg_starthere();
12583 *p = c;
12584 }
12585 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012587 if (argvars[1].v_type != VAR_UNKNOWN)
12588 {
12589 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12590 if (defstr != NULL)
12591 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012593 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012594 {
12595 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012596 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012597 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012598
Bram Moolenaar4463f292005-09-25 22:20:24 +000012599 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012600
Bram Moolenaar4463f292005-09-25 22:20:24 +000012601 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12602 if (xp_name == NULL)
12603 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012604
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012605 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012606
Bram Moolenaar4463f292005-09-25 22:20:24 +000012607 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12608 &xp_arg) == FAIL)
12609 return;
12610 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012611 }
12612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012613 if (defstr != NULL)
12614 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012615 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12616 xp_type, xp_arg);
12617
12618 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012620 /* since the user typed this, no need to wait for return */
12621 need_wait_return = FALSE;
12622 msg_didout = FALSE;
12623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624 cmd_silent = cmd_silent_save;
12625}
12626
12627/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012628 * "input()" function
12629 * Also handles inputsecret() when inputsecret is set.
12630 */
12631 static void
12632f_input(argvars, rettv)
12633 typval_T *argvars;
12634 typval_T *rettv;
12635{
12636 get_user_input(argvars, rettv, FALSE);
12637}
12638
12639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640 * "inputdialog()" function
12641 */
12642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012644 typval_T *argvars;
12645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646{
12647#if defined(FEAT_GUI_TEXTDIALOG)
12648 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12649 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12650 {
12651 char_u *message;
12652 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012653 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012655 message = get_tv_string_chk(&argvars[0]);
12656 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012657 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012658 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659 else
12660 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012661 if (message != NULL && defstr != NULL
12662 && do_dialog(VIM_QUESTION, NULL, message,
12663 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012664 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 else
12666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012667 if (message != NULL && defstr != NULL
12668 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012669 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012670 rettv->vval.v_string = vim_strsave(
12671 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 }
12677 else
12678#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012679 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680}
12681
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012682/*
12683 * "inputlist()" function
12684 */
12685 static void
12686f_inputlist(argvars, rettv)
12687 typval_T *argvars;
12688 typval_T *rettv;
12689{
12690 listitem_T *li;
12691 int selected;
12692 int mouse_used;
12693
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012694#ifdef NO_CONSOLE_INPUT
12695 /* While starting up, there is no place to enter text. */
12696 if (no_console_input())
12697 return;
12698#endif
12699 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12700 {
12701 EMSG2(_(e_listarg), "inputlist()");
12702 return;
12703 }
12704
12705 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012706 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012707 lines_left = Rows; /* avoid more prompt */
12708 msg_scroll = TRUE;
12709 msg_clr_eos();
12710
12711 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12712 {
12713 msg_puts(get_tv_string(&li->li_tv));
12714 msg_putchar('\n');
12715 }
12716
12717 /* Ask for choice. */
12718 selected = prompt_for_number(&mouse_used);
12719 if (mouse_used)
12720 selected -= lines_left;
12721
12722 rettv->vval.v_number = selected;
12723}
12724
12725
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12727
12728/*
12729 * "inputrestore()" function
12730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012733 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735{
12736 if (ga_userinput.ga_len > 0)
12737 {
12738 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12740 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012741 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742 }
12743 else if (p_verbose > 1)
12744 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012745 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012746 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 }
12748}
12749
12750/*
12751 * "inputsave()" function
12752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012755 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012758 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 if (ga_grow(&ga_userinput, 1) == OK)
12760 {
12761 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12762 + ga_userinput.ga_len);
12763 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012764 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765 }
12766 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012767 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768}
12769
12770/*
12771 * "inputsecret()" function
12772 */
12773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012775 typval_T *argvars;
12776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777{
12778 ++cmdline_star;
12779 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 --cmdline_star;
12782 --inputsecret_flag;
12783}
12784
12785/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012786 * "insert()" function
12787 */
12788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012789f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012790 typval_T *argvars;
12791 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012792{
12793 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012794 listitem_T *item;
12795 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012796 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012797
12798 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012799 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012800 else if ((l = argvars[0].vval.v_list) != NULL
12801 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012802 {
12803 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012804 before = get_tv_number_chk(&argvars[2], &error);
12805 if (error)
12806 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012807
Bram Moolenaar758711c2005-02-02 23:11:38 +000012808 if (before == l->lv_len)
12809 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012810 else
12811 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012812 item = list_find(l, before);
12813 if (item == NULL)
12814 {
12815 EMSGN(_(e_listidx), before);
12816 l = NULL;
12817 }
12818 }
12819 if (l != NULL)
12820 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012821 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012822 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012823 }
12824 }
12825}
12826
12827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 * "isdirectory()" function
12829 */
12830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012832 typval_T *argvars;
12833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836}
12837
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012838/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012839 * "islocked()" function
12840 */
12841 static void
12842f_islocked(argvars, rettv)
12843 typval_T *argvars;
12844 typval_T *rettv;
12845{
12846 lval_T lv;
12847 char_u *end;
12848 dictitem_T *di;
12849
12850 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012851 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12852 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012853 if (end != NULL && lv.ll_name != NULL)
12854 {
12855 if (*end != NUL)
12856 EMSG(_(e_trailing));
12857 else
12858 {
12859 if (lv.ll_tv == NULL)
12860 {
12861 if (check_changedtick(lv.ll_name))
12862 rettv->vval.v_number = 1; /* always locked */
12863 else
12864 {
12865 di = find_var(lv.ll_name, NULL);
12866 if (di != NULL)
12867 {
12868 /* Consider a variable locked when:
12869 * 1. the variable itself is locked
12870 * 2. the value of the variable is locked.
12871 * 3. the List or Dict value is locked.
12872 */
12873 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12874 || tv_islocked(&di->di_tv));
12875 }
12876 }
12877 }
12878 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012879 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012880 else if (lv.ll_newkey != NULL)
12881 EMSG2(_(e_dictkey), lv.ll_newkey);
12882 else if (lv.ll_list != NULL)
12883 /* List item. */
12884 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12885 else
12886 /* Dictionary item. */
12887 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12888 }
12889 }
12890
12891 clear_lval(&lv);
12892}
12893
Bram Moolenaar33570922005-01-25 22:26:29 +000012894static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012895
12896/*
12897 * Turn a dict into a list:
12898 * "what" == 0: list of keys
12899 * "what" == 1: list of values
12900 * "what" == 2: list of items
12901 */
12902 static void
12903dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012904 typval_T *argvars;
12905 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012906 int what;
12907{
Bram Moolenaar33570922005-01-25 22:26:29 +000012908 list_T *l2;
12909 dictitem_T *di;
12910 hashitem_T *hi;
12911 listitem_T *li;
12912 listitem_T *li2;
12913 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012914 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012915
Bram Moolenaar8c711452005-01-14 21:53:12 +000012916 if (argvars[0].v_type != VAR_DICT)
12917 {
12918 EMSG(_(e_dictreq));
12919 return;
12920 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012921 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012922 return;
12923
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012924 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012925 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012926
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012927 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012928 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012929 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012930 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012931 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012932 --todo;
12933 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012934
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012935 li = listitem_alloc();
12936 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012937 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012938 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012939
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012940 if (what == 0)
12941 {
12942 /* keys() */
12943 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012944 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012945 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12946 }
12947 else if (what == 1)
12948 {
12949 /* values() */
12950 copy_tv(&di->di_tv, &li->li_tv);
12951 }
12952 else
12953 {
12954 /* items() */
12955 l2 = list_alloc();
12956 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012957 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012958 li->li_tv.vval.v_list = l2;
12959 if (l2 == NULL)
12960 break;
12961 ++l2->lv_refcount;
12962
12963 li2 = listitem_alloc();
12964 if (li2 == NULL)
12965 break;
12966 list_append(l2, li2);
12967 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012968 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012969 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12970
12971 li2 = listitem_alloc();
12972 if (li2 == NULL)
12973 break;
12974 list_append(l2, li2);
12975 copy_tv(&di->di_tv, &li2->li_tv);
12976 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012977 }
12978 }
12979}
12980
12981/*
12982 * "items(dict)" function
12983 */
12984 static void
12985f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012986 typval_T *argvars;
12987 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012988{
12989 dict_list(argvars, rettv, 2);
12990}
12991
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012993 * "join()" function
12994 */
12995 static void
12996f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012997 typval_T *argvars;
12998 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012999{
13000 garray_T ga;
13001 char_u *sep;
13002
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013003 if (argvars[0].v_type != VAR_LIST)
13004 {
13005 EMSG(_(e_listreq));
13006 return;
13007 }
13008 if (argvars[0].vval.v_list == NULL)
13009 return;
13010 if (argvars[1].v_type == VAR_UNKNOWN)
13011 sep = (char_u *)" ";
13012 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013013 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013014
13015 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013016
13017 if (sep != NULL)
13018 {
13019 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013020 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013021 ga_append(&ga, NUL);
13022 rettv->vval.v_string = (char_u *)ga.ga_data;
13023 }
13024 else
13025 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013026}
13027
13028/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013029 * "keys()" function
13030 */
13031 static void
13032f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013033 typval_T *argvars;
13034 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013035{
13036 dict_list(argvars, rettv, 0);
13037}
13038
13039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040 * "last_buffer_nr()" function.
13041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013044 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046{
13047 int n = 0;
13048 buf_T *buf;
13049
13050 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13051 if (n < buf->b_fnum)
13052 n = buf->b_fnum;
13053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013054 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013055}
13056
13057/*
13058 * "len()" function
13059 */
13060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013061f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013062 typval_T *argvars;
13063 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013064{
13065 switch (argvars[0].v_type)
13066 {
13067 case VAR_STRING:
13068 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013069 rettv->vval.v_number = (varnumber_T)STRLEN(
13070 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013071 break;
13072 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013074 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013075 case VAR_DICT:
13076 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13077 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013078 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013079 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013080 break;
13081 }
13082}
13083
Bram Moolenaar33570922005-01-25 22:26:29 +000013084static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013085
13086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013087libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013088 typval_T *argvars;
13089 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013090 int type;
13091{
13092#ifdef FEAT_LIBCALL
13093 char_u *string_in;
13094 char_u **string_result;
13095 int nr_result;
13096#endif
13097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013098 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013099 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013100 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013101
13102 if (check_restricted() || check_secure())
13103 return;
13104
13105#ifdef FEAT_LIBCALL
13106 /* The first two args must be strings, otherwise its meaningless */
13107 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13108 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013109 string_in = NULL;
13110 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013111 string_in = argvars[2].vval.v_string;
13112 if (type == VAR_NUMBER)
13113 string_result = NULL;
13114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013115 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013116 if (mch_libcall(argvars[0].vval.v_string,
13117 argvars[1].vval.v_string,
13118 string_in,
13119 argvars[2].vval.v_number,
13120 string_result,
13121 &nr_result) == OK
13122 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013123 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013124 }
13125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126}
13127
13128/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013129 * "libcall()" function
13130 */
13131 static void
13132f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013133 typval_T *argvars;
13134 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013135{
13136 libcall_common(argvars, rettv, VAR_STRING);
13137}
13138
13139/*
13140 * "libcallnr()" function
13141 */
13142 static void
13143f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013144 typval_T *argvars;
13145 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013146{
13147 libcall_common(argvars, rettv, VAR_NUMBER);
13148}
13149
13150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151 * "line(string)" function
13152 */
13153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013154f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013155 typval_T *argvars;
13156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157{
13158 linenr_T lnum = 0;
13159 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013160 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013162 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 if (fp != NULL)
13164 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013165 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166}
13167
13168/*
13169 * "line2byte(lnum)" function
13170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013172f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013173 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175{
13176#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178#else
13179 linenr_T lnum;
13180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013181 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013183 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13186 if (rettv->vval.v_number >= 0)
13187 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188#endif
13189}
13190
13191/*
13192 * "lispindent(lnum)" function
13193 */
13194 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013195f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013196 typval_T *argvars;
13197 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198{
13199#ifdef FEAT_LISP
13200 pos_T pos;
13201 linenr_T lnum;
13202
13203 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013204 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13206 {
13207 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013208 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209 curwin->w_cursor = pos;
13210 }
13211 else
13212#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214}
13215
13216/*
13217 * "localtime()" function
13218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013221 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013224 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225}
13226
Bram Moolenaar33570922005-01-25 22:26:29 +000013227static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228
13229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013231 typval_T *argvars;
13232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 int exact;
13234{
13235 char_u *keys;
13236 char_u *which;
13237 char_u buf[NUMBUFLEN];
13238 char_u *keys_buf = NULL;
13239 char_u *rhs;
13240 int mode;
13241 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013242 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243
13244 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013245 rettv->v_type = VAR_STRING;
13246 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013248 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249 if (*keys == NUL)
13250 return;
13251
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013252 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013254 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013255 if (argvars[2].v_type != VAR_UNKNOWN)
13256 abbr = get_tv_number(&argvars[2]);
13257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 else
13259 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013260 if (which == NULL)
13261 return;
13262
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 mode = get_map_mode(&which, 0);
13264
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013265 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013266 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267 vim_free(keys_buf);
13268 if (rhs != NULL)
13269 {
13270 ga_init(&ga);
13271 ga.ga_itemsize = 1;
13272 ga.ga_growsize = 40;
13273
13274 while (*rhs != NUL)
13275 ga_concat(&ga, str2special(&rhs, FALSE));
13276
13277 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279 }
13280}
13281
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013282#ifdef FEAT_FLOAT
13283/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013284 * "log()" function
13285 */
13286 static void
13287f_log(argvars, rettv)
13288 typval_T *argvars;
13289 typval_T *rettv;
13290{
13291 float_T f;
13292
13293 rettv->v_type = VAR_FLOAT;
13294 if (get_float_arg(argvars, &f) == OK)
13295 rettv->vval.v_float = log(f);
13296 else
13297 rettv->vval.v_float = 0.0;
13298}
13299
13300/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013301 * "log10()" function
13302 */
13303 static void
13304f_log10(argvars, rettv)
13305 typval_T *argvars;
13306 typval_T *rettv;
13307{
13308 float_T f;
13309
13310 rettv->v_type = VAR_FLOAT;
13311 if (get_float_arg(argvars, &f) == OK)
13312 rettv->vval.v_float = log10(f);
13313 else
13314 rettv->vval.v_float = 0.0;
13315}
13316#endif
13317
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013319 * "map()" function
13320 */
13321 static void
13322f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013323 typval_T *argvars;
13324 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013325{
13326 filter_map(argvars, rettv, TRUE);
13327}
13328
13329/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013330 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 */
13332 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013333f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013334 typval_T *argvars;
13335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013337 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338}
13339
13340/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013341 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342 */
13343 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013344f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 typval_T *argvars;
13346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013348 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349}
13350
Bram Moolenaar33570922005-01-25 22:26:29 +000013351static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352
13353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013354find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013355 typval_T *argvars;
13356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 int type;
13358{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013359 char_u *str = NULL;
13360 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361 char_u *pat;
13362 regmatch_T regmatch;
13363 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013364 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365 char_u *save_cpo;
13366 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013367 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013368 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013369 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013370 list_T *l = NULL;
13371 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013372 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013373 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374
13375 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13376 save_cpo = p_cpo;
13377 p_cpo = (char_u *)"";
13378
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013379 rettv->vval.v_number = -1;
13380 if (type == 3)
13381 {
13382 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013383 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013384 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013385 }
13386 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013388 rettv->v_type = VAR_STRING;
13389 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013392 if (argvars[0].v_type == VAR_LIST)
13393 {
13394 if ((l = argvars[0].vval.v_list) == NULL)
13395 goto theend;
13396 li = l->lv_first;
13397 }
13398 else
13399 expr = str = get_tv_string(&argvars[0]);
13400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013401 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13402 if (pat == NULL)
13403 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013404
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013405 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013407 int error = FALSE;
13408
13409 start = get_tv_number_chk(&argvars[2], &error);
13410 if (error)
13411 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013412 if (l != NULL)
13413 {
13414 li = list_find(l, start);
13415 if (li == NULL)
13416 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013417 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013418 }
13419 else
13420 {
13421 if (start < 0)
13422 start = 0;
13423 if (start > (long)STRLEN(str))
13424 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013425 /* When "count" argument is there ignore matches before "start",
13426 * otherwise skip part of the string. Differs when pattern is "^"
13427 * or "\<". */
13428 if (argvars[3].v_type != VAR_UNKNOWN)
13429 startcol = start;
13430 else
13431 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013432 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013433
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013434 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013435 nth = get_tv_number_chk(&argvars[3], &error);
13436 if (error)
13437 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 }
13439
13440 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13441 if (regmatch.regprog != NULL)
13442 {
13443 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013444
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013445 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013446 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013447 if (l != NULL)
13448 {
13449 if (li == NULL)
13450 {
13451 match = FALSE;
13452 break;
13453 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013454 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013455 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013456 if (str == NULL)
13457 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013458 }
13459
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013460 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013461
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013462 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013463 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013464 if (l == NULL && !match)
13465 break;
13466
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013467 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013468 if (l != NULL)
13469 {
13470 li = li->li_next;
13471 ++idx;
13472 }
13473 else
13474 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013475#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013476 startcol = (colnr_T)(regmatch.startp[0]
13477 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013478#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013479 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013480#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013481 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013482 }
13483
13484 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013486 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013487 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013488 int i;
13489
13490 /* return list with matched string and submatches */
13491 for (i = 0; i < NSUBEXP; ++i)
13492 {
13493 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013494 {
13495 if (list_append_string(rettv->vval.v_list,
13496 (char_u *)"", 0) == FAIL)
13497 break;
13498 }
13499 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013500 regmatch.startp[i],
13501 (int)(regmatch.endp[i] - regmatch.startp[i]))
13502 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013503 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013504 }
13505 }
13506 else if (type == 2)
13507 {
13508 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013509 if (l != NULL)
13510 copy_tv(&li->li_tv, rettv);
13511 else
13512 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013514 }
13515 else if (l != NULL)
13516 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 else
13518 {
13519 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521 (varnumber_T)(regmatch.startp[0] - str);
13522 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013523 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013525 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526 }
13527 }
13528 vim_free(regmatch.regprog);
13529 }
13530
13531theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013532 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 p_cpo = save_cpo;
13534}
13535
13536/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013537 * "match()" function
13538 */
13539 static void
13540f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013541 typval_T *argvars;
13542 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013543{
13544 find_some_match(argvars, rettv, 1);
13545}
13546
13547/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013548 * "matchadd()" function
13549 */
13550 static void
13551f_matchadd(argvars, rettv)
13552 typval_T *argvars;
13553 typval_T *rettv;
13554{
13555#ifdef FEAT_SEARCH_EXTRA
13556 char_u buf[NUMBUFLEN];
13557 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13558 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13559 int prio = 10; /* default priority */
13560 int id = -1;
13561 int error = FALSE;
13562
13563 rettv->vval.v_number = -1;
13564
13565 if (grp == NULL || pat == NULL)
13566 return;
13567 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013568 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013569 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013570 if (argvars[3].v_type != VAR_UNKNOWN)
13571 id = get_tv_number_chk(&argvars[3], &error);
13572 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013573 if (error == TRUE)
13574 return;
13575 if (id >= 1 && id <= 3)
13576 {
13577 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13578 return;
13579 }
13580
13581 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13582#endif
13583}
13584
13585/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013586 * "matcharg()" function
13587 */
13588 static void
13589f_matcharg(argvars, rettv)
13590 typval_T *argvars;
13591 typval_T *rettv;
13592{
13593 if (rettv_list_alloc(rettv) == OK)
13594 {
13595#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013596 int id = get_tv_number(&argvars[0]);
13597 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013598
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013599 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013600 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013601 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13602 {
13603 list_append_string(rettv->vval.v_list,
13604 syn_id2name(m->hlg_id), -1);
13605 list_append_string(rettv->vval.v_list, m->pattern, -1);
13606 }
13607 else
13608 {
13609 list_append_string(rettv->vval.v_list, NUL, -1);
13610 list_append_string(rettv->vval.v_list, NUL, -1);
13611 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013612 }
13613#endif
13614 }
13615}
13616
13617/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013618 * "matchdelete()" function
13619 */
13620 static void
13621f_matchdelete(argvars, rettv)
13622 typval_T *argvars;
13623 typval_T *rettv;
13624{
13625#ifdef FEAT_SEARCH_EXTRA
13626 rettv->vval.v_number = match_delete(curwin,
13627 (int)get_tv_number(&argvars[0]), TRUE);
13628#endif
13629}
13630
13631/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013632 * "matchend()" function
13633 */
13634 static void
13635f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013636 typval_T *argvars;
13637 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013638{
13639 find_some_match(argvars, rettv, 0);
13640}
13641
13642/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013643 * "matchlist()" function
13644 */
13645 static void
13646f_matchlist(argvars, rettv)
13647 typval_T *argvars;
13648 typval_T *rettv;
13649{
13650 find_some_match(argvars, rettv, 3);
13651}
13652
13653/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013654 * "matchstr()" function
13655 */
13656 static void
13657f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013658 typval_T *argvars;
13659 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013660{
13661 find_some_match(argvars, rettv, 2);
13662}
13663
Bram Moolenaar33570922005-01-25 22:26:29 +000013664static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013665
13666 static void
13667max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013668 typval_T *argvars;
13669 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013670 int domax;
13671{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013672 long n = 0;
13673 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013674 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013675
13676 if (argvars[0].v_type == VAR_LIST)
13677 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013678 list_T *l;
13679 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013680
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013681 l = argvars[0].vval.v_list;
13682 if (l != NULL)
13683 {
13684 li = l->lv_first;
13685 if (li != NULL)
13686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013687 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013688 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013689 {
13690 li = li->li_next;
13691 if (li == NULL)
13692 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013693 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013694 if (domax ? i > n : i < n)
13695 n = i;
13696 }
13697 }
13698 }
13699 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013700 else if (argvars[0].v_type == VAR_DICT)
13701 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013702 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013703 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013704 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013705 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013706
13707 d = argvars[0].vval.v_dict;
13708 if (d != NULL)
13709 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013710 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013711 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013712 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013713 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013714 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013715 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013716 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013717 if (first)
13718 {
13719 n = i;
13720 first = FALSE;
13721 }
13722 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013723 n = i;
13724 }
13725 }
13726 }
13727 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013728 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013729 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013730 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013731}
13732
13733/*
13734 * "max()" function
13735 */
13736 static void
13737f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013738 typval_T *argvars;
13739 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013740{
13741 max_min(argvars, rettv, TRUE);
13742}
13743
13744/*
13745 * "min()" function
13746 */
13747 static void
13748f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013749 typval_T *argvars;
13750 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013751{
13752 max_min(argvars, rettv, FALSE);
13753}
13754
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013755static int mkdir_recurse __ARGS((char_u *dir, int prot));
13756
13757/*
13758 * Create the directory in which "dir" is located, and higher levels when
13759 * needed.
13760 */
13761 static int
13762mkdir_recurse(dir, prot)
13763 char_u *dir;
13764 int prot;
13765{
13766 char_u *p;
13767 char_u *updir;
13768 int r = FAIL;
13769
13770 /* Get end of directory name in "dir".
13771 * We're done when it's "/" or "c:/". */
13772 p = gettail_sep(dir);
13773 if (p <= get_past_head(dir))
13774 return OK;
13775
13776 /* If the directory exists we're done. Otherwise: create it.*/
13777 updir = vim_strnsave(dir, (int)(p - dir));
13778 if (updir == NULL)
13779 return FAIL;
13780 if (mch_isdir(updir))
13781 r = OK;
13782 else if (mkdir_recurse(updir, prot) == OK)
13783 r = vim_mkdir_emsg(updir, prot);
13784 vim_free(updir);
13785 return r;
13786}
13787
13788#ifdef vim_mkdir
13789/*
13790 * "mkdir()" function
13791 */
13792 static void
13793f_mkdir(argvars, rettv)
13794 typval_T *argvars;
13795 typval_T *rettv;
13796{
13797 char_u *dir;
13798 char_u buf[NUMBUFLEN];
13799 int prot = 0755;
13800
13801 rettv->vval.v_number = FAIL;
13802 if (check_restricted() || check_secure())
13803 return;
13804
13805 dir = get_tv_string_buf(&argvars[0], buf);
13806 if (argvars[1].v_type != VAR_UNKNOWN)
13807 {
13808 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013809 prot = get_tv_number_chk(&argvars[2], NULL);
13810 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013811 mkdir_recurse(dir, prot);
13812 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013814}
13815#endif
13816
Bram Moolenaar0d660222005-01-07 21:51:51 +000013817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 * "mode()" function
13819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013821f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013822 typval_T *argvars;
13823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013825 char_u buf[3];
13826
13827 buf[1] = NUL;
13828 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829
13830#ifdef FEAT_VISUAL
13831 if (VIsual_active)
13832 {
13833 if (VIsual_select)
13834 buf[0] = VIsual_mode + 's' - 'v';
13835 else
13836 buf[0] = VIsual_mode;
13837 }
13838 else
13839#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013840 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13841 || State == CONFIRM)
13842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013844 if (State == ASKMORE)
13845 buf[1] = 'm';
13846 else if (State == CONFIRM)
13847 buf[1] = '?';
13848 }
13849 else if (State == EXTERNCMD)
13850 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851 else if (State & INSERT)
13852 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013853#ifdef FEAT_VREPLACE
13854 if (State & VREPLACE_FLAG)
13855 {
13856 buf[0] = 'R';
13857 buf[1] = 'v';
13858 }
13859 else
13860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 if (State & REPLACE_FLAG)
13862 buf[0] = 'R';
13863 else
13864 buf[0] = 'i';
13865 }
13866 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013869 if (exmode_active)
13870 buf[1] = 'v';
13871 }
13872 else if (exmode_active)
13873 {
13874 buf[0] = 'c';
13875 buf[1] = 'e';
13876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013880 if (finish_op)
13881 buf[1] = 'o';
13882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013884 /* Clear out the minor mode when the argument is not a non-zero number or
13885 * non-empty string. */
13886 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013887 buf[1] = NUL;
13888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013889 rettv->vval.v_string = vim_strsave(buf);
13890 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891}
13892
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013893#ifdef FEAT_MZSCHEME
13894/*
13895 * "mzeval()" function
13896 */
13897 static void
13898f_mzeval(argvars, rettv)
13899 typval_T *argvars;
13900 typval_T *rettv;
13901{
13902 char_u *str;
13903 char_u buf[NUMBUFLEN];
13904
13905 str = get_tv_string_buf(&argvars[0], buf);
13906 do_mzeval(str, rettv);
13907}
13908#endif
13909
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013911 * "nextnonblank()" function
13912 */
13913 static void
13914f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013915 typval_T *argvars;
13916 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013917{
13918 linenr_T lnum;
13919
13920 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013922 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013923 {
13924 lnum = 0;
13925 break;
13926 }
13927 if (*skipwhite(ml_get(lnum)) != NUL)
13928 break;
13929 }
13930 rettv->vval.v_number = lnum;
13931}
13932
13933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 * "nr2char()" function
13935 */
13936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013937f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013938 typval_T *argvars;
13939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940{
13941 char_u buf[NUMBUFLEN];
13942
13943#ifdef FEAT_MBYTE
13944 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013945 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946 else
13947#endif
13948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013949 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 buf[1] = NUL;
13951 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013952 rettv->v_type = VAR_STRING;
13953 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013954}
13955
13956/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013957 * "pathshorten()" function
13958 */
13959 static void
13960f_pathshorten(argvars, rettv)
13961 typval_T *argvars;
13962 typval_T *rettv;
13963{
13964 char_u *p;
13965
13966 rettv->v_type = VAR_STRING;
13967 p = get_tv_string_chk(&argvars[0]);
13968 if (p == NULL)
13969 rettv->vval.v_string = NULL;
13970 else
13971 {
13972 p = vim_strsave(p);
13973 rettv->vval.v_string = p;
13974 if (p != NULL)
13975 shorten_dir(p);
13976 }
13977}
13978
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013979#ifdef FEAT_FLOAT
13980/*
13981 * "pow()" function
13982 */
13983 static void
13984f_pow(argvars, rettv)
13985 typval_T *argvars;
13986 typval_T *rettv;
13987{
13988 float_T fx, fy;
13989
13990 rettv->v_type = VAR_FLOAT;
13991 if (get_float_arg(argvars, &fx) == OK
13992 && get_float_arg(&argvars[1], &fy) == OK)
13993 rettv->vval.v_float = pow(fx, fy);
13994 else
13995 rettv->vval.v_float = 0.0;
13996}
13997#endif
13998
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013999/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014000 * "prevnonblank()" function
14001 */
14002 static void
14003f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014004 typval_T *argvars;
14005 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014006{
14007 linenr_T lnum;
14008
14009 lnum = get_tv_lnum(argvars);
14010 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14011 lnum = 0;
14012 else
14013 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14014 --lnum;
14015 rettv->vval.v_number = lnum;
14016}
14017
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014018#ifdef HAVE_STDARG_H
14019/* This dummy va_list is here because:
14020 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14021 * - locally in the function results in a "used before set" warning
14022 * - using va_start() to initialize it gives "function with fixed args" error */
14023static va_list ap;
14024#endif
14025
Bram Moolenaar8c711452005-01-14 21:53:12 +000014026/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014027 * "printf()" function
14028 */
14029 static void
14030f_printf(argvars, rettv)
14031 typval_T *argvars;
14032 typval_T *rettv;
14033{
14034 rettv->v_type = VAR_STRING;
14035 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014036#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014037 {
14038 char_u buf[NUMBUFLEN];
14039 int len;
14040 char_u *s;
14041 int saved_did_emsg = did_emsg;
14042 char *fmt;
14043
14044 /* Get the required length, allocate the buffer and do it for real. */
14045 did_emsg = FALSE;
14046 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014047 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014048 if (!did_emsg)
14049 {
14050 s = alloc(len + 1);
14051 if (s != NULL)
14052 {
14053 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014054 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014055 }
14056 }
14057 did_emsg |= saved_did_emsg;
14058 }
14059#endif
14060}
14061
14062/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014063 * "pumvisible()" function
14064 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014065 static void
14066f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014067 typval_T *argvars UNUSED;
14068 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014069{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014070#ifdef FEAT_INS_EXPAND
14071 if (pum_visible())
14072 rettv->vval.v_number = 1;
14073#endif
14074}
14075
14076/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014077 * "range()" function
14078 */
14079 static void
14080f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014081 typval_T *argvars;
14082 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014083{
14084 long start;
14085 long end;
14086 long stride = 1;
14087 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014088 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014090 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014091 if (argvars[1].v_type == VAR_UNKNOWN)
14092 {
14093 end = start - 1;
14094 start = 0;
14095 }
14096 else
14097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014098 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014099 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014100 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014101 }
14102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014103 if (error)
14104 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014105 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014106 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014107 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014108 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014109 else
14110 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014111 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014112 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014113 if (list_append_number(rettv->vval.v_list,
14114 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014115 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014116 }
14117}
14118
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014119/*
14120 * "readfile()" function
14121 */
14122 static void
14123f_readfile(argvars, rettv)
14124 typval_T *argvars;
14125 typval_T *rettv;
14126{
14127 int binary = FALSE;
14128 char_u *fname;
14129 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014130 listitem_T *li;
14131#define FREAD_SIZE 200 /* optimized for text lines */
14132 char_u buf[FREAD_SIZE];
14133 int readlen; /* size of last fread() */
14134 int buflen; /* nr of valid chars in buf[] */
14135 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14136 int tolist; /* first byte in buf[] still to be put in list */
14137 int chop; /* how many CR to chop off */
14138 char_u *prev = NULL; /* previously read bytes, if any */
14139 int prevlen = 0; /* length of "prev" if not NULL */
14140 char_u *s;
14141 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014142 long maxline = MAXLNUM;
14143 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014144
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014145 if (argvars[1].v_type != VAR_UNKNOWN)
14146 {
14147 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14148 binary = TRUE;
14149 if (argvars[2].v_type != VAR_UNKNOWN)
14150 maxline = get_tv_number(&argvars[2]);
14151 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014152
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014153 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014154 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014155
14156 /* Always open the file in binary mode, library functions have a mind of
14157 * their own about CR-LF conversion. */
14158 fname = get_tv_string(&argvars[0]);
14159 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14160 {
14161 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14162 return;
14163 }
14164
14165 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014166 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014167 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014168 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014169 buflen = filtd + readlen;
14170 tolist = 0;
14171 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14172 {
14173 if (buf[filtd] == '\n' || readlen <= 0)
14174 {
14175 /* Only when in binary mode add an empty list item when the
14176 * last line ends in a '\n'. */
14177 if (!binary && readlen == 0 && filtd == 0)
14178 break;
14179
14180 /* Found end-of-line or end-of-file: add a text line to the
14181 * list. */
14182 chop = 0;
14183 if (!binary)
14184 while (filtd - chop - 1 >= tolist
14185 && buf[filtd - chop - 1] == '\r')
14186 ++chop;
14187 len = filtd - tolist - chop;
14188 if (prev == NULL)
14189 s = vim_strnsave(buf + tolist, len);
14190 else
14191 {
14192 s = alloc((unsigned)(prevlen + len + 1));
14193 if (s != NULL)
14194 {
14195 mch_memmove(s, prev, prevlen);
14196 vim_free(prev);
14197 prev = NULL;
14198 mch_memmove(s + prevlen, buf + tolist, len);
14199 s[prevlen + len] = NUL;
14200 }
14201 }
14202 tolist = filtd + 1;
14203
14204 li = listitem_alloc();
14205 if (li == NULL)
14206 {
14207 vim_free(s);
14208 break;
14209 }
14210 li->li_tv.v_type = VAR_STRING;
14211 li->li_tv.v_lock = 0;
14212 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014213 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014214
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014215 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014216 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014217 if (readlen <= 0)
14218 break;
14219 }
14220 else if (buf[filtd] == NUL)
14221 buf[filtd] = '\n';
14222 }
14223 if (readlen <= 0)
14224 break;
14225
14226 if (tolist == 0)
14227 {
14228 /* "buf" is full, need to move text to an allocated buffer */
14229 if (prev == NULL)
14230 {
14231 prev = vim_strnsave(buf, buflen);
14232 prevlen = buflen;
14233 }
14234 else
14235 {
14236 s = alloc((unsigned)(prevlen + buflen));
14237 if (s != NULL)
14238 {
14239 mch_memmove(s, prev, prevlen);
14240 mch_memmove(s + prevlen, buf, buflen);
14241 vim_free(prev);
14242 prev = s;
14243 prevlen += buflen;
14244 }
14245 }
14246 filtd = 0;
14247 }
14248 else
14249 {
14250 mch_memmove(buf, buf + tolist, buflen - tolist);
14251 filtd -= tolist;
14252 }
14253 }
14254
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014255 /*
14256 * For a negative line count use only the lines at the end of the file,
14257 * free the rest.
14258 */
14259 if (maxline < 0)
14260 while (cnt > -maxline)
14261 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014262 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014263 --cnt;
14264 }
14265
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014266 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014267 fclose(fd);
14268}
14269
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014270#if defined(FEAT_RELTIME)
14271static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14272
14273/*
14274 * Convert a List to proftime_T.
14275 * Return FAIL when there is something wrong.
14276 */
14277 static int
14278list2proftime(arg, tm)
14279 typval_T *arg;
14280 proftime_T *tm;
14281{
14282 long n1, n2;
14283 int error = FALSE;
14284
14285 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14286 || arg->vval.v_list->lv_len != 2)
14287 return FAIL;
14288 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14289 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14290# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014291 tm->HighPart = n1;
14292 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014293# else
14294 tm->tv_sec = n1;
14295 tm->tv_usec = n2;
14296# endif
14297 return error ? FAIL : OK;
14298}
14299#endif /* FEAT_RELTIME */
14300
14301/*
14302 * "reltime()" function
14303 */
14304 static void
14305f_reltime(argvars, rettv)
14306 typval_T *argvars;
14307 typval_T *rettv;
14308{
14309#ifdef FEAT_RELTIME
14310 proftime_T res;
14311 proftime_T start;
14312
14313 if (argvars[0].v_type == VAR_UNKNOWN)
14314 {
14315 /* No arguments: get current time. */
14316 profile_start(&res);
14317 }
14318 else if (argvars[1].v_type == VAR_UNKNOWN)
14319 {
14320 if (list2proftime(&argvars[0], &res) == FAIL)
14321 return;
14322 profile_end(&res);
14323 }
14324 else
14325 {
14326 /* Two arguments: compute the difference. */
14327 if (list2proftime(&argvars[0], &start) == FAIL
14328 || list2proftime(&argvars[1], &res) == FAIL)
14329 return;
14330 profile_sub(&res, &start);
14331 }
14332
14333 if (rettv_list_alloc(rettv) == OK)
14334 {
14335 long n1, n2;
14336
14337# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014338 n1 = res.HighPart;
14339 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014340# else
14341 n1 = res.tv_sec;
14342 n2 = res.tv_usec;
14343# endif
14344 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14345 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14346 }
14347#endif
14348}
14349
14350/*
14351 * "reltimestr()" function
14352 */
14353 static void
14354f_reltimestr(argvars, rettv)
14355 typval_T *argvars;
14356 typval_T *rettv;
14357{
14358#ifdef FEAT_RELTIME
14359 proftime_T tm;
14360#endif
14361
14362 rettv->v_type = VAR_STRING;
14363 rettv->vval.v_string = NULL;
14364#ifdef FEAT_RELTIME
14365 if (list2proftime(&argvars[0], &tm) == OK)
14366 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14367#endif
14368}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014369
Bram Moolenaar0d660222005-01-07 21:51:51 +000014370#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14371static void make_connection __ARGS((void));
14372static int check_connection __ARGS((void));
14373
14374 static void
14375make_connection()
14376{
14377 if (X_DISPLAY == NULL
14378# ifdef FEAT_GUI
14379 && !gui.in_use
14380# endif
14381 )
14382 {
14383 x_force_connect = TRUE;
14384 setup_term_clip();
14385 x_force_connect = FALSE;
14386 }
14387}
14388
14389 static int
14390check_connection()
14391{
14392 make_connection();
14393 if (X_DISPLAY == NULL)
14394 {
14395 EMSG(_("E240: No connection to Vim server"));
14396 return FAIL;
14397 }
14398 return OK;
14399}
14400#endif
14401
14402#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014403static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014404
14405 static void
14406remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014407 typval_T *argvars;
14408 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014409 int expr;
14410{
14411 char_u *server_name;
14412 char_u *keys;
14413 char_u *r = NULL;
14414 char_u buf[NUMBUFLEN];
14415# ifdef WIN32
14416 HWND w;
14417# else
14418 Window w;
14419# endif
14420
14421 if (check_restricted() || check_secure())
14422 return;
14423
14424# ifdef FEAT_X11
14425 if (check_connection() == FAIL)
14426 return;
14427# endif
14428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014429 server_name = get_tv_string_chk(&argvars[0]);
14430 if (server_name == NULL)
14431 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014432 keys = get_tv_string_buf(&argvars[1], buf);
14433# ifdef WIN32
14434 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14435# else
14436 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14437 < 0)
14438# endif
14439 {
14440 if (r != NULL)
14441 EMSG(r); /* sending worked but evaluation failed */
14442 else
14443 EMSG2(_("E241: Unable to send to %s"), server_name);
14444 return;
14445 }
14446
14447 rettv->vval.v_string = r;
14448
14449 if (argvars[2].v_type != VAR_UNKNOWN)
14450 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014451 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014452 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014453 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014454
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014455 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014456 v.di_tv.v_type = VAR_STRING;
14457 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014458 idvar = get_tv_string_chk(&argvars[2]);
14459 if (idvar != NULL)
14460 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014461 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014462 }
14463}
14464#endif
14465
14466/*
14467 * "remote_expr()" function
14468 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014469 static void
14470f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014471 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014472 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014473{
14474 rettv->v_type = VAR_STRING;
14475 rettv->vval.v_string = NULL;
14476#ifdef FEAT_CLIENTSERVER
14477 remote_common(argvars, rettv, TRUE);
14478#endif
14479}
14480
14481/*
14482 * "remote_foreground()" function
14483 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014484 static void
14485f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014486 typval_T *argvars UNUSED;
14487 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014488{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014489#ifdef FEAT_CLIENTSERVER
14490# ifdef WIN32
14491 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014492 {
14493 char_u *server_name = get_tv_string_chk(&argvars[0]);
14494
14495 if (server_name != NULL)
14496 serverForeground(server_name);
14497 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014498# else
14499 /* Send a foreground() expression to the server. */
14500 argvars[1].v_type = VAR_STRING;
14501 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14502 argvars[2].v_type = VAR_UNKNOWN;
14503 remote_common(argvars, rettv, TRUE);
14504 vim_free(argvars[1].vval.v_string);
14505# endif
14506#endif
14507}
14508
Bram Moolenaar0d660222005-01-07 21:51:51 +000014509 static void
14510f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014511 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014512 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014513{
14514#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014515 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014516 char_u *s = NULL;
14517# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014518 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014519# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014520 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014521
14522 if (check_restricted() || check_secure())
14523 {
14524 rettv->vval.v_number = -1;
14525 return;
14526 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014527 serverid = get_tv_string_chk(&argvars[0]);
14528 if (serverid == NULL)
14529 {
14530 rettv->vval.v_number = -1;
14531 return; /* type error; errmsg already given */
14532 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014533# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014534 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014535 if (n == 0)
14536 rettv->vval.v_number = -1;
14537 else
14538 {
14539 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14540 rettv->vval.v_number = (s != NULL);
14541 }
14542# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014543 if (check_connection() == FAIL)
14544 return;
14545
14546 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014547 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014548# endif
14549
14550 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014552 char_u *retvar;
14553
Bram Moolenaar33570922005-01-25 22:26:29 +000014554 v.di_tv.v_type = VAR_STRING;
14555 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014556 retvar = get_tv_string_chk(&argvars[1]);
14557 if (retvar != NULL)
14558 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014559 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014560 }
14561#else
14562 rettv->vval.v_number = -1;
14563#endif
14564}
14565
Bram Moolenaar0d660222005-01-07 21:51:51 +000014566 static void
14567f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014568 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014569 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570{
14571 char_u *r = NULL;
14572
14573#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014574 char_u *serverid = get_tv_string_chk(&argvars[0]);
14575
14576 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014577 {
14578# ifdef WIN32
14579 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014580 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014582 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014583 if (n != 0)
14584 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14585 if (r == NULL)
14586# else
14587 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014588 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014589# endif
14590 EMSG(_("E277: Unable to read a server reply"));
14591 }
14592#endif
14593 rettv->v_type = VAR_STRING;
14594 rettv->vval.v_string = r;
14595}
14596
14597/*
14598 * "remote_send()" function
14599 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014600 static void
14601f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014602 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014603 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604{
14605 rettv->v_type = VAR_STRING;
14606 rettv->vval.v_string = NULL;
14607#ifdef FEAT_CLIENTSERVER
14608 remote_common(argvars, rettv, FALSE);
14609#endif
14610}
14611
14612/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014613 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014614 */
14615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014616f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014617 typval_T *argvars;
14618 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014619{
Bram Moolenaar33570922005-01-25 22:26:29 +000014620 list_T *l;
14621 listitem_T *item, *item2;
14622 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014623 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014624 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014625 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014626 dict_T *d;
14627 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014628
Bram Moolenaar8c711452005-01-14 21:53:12 +000014629 if (argvars[0].v_type == VAR_DICT)
14630 {
14631 if (argvars[2].v_type != VAR_UNKNOWN)
14632 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014633 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014634 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014635 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 key = get_tv_string_chk(&argvars[1]);
14637 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014638 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014639 di = dict_find(d, key, -1);
14640 if (di == NULL)
14641 EMSG2(_(e_dictkey), key);
14642 else
14643 {
14644 *rettv = di->di_tv;
14645 init_tv(&di->di_tv);
14646 dictitem_remove(d, di);
14647 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014648 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014649 }
14650 }
14651 else if (argvars[0].v_type != VAR_LIST)
14652 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014653 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014654 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 int error = FALSE;
14657
14658 idx = get_tv_number_chk(&argvars[1], &error);
14659 if (error)
14660 ; /* type error: do nothing, errmsg already given */
14661 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014662 EMSGN(_(e_listidx), idx);
14663 else
14664 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014665 if (argvars[2].v_type == VAR_UNKNOWN)
14666 {
14667 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014668 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014669 *rettv = item->li_tv;
14670 vim_free(item);
14671 }
14672 else
14673 {
14674 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014675 end = get_tv_number_chk(&argvars[2], &error);
14676 if (error)
14677 ; /* type error: do nothing */
14678 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014679 EMSGN(_(e_listidx), end);
14680 else
14681 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014682 int cnt = 0;
14683
14684 for (li = item; li != NULL; li = li->li_next)
14685 {
14686 ++cnt;
14687 if (li == item2)
14688 break;
14689 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014690 if (li == NULL) /* didn't find "item2" after "item" */
14691 EMSG(_(e_invrange));
14692 else
14693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014694 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014695 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014696 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014697 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014698 l->lv_first = item;
14699 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014700 item->li_prev = NULL;
14701 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014702 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014703 }
14704 }
14705 }
14706 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014707 }
14708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709}
14710
14711/*
14712 * "rename({from}, {to})" function
14713 */
14714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014715f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014716 typval_T *argvars;
14717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718{
14719 char_u buf[NUMBUFLEN];
14720
14721 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014722 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014724 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14725 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726}
14727
14728/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014729 * "repeat()" function
14730 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014731 static void
14732f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014733 typval_T *argvars;
14734 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014735{
14736 char_u *p;
14737 int n;
14738 int slen;
14739 int len;
14740 char_u *r;
14741 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014742
14743 n = get_tv_number(&argvars[1]);
14744 if (argvars[0].v_type == VAR_LIST)
14745 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014746 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014747 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014748 if (list_extend(rettv->vval.v_list,
14749 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014750 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014751 }
14752 else
14753 {
14754 p = get_tv_string(&argvars[0]);
14755 rettv->v_type = VAR_STRING;
14756 rettv->vval.v_string = NULL;
14757
14758 slen = (int)STRLEN(p);
14759 len = slen * n;
14760 if (len <= 0)
14761 return;
14762
14763 r = alloc(len + 1);
14764 if (r != NULL)
14765 {
14766 for (i = 0; i < n; i++)
14767 mch_memmove(r + i * slen, p, (size_t)slen);
14768 r[len] = NUL;
14769 }
14770
14771 rettv->vval.v_string = r;
14772 }
14773}
14774
14775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776 * "resolve()" function
14777 */
14778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014779f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014780 typval_T *argvars;
14781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782{
14783 char_u *p;
14784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014785 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786#ifdef FEAT_SHORTCUT
14787 {
14788 char_u *v = NULL;
14789
14790 v = mch_resolve_shortcut(p);
14791 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014792 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014794 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 }
14796#else
14797# ifdef HAVE_READLINK
14798 {
14799 char_u buf[MAXPATHL + 1];
14800 char_u *cpy;
14801 int len;
14802 char_u *remain = NULL;
14803 char_u *q;
14804 int is_relative_to_current = FALSE;
14805 int has_trailing_pathsep = FALSE;
14806 int limit = 100;
14807
14808 p = vim_strsave(p);
14809
14810 if (p[0] == '.' && (vim_ispathsep(p[1])
14811 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14812 is_relative_to_current = TRUE;
14813
14814 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014815 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 has_trailing_pathsep = TRUE;
14817
14818 q = getnextcomp(p);
14819 if (*q != NUL)
14820 {
14821 /* Separate the first path component in "p", and keep the
14822 * remainder (beginning with the path separator). */
14823 remain = vim_strsave(q - 1);
14824 q[-1] = NUL;
14825 }
14826
14827 for (;;)
14828 {
14829 for (;;)
14830 {
14831 len = readlink((char *)p, (char *)buf, MAXPATHL);
14832 if (len <= 0)
14833 break;
14834 buf[len] = NUL;
14835
14836 if (limit-- == 0)
14837 {
14838 vim_free(p);
14839 vim_free(remain);
14840 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014841 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842 goto fail;
14843 }
14844
14845 /* Ensure that the result will have a trailing path separator
14846 * if the argument has one. */
14847 if (remain == NULL && has_trailing_pathsep)
14848 add_pathsep(buf);
14849
14850 /* Separate the first path component in the link value and
14851 * concatenate the remainders. */
14852 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14853 if (*q != NUL)
14854 {
14855 if (remain == NULL)
14856 remain = vim_strsave(q - 1);
14857 else
14858 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014859 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860 if (cpy != NULL)
14861 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 vim_free(remain);
14863 remain = cpy;
14864 }
14865 }
14866 q[-1] = NUL;
14867 }
14868
14869 q = gettail(p);
14870 if (q > p && *q == NUL)
14871 {
14872 /* Ignore trailing path separator. */
14873 q[-1] = NUL;
14874 q = gettail(p);
14875 }
14876 if (q > p && !mch_isFullName(buf))
14877 {
14878 /* symlink is relative to directory of argument */
14879 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14880 if (cpy != NULL)
14881 {
14882 STRCPY(cpy, p);
14883 STRCPY(gettail(cpy), buf);
14884 vim_free(p);
14885 p = cpy;
14886 }
14887 }
14888 else
14889 {
14890 vim_free(p);
14891 p = vim_strsave(buf);
14892 }
14893 }
14894
14895 if (remain == NULL)
14896 break;
14897
14898 /* Append the first path component of "remain" to "p". */
14899 q = getnextcomp(remain + 1);
14900 len = q - remain - (*q != NUL);
14901 cpy = vim_strnsave(p, STRLEN(p) + len);
14902 if (cpy != NULL)
14903 {
14904 STRNCAT(cpy, remain, len);
14905 vim_free(p);
14906 p = cpy;
14907 }
14908 /* Shorten "remain". */
14909 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014910 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 else
14912 {
14913 vim_free(remain);
14914 remain = NULL;
14915 }
14916 }
14917
14918 /* If the result is a relative path name, make it explicitly relative to
14919 * the current directory if and only if the argument had this form. */
14920 if (!vim_ispathsep(*p))
14921 {
14922 if (is_relative_to_current
14923 && *p != NUL
14924 && !(p[0] == '.'
14925 && (p[1] == NUL
14926 || vim_ispathsep(p[1])
14927 || (p[1] == '.'
14928 && (p[2] == NUL
14929 || vim_ispathsep(p[2]))))))
14930 {
14931 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014932 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 if (cpy != NULL)
14934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935 vim_free(p);
14936 p = cpy;
14937 }
14938 }
14939 else if (!is_relative_to_current)
14940 {
14941 /* Strip leading "./". */
14942 q = p;
14943 while (q[0] == '.' && vim_ispathsep(q[1]))
14944 q += 2;
14945 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014946 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947 }
14948 }
14949
14950 /* Ensure that the result will have no trailing path separator
14951 * if the argument had none. But keep "/" or "//". */
14952 if (!has_trailing_pathsep)
14953 {
14954 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014955 if (after_pathsep(p, q))
14956 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957 }
14958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014959 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 }
14961# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014962 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963# endif
14964#endif
14965
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014966 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967
14968#ifdef HAVE_READLINK
14969fail:
14970#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014971 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972}
14973
14974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014975 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 */
14977 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014978f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014979 typval_T *argvars;
14980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981{
Bram Moolenaar33570922005-01-25 22:26:29 +000014982 list_T *l;
14983 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984
Bram Moolenaar0d660222005-01-07 21:51:51 +000014985 if (argvars[0].v_type != VAR_LIST)
14986 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014987 else if ((l = argvars[0].vval.v_list) != NULL
14988 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000014989 {
14990 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014991 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014992 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014993 while (li != NULL)
14994 {
14995 ni = li->li_prev;
14996 list_append(l, li);
14997 li = ni;
14998 }
14999 rettv->vval.v_list = l;
15000 rettv->v_type = VAR_LIST;
15001 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015002 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004}
15005
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015006#define SP_NOMOVE 0x01 /* don't move cursor */
15007#define SP_REPEAT 0x02 /* repeat to find outer pair */
15008#define SP_RETCOUNT 0x04 /* return matchcount */
15009#define SP_SETPCMARK 0x08 /* set previous context mark */
15010#define SP_START 0x10 /* accept match at start position */
15011#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15012#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015013
Bram Moolenaar33570922005-01-25 22:26:29 +000015014static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015015
15016/*
15017 * Get flags for a search function.
15018 * Possibly sets "p_ws".
15019 * Returns BACKWARD, FORWARD or zero (for an error).
15020 */
15021 static int
15022get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015023 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015024 int *flagsp;
15025{
15026 int dir = FORWARD;
15027 char_u *flags;
15028 char_u nbuf[NUMBUFLEN];
15029 int mask;
15030
15031 if (varp->v_type != VAR_UNKNOWN)
15032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015033 flags = get_tv_string_buf_chk(varp, nbuf);
15034 if (flags == NULL)
15035 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015036 while (*flags != NUL)
15037 {
15038 switch (*flags)
15039 {
15040 case 'b': dir = BACKWARD; break;
15041 case 'w': p_ws = TRUE; break;
15042 case 'W': p_ws = FALSE; break;
15043 default: mask = 0;
15044 if (flagsp != NULL)
15045 switch (*flags)
15046 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015047 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015048 case 'e': mask = SP_END; break;
15049 case 'm': mask = SP_RETCOUNT; break;
15050 case 'n': mask = SP_NOMOVE; break;
15051 case 'p': mask = SP_SUBPAT; break;
15052 case 'r': mask = SP_REPEAT; break;
15053 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015054 }
15055 if (mask == 0)
15056 {
15057 EMSG2(_(e_invarg2), flags);
15058 dir = 0;
15059 }
15060 else
15061 *flagsp |= mask;
15062 }
15063 if (dir == 0)
15064 break;
15065 ++flags;
15066 }
15067 }
15068 return dir;
15069}
15070
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015072 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015074 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015075search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015076 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015077 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015078 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015080 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081 char_u *pat;
15082 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015083 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084 int save_p_ws = p_ws;
15085 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015086 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015087 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015088 proftime_T tm;
15089#ifdef FEAT_RELTIME
15090 long time_limit = 0;
15091#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015092 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015093 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015095 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015096 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015097 if (dir == 0)
15098 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015099 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015100 if (flags & SP_START)
15101 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015102 if (flags & SP_END)
15103 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015104
Bram Moolenaar76929292008-01-06 19:07:36 +000015105 /* Optional arguments: line number to stop searching and timeout. */
15106 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015107 {
15108 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15109 if (lnum_stop < 0)
15110 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015111#ifdef FEAT_RELTIME
15112 if (argvars[3].v_type != VAR_UNKNOWN)
15113 {
15114 time_limit = get_tv_number_chk(&argvars[3], NULL);
15115 if (time_limit < 0)
15116 goto theend;
15117 }
15118#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015119 }
15120
Bram Moolenaar76929292008-01-06 19:07:36 +000015121#ifdef FEAT_RELTIME
15122 /* Set the time limit, if there is one. */
15123 profile_setlimit(time_limit, &tm);
15124#endif
15125
Bram Moolenaar231334e2005-07-25 20:46:57 +000015126 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015127 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015128 * Check to make sure only those flags are set.
15129 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15130 * flags cannot be set. Check for that condition also.
15131 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015132 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015133 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015134 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015136 goto theend;
15137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015139 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015140 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015141 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015142 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015144 if (flags & SP_SUBPAT)
15145 retval = subpatnum;
15146 else
15147 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015148 if (flags & SP_SETPCMARK)
15149 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015151 if (match_pos != NULL)
15152 {
15153 /* Store the match cursor position */
15154 match_pos->lnum = pos.lnum;
15155 match_pos->col = pos.col + 1;
15156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 /* "/$" will put the cursor after the end of the line, may need to
15158 * correct that here */
15159 check_cursor();
15160 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015161
15162 /* If 'n' flag is used: restore cursor position. */
15163 if (flags & SP_NOMOVE)
15164 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015165 else
15166 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015167theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015169
15170 return retval;
15171}
15172
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015173#ifdef FEAT_FLOAT
15174/*
15175 * "round({float})" function
15176 */
15177 static void
15178f_round(argvars, rettv)
15179 typval_T *argvars;
15180 typval_T *rettv;
15181{
15182 float_T f;
15183
15184 rettv->v_type = VAR_FLOAT;
15185 if (get_float_arg(argvars, &f) == OK)
15186 /* round() is not in C90, use ceil() or floor() instead. */
15187 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15188 else
15189 rettv->vval.v_float = 0.0;
15190}
15191#endif
15192
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015193/*
15194 * "search()" function
15195 */
15196 static void
15197f_search(argvars, rettv)
15198 typval_T *argvars;
15199 typval_T *rettv;
15200{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015201 int flags = 0;
15202
15203 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204}
15205
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015207 * "searchdecl()" function
15208 */
15209 static void
15210f_searchdecl(argvars, rettv)
15211 typval_T *argvars;
15212 typval_T *rettv;
15213{
15214 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015215 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015216 int error = FALSE;
15217 char_u *name;
15218
15219 rettv->vval.v_number = 1; /* default: FAIL */
15220
15221 name = get_tv_string_chk(&argvars[0]);
15222 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015223 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015224 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015225 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15226 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15227 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015228 if (!error && name != NULL)
15229 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015230 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015231}
15232
15233/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015234 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015236 static int
15237searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015238 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015239 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240{
15241 char_u *spat, *mpat, *epat;
15242 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015243 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244 int dir;
15245 int flags = 0;
15246 char_u nbuf1[NUMBUFLEN];
15247 char_u nbuf2[NUMBUFLEN];
15248 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015249 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015250 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015251 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015254 spat = get_tv_string_chk(&argvars[0]);
15255 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15256 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15257 if (spat == NULL || mpat == NULL || epat == NULL)
15258 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260 /* Handle the optional fourth argument: flags */
15261 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015262 if (dir == 0)
15263 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015264
15265 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015266 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15267 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015268 if ((flags & (SP_END | SP_SUBPAT)) != 0
15269 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015270 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015271 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015272 goto theend;
15273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015275 /* Using 'r' implies 'W', otherwise it doesn't work. */
15276 if (flags & SP_REPEAT)
15277 p_ws = FALSE;
15278
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015279 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015280 if (argvars[3].v_type == VAR_UNKNOWN
15281 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282 skip = (char_u *)"";
15283 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015285 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015286 if (argvars[5].v_type != VAR_UNKNOWN)
15287 {
15288 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15289 if (lnum_stop < 0)
15290 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015291#ifdef FEAT_RELTIME
15292 if (argvars[6].v_type != VAR_UNKNOWN)
15293 {
15294 time_limit = get_tv_number_chk(&argvars[6], NULL);
15295 if (time_limit < 0)
15296 goto theend;
15297 }
15298#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015299 }
15300 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015301 if (skip == NULL)
15302 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015304 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015305 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015306
15307theend:
15308 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015309
15310 return retval;
15311}
15312
15313/*
15314 * "searchpair()" function
15315 */
15316 static void
15317f_searchpair(argvars, rettv)
15318 typval_T *argvars;
15319 typval_T *rettv;
15320{
15321 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15322}
15323
15324/*
15325 * "searchpairpos()" function
15326 */
15327 static void
15328f_searchpairpos(argvars, rettv)
15329 typval_T *argvars;
15330 typval_T *rettv;
15331{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015332 pos_T match_pos;
15333 int lnum = 0;
15334 int col = 0;
15335
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015336 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015337 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015338
15339 if (searchpair_cmn(argvars, &match_pos) > 0)
15340 {
15341 lnum = match_pos.lnum;
15342 col = match_pos.col;
15343 }
15344
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015345 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15346 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015347}
15348
15349/*
15350 * Search for a start/middle/end thing.
15351 * Used by searchpair(), see its documentation for the details.
15352 * Returns 0 or -1 for no match,
15353 */
15354 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015355do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15356 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015357 char_u *spat; /* start pattern */
15358 char_u *mpat; /* middle pattern */
15359 char_u *epat; /* end pattern */
15360 int dir; /* BACKWARD or FORWARD */
15361 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015362 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015363 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015364 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015365 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015366{
15367 char_u *save_cpo;
15368 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15369 long retval = 0;
15370 pos_T pos;
15371 pos_T firstpos;
15372 pos_T foundpos;
15373 pos_T save_cursor;
15374 pos_T save_pos;
15375 int n;
15376 int r;
15377 int nest = 1;
15378 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015379 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015380 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015381
15382 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15383 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015384 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015385
Bram Moolenaar76929292008-01-06 19:07:36 +000015386#ifdef FEAT_RELTIME
15387 /* Set the time limit, if there is one. */
15388 profile_setlimit(time_limit, &tm);
15389#endif
15390
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015391 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15392 * start/middle/end (pat3, for the top pair). */
15393 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15394 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15395 if (pat2 == NULL || pat3 == NULL)
15396 goto theend;
15397 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15398 if (*mpat == NUL)
15399 STRCPY(pat3, pat2);
15400 else
15401 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15402 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015403 if (flags & SP_START)
15404 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015405
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406 save_cursor = curwin->w_cursor;
15407 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015408 clearpos(&firstpos);
15409 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 pat = pat3;
15411 for (;;)
15412 {
15413 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015414 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15416 /* didn't find it or found the first match again: FAIL */
15417 break;
15418
15419 if (firstpos.lnum == 0)
15420 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015421 if (equalpos(pos, foundpos))
15422 {
15423 /* Found the same position again. Can happen with a pattern that
15424 * has "\zs" at the end and searching backwards. Advance one
15425 * character and try again. */
15426 if (dir == BACKWARD)
15427 decl(&pos);
15428 else
15429 incl(&pos);
15430 }
15431 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015433 /* clear the start flag to avoid getting stuck here */
15434 options &= ~SEARCH_START;
15435
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436 /* If the skip pattern matches, ignore this match. */
15437 if (*skip != NUL)
15438 {
15439 save_pos = curwin->w_cursor;
15440 curwin->w_cursor = pos;
15441 r = eval_to_bool(skip, &err, NULL, FALSE);
15442 curwin->w_cursor = save_pos;
15443 if (err)
15444 {
15445 /* Evaluating {skip} caused an error, break here. */
15446 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015447 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 break;
15449 }
15450 if (r)
15451 continue;
15452 }
15453
15454 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15455 {
15456 /* Found end when searching backwards or start when searching
15457 * forward: nested pair. */
15458 ++nest;
15459 pat = pat2; /* nested, don't search for middle */
15460 }
15461 else
15462 {
15463 /* Found end when searching forward or start when searching
15464 * backward: end of (nested) pair; or found middle in outer pair. */
15465 if (--nest == 1)
15466 pat = pat3; /* outer level, search for middle */
15467 }
15468
15469 if (nest == 0)
15470 {
15471 /* Found the match: return matchcount or line number. */
15472 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015473 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015475 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015476 if (flags & SP_SETPCMARK)
15477 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478 curwin->w_cursor = pos;
15479 if (!(flags & SP_REPEAT))
15480 break;
15481 nest = 1; /* search for next unmatched */
15482 }
15483 }
15484
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015485 if (match_pos != NULL)
15486 {
15487 /* Store the match cursor position */
15488 match_pos->lnum = curwin->w_cursor.lnum;
15489 match_pos->col = curwin->w_cursor.col + 1;
15490 }
15491
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015493 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 curwin->w_cursor = save_cursor;
15495
15496theend:
15497 vim_free(pat2);
15498 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015499 if (p_cpo == empty_option)
15500 p_cpo = save_cpo;
15501 else
15502 /* Darn, evaluating the {skip} expression changed the value. */
15503 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015504
15505 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506}
15507
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015508/*
15509 * "searchpos()" function
15510 */
15511 static void
15512f_searchpos(argvars, rettv)
15513 typval_T *argvars;
15514 typval_T *rettv;
15515{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015516 pos_T match_pos;
15517 int lnum = 0;
15518 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015519 int n;
15520 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015521
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015522 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015523 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015524
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015525 n = search_cmn(argvars, &match_pos, &flags);
15526 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015527 {
15528 lnum = match_pos.lnum;
15529 col = match_pos.col;
15530 }
15531
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015532 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15533 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015534 if (flags & SP_SUBPAT)
15535 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015536}
15537
15538
Bram Moolenaar0d660222005-01-07 21:51:51 +000015539 static void
15540f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015541 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015544#ifdef FEAT_CLIENTSERVER
15545 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015546 char_u *server = get_tv_string_chk(&argvars[0]);
15547 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548
Bram Moolenaar0d660222005-01-07 21:51:51 +000015549 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015550 if (server == NULL || reply == NULL)
15551 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015552 if (check_restricted() || check_secure())
15553 return;
15554# ifdef FEAT_X11
15555 if (check_connection() == FAIL)
15556 return;
15557# endif
15558
15559 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015561 EMSG(_("E258: Unable to send to client"));
15562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015564 rettv->vval.v_number = 0;
15565#else
15566 rettv->vval.v_number = -1;
15567#endif
15568}
15569
Bram Moolenaar0d660222005-01-07 21:51:51 +000015570 static void
15571f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015572 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015573 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015574{
15575 char_u *r = NULL;
15576
15577#ifdef FEAT_CLIENTSERVER
15578# ifdef WIN32
15579 r = serverGetVimNames();
15580# else
15581 make_connection();
15582 if (X_DISPLAY != NULL)
15583 r = serverGetVimNames(X_DISPLAY);
15584# endif
15585#endif
15586 rettv->v_type = VAR_STRING;
15587 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588}
15589
15590/*
15591 * "setbufvar()" function
15592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015594f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015595 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015596 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597{
15598 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015601 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 char_u nbuf[NUMBUFLEN];
15603
15604 if (check_restricted() || check_secure())
15605 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015606 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15607 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015608 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609 varp = &argvars[2];
15610
15611 if (buf != NULL && varname != NULL && varp != NULL)
15612 {
15613 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615
15616 if (*varname == '&')
15617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015618 long numval;
15619 char_u *strval;
15620 int error = FALSE;
15621
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015623 numval = get_tv_number_chk(varp, &error);
15624 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015625 if (!error && strval != NULL)
15626 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 }
15628 else
15629 {
15630 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15631 if (bufvarname != NULL)
15632 {
15633 STRCPY(bufvarname, "b:");
15634 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015635 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636 vim_free(bufvarname);
15637 }
15638 }
15639
15640 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643}
15644
15645/*
15646 * "setcmdpos()" function
15647 */
15648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015649f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015650 typval_T *argvars;
15651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015653 int pos = (int)get_tv_number(&argvars[0]) - 1;
15654
15655 if (pos >= 0)
15656 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657}
15658
15659/*
15660 * "setline()" function
15661 */
15662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015663f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015664 typval_T *argvars;
15665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666{
15667 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015668 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015669 list_T *l = NULL;
15670 listitem_T *li = NULL;
15671 long added = 0;
15672 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015674 lnum = get_tv_lnum(&argvars[0]);
15675 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015677 l = argvars[1].vval.v_list;
15678 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015680 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015681 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015682
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015683 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015684 for (;;)
15685 {
15686 if (l != NULL)
15687 {
15688 /* list argument, get next string */
15689 if (li == NULL)
15690 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015691 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015692 li = li->li_next;
15693 }
15694
15695 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015696 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015697 break;
15698 if (lnum <= curbuf->b_ml.ml_line_count)
15699 {
15700 /* existing line, replace it */
15701 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15702 {
15703 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015704 if (lnum == curwin->w_cursor.lnum)
15705 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015706 rettv->vval.v_number = 0; /* OK */
15707 }
15708 }
15709 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15710 {
15711 /* lnum is one past the last line, append the line */
15712 ++added;
15713 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15714 rettv->vval.v_number = 0; /* OK */
15715 }
15716
15717 if (l == NULL) /* only one string argument */
15718 break;
15719 ++lnum;
15720 }
15721
15722 if (added > 0)
15723 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724}
15725
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015726static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15727
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015729 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015730 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015731 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015732set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015733 win_T *wp UNUSED;
15734 typval_T *list_arg UNUSED;
15735 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015736 typval_T *rettv;
15737{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015738#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015739 char_u *act;
15740 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015741#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015742
Bram Moolenaar2641f772005-03-25 21:58:17 +000015743 rettv->vval.v_number = -1;
15744
15745#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015746 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015747 EMSG(_(e_listreq));
15748 else
15749 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015750 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015751
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015752 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015753 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015754 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015755 if (act == NULL)
15756 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015757 if (*act == 'a' || *act == 'r')
15758 action = *act;
15759 }
15760
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015761 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015762 rettv->vval.v_number = 0;
15763 }
15764#endif
15765}
15766
15767/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015768 * "setloclist()" function
15769 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015770 static void
15771f_setloclist(argvars, rettv)
15772 typval_T *argvars;
15773 typval_T *rettv;
15774{
15775 win_T *win;
15776
15777 rettv->vval.v_number = -1;
15778
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015779 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015780 if (win != NULL)
15781 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15782}
15783
15784/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015785 * "setmatches()" function
15786 */
15787 static void
15788f_setmatches(argvars, rettv)
15789 typval_T *argvars;
15790 typval_T *rettv;
15791{
15792#ifdef FEAT_SEARCH_EXTRA
15793 list_T *l;
15794 listitem_T *li;
15795 dict_T *d;
15796
15797 rettv->vval.v_number = -1;
15798 if (argvars[0].v_type != VAR_LIST)
15799 {
15800 EMSG(_(e_listreq));
15801 return;
15802 }
15803 if ((l = argvars[0].vval.v_list) != NULL)
15804 {
15805
15806 /* To some extent make sure that we are dealing with a list from
15807 * "getmatches()". */
15808 li = l->lv_first;
15809 while (li != NULL)
15810 {
15811 if (li->li_tv.v_type != VAR_DICT
15812 || (d = li->li_tv.vval.v_dict) == NULL)
15813 {
15814 EMSG(_(e_invarg));
15815 return;
15816 }
15817 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15818 && dict_find(d, (char_u *)"pattern", -1) != NULL
15819 && dict_find(d, (char_u *)"priority", -1) != NULL
15820 && dict_find(d, (char_u *)"id", -1) != NULL))
15821 {
15822 EMSG(_(e_invarg));
15823 return;
15824 }
15825 li = li->li_next;
15826 }
15827
15828 clear_matches(curwin);
15829 li = l->lv_first;
15830 while (li != NULL)
15831 {
15832 d = li->li_tv.vval.v_dict;
15833 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15834 get_dict_string(d, (char_u *)"pattern", FALSE),
15835 (int)get_dict_number(d, (char_u *)"priority"),
15836 (int)get_dict_number(d, (char_u *)"id"));
15837 li = li->li_next;
15838 }
15839 rettv->vval.v_number = 0;
15840 }
15841#endif
15842}
15843
15844/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015845 * "setpos()" function
15846 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015847 static void
15848f_setpos(argvars, rettv)
15849 typval_T *argvars;
15850 typval_T *rettv;
15851{
15852 pos_T pos;
15853 int fnum;
15854 char_u *name;
15855
Bram Moolenaar08250432008-02-13 11:42:46 +000015856 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015857 name = get_tv_string_chk(argvars);
15858 if (name != NULL)
15859 {
15860 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15861 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015862 if (--pos.col < 0)
15863 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015864 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015865 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015866 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015867 if (fnum == curbuf->b_fnum)
15868 {
15869 curwin->w_cursor = pos;
15870 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015871 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015872 }
15873 else
15874 EMSG(_(e_invarg));
15875 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015876 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15877 {
15878 /* set mark */
15879 if (setmark_pos(name[1], &pos, fnum) == OK)
15880 rettv->vval.v_number = 0;
15881 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015882 else
15883 EMSG(_(e_invarg));
15884 }
15885 }
15886}
15887
15888/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015889 * "setqflist()" function
15890 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015891 static void
15892f_setqflist(argvars, rettv)
15893 typval_T *argvars;
15894 typval_T *rettv;
15895{
15896 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15897}
15898
15899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 * "setreg()" function
15901 */
15902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015903f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015904 typval_T *argvars;
15905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906{
15907 int regname;
15908 char_u *strregname;
15909 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015910 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911 int append;
15912 char_u yank_type;
15913 long block_len;
15914
15915 block_len = -1;
15916 yank_type = MAUTO;
15917 append = FALSE;
15918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015919 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015920 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015922 if (strregname == NULL)
15923 return; /* type error; errmsg already given */
15924 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925 if (regname == 0 || regname == '@')
15926 regname = '"';
15927 else if (regname == '=')
15928 return;
15929
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015930 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015932 stropt = get_tv_string_chk(&argvars[2]);
15933 if (stropt == NULL)
15934 return; /* type error */
15935 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015936 switch (*stropt)
15937 {
15938 case 'a': case 'A': /* append */
15939 append = TRUE;
15940 break;
15941 case 'v': case 'c': /* character-wise selection */
15942 yank_type = MCHAR;
15943 break;
15944 case 'V': case 'l': /* line-wise selection */
15945 yank_type = MLINE;
15946 break;
15947#ifdef FEAT_VISUAL
15948 case 'b': case Ctrl_V: /* block-wise selection */
15949 yank_type = MBLOCK;
15950 if (VIM_ISDIGIT(stropt[1]))
15951 {
15952 ++stropt;
15953 block_len = getdigits(&stropt) - 1;
15954 --stropt;
15955 }
15956 break;
15957#endif
15958 }
15959 }
15960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015961 strval = get_tv_string_chk(&argvars[1]);
15962 if (strval != NULL)
15963 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015965 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966}
15967
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015968/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020015969 * "settabvar()" function
15970 */
15971 static void
15972f_settabvar(argvars, rettv)
15973 typval_T *argvars;
15974 typval_T *rettv;
15975{
15976 tabpage_T *save_curtab;
15977 char_u *varname, *tabvarname;
15978 typval_T *varp;
15979 tabpage_T *tp;
15980
15981 rettv->vval.v_number = 0;
15982
15983 if (check_restricted() || check_secure())
15984 return;
15985
15986 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
15987 varname = get_tv_string_chk(&argvars[1]);
15988 varp = &argvars[2];
15989
15990 if (tp != NULL && varname != NULL && varp != NULL)
15991 {
15992 save_curtab = curtab;
15993 goto_tabpage_tp(tp);
15994
15995 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
15996 if (tabvarname != NULL)
15997 {
15998 STRCPY(tabvarname, "t:");
15999 STRCPY(tabvarname + 2, varname);
16000 set_var(tabvarname, varp, TRUE);
16001 vim_free(tabvarname);
16002 }
16003
16004 /* Restore current tabpage */
16005 if (valid_tabpage(save_curtab))
16006 goto_tabpage_tp(save_curtab);
16007 }
16008}
16009
16010/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016011 * "settabwinvar()" function
16012 */
16013 static void
16014f_settabwinvar(argvars, rettv)
16015 typval_T *argvars;
16016 typval_T *rettv;
16017{
16018 setwinvar(argvars, rettv, 1);
16019}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020
16021/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016022 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016025f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016026 typval_T *argvars;
16027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016029 setwinvar(argvars, rettv, 0);
16030}
16031
16032/*
16033 * "setwinvar()" and "settabwinvar()" functions
16034 */
16035 static void
16036setwinvar(argvars, rettv, off)
16037 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016038 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016039 int off;
16040{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 win_T *win;
16042#ifdef FEAT_WINDOWS
16043 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016044 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045#endif
16046 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016047 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016049 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050
16051 if (check_restricted() || check_secure())
16052 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016053
16054#ifdef FEAT_WINDOWS
16055 if (off == 1)
16056 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16057 else
16058 tp = curtab;
16059#endif
16060 win = find_win_by_nr(&argvars[off], tp);
16061 varname = get_tv_string_chk(&argvars[off + 1]);
16062 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063
16064 if (win != NULL && varname != NULL && varp != NULL)
16065 {
16066#ifdef FEAT_WINDOWS
16067 /* set curwin to be our win, temporarily */
16068 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016069 save_curtab = curtab;
16070 goto_tabpage_tp(tp);
16071 if (!win_valid(win))
16072 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073 curwin = win;
16074 curbuf = curwin->w_buffer;
16075#endif
16076
16077 if (*varname == '&')
16078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016079 long numval;
16080 char_u *strval;
16081 int error = FALSE;
16082
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016084 numval = get_tv_number_chk(varp, &error);
16085 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016086 if (!error && strval != NULL)
16087 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088 }
16089 else
16090 {
16091 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16092 if (winvarname != NULL)
16093 {
16094 STRCPY(winvarname, "w:");
16095 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016096 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097 vim_free(winvarname);
16098 }
16099 }
16100
16101#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016102 /* Restore current tabpage and window, if still valid (autocomands can
16103 * make them invalid). */
16104 if (valid_tabpage(save_curtab))
16105 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016106 if (win_valid(save_curwin))
16107 {
16108 curwin = save_curwin;
16109 curbuf = curwin->w_buffer;
16110 }
16111#endif
16112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113}
16114
16115/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016116 * "shellescape({string})" function
16117 */
16118 static void
16119f_shellescape(argvars, rettv)
16120 typval_T *argvars;
16121 typval_T *rettv;
16122{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016123 rettv->vval.v_string = vim_strsave_shellescape(
16124 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016125 rettv->v_type = VAR_STRING;
16126}
16127
16128/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016129 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130 */
16131 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016132f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016133 typval_T *argvars;
16134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016136 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137
Bram Moolenaar0d660222005-01-07 21:51:51 +000016138 p = get_tv_string(&argvars[0]);
16139 rettv->vval.v_string = vim_strsave(p);
16140 simplify_filename(rettv->vval.v_string); /* simplify in place */
16141 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142}
16143
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016144#ifdef FEAT_FLOAT
16145/*
16146 * "sin()" function
16147 */
16148 static void
16149f_sin(argvars, rettv)
16150 typval_T *argvars;
16151 typval_T *rettv;
16152{
16153 float_T f;
16154
16155 rettv->v_type = VAR_FLOAT;
16156 if (get_float_arg(argvars, &f) == OK)
16157 rettv->vval.v_float = sin(f);
16158 else
16159 rettv->vval.v_float = 0.0;
16160}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016161
16162/*
16163 * "sinh()" function
16164 */
16165 static void
16166f_sinh(argvars, rettv)
16167 typval_T *argvars;
16168 typval_T *rettv;
16169{
16170 float_T f;
16171
16172 rettv->v_type = VAR_FLOAT;
16173 if (get_float_arg(argvars, &f) == OK)
16174 rettv->vval.v_float = sinh(f);
16175 else
16176 rettv->vval.v_float = 0.0;
16177}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016178#endif
16179
Bram Moolenaar0d660222005-01-07 21:51:51 +000016180static int
16181#ifdef __BORLANDC__
16182 _RTLENTRYF
16183#endif
16184 item_compare __ARGS((const void *s1, const void *s2));
16185static int
16186#ifdef __BORLANDC__
16187 _RTLENTRYF
16188#endif
16189 item_compare2 __ARGS((const void *s1, const void *s2));
16190
16191static int item_compare_ic;
16192static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016193static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016194#define ITEM_COMPARE_FAIL 999
16195
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016197 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016199 static int
16200#ifdef __BORLANDC__
16201_RTLENTRYF
16202#endif
16203item_compare(s1, s2)
16204 const void *s1;
16205 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016207 char_u *p1, *p2;
16208 char_u *tofree1, *tofree2;
16209 int res;
16210 char_u numbuf1[NUMBUFLEN];
16211 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016213 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16214 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016215 if (p1 == NULL)
16216 p1 = (char_u *)"";
16217 if (p2 == NULL)
16218 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219 if (item_compare_ic)
16220 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016222 res = STRCMP(p1, p2);
16223 vim_free(tofree1);
16224 vim_free(tofree2);
16225 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226}
16227
16228 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016229#ifdef __BORLANDC__
16230_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016232item_compare2(s1, s2)
16233 const void *s1;
16234 const void *s2;
16235{
16236 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016237 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016238 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016239 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016241 /* shortcut after failure in previous call; compare all items equal */
16242 if (item_compare_func_err)
16243 return 0;
16244
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016245 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16246 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016247 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16248 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016249
16250 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016251 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016252 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016253 clear_tv(&argv[0]);
16254 clear_tv(&argv[1]);
16255
16256 if (res == FAIL)
16257 res = ITEM_COMPARE_FAIL;
16258 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016259 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16260 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016261 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016262 clear_tv(&rettv);
16263 return res;
16264}
16265
16266/*
16267 * "sort({list})" function
16268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016270f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016271 typval_T *argvars;
16272 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273{
Bram Moolenaar33570922005-01-25 22:26:29 +000016274 list_T *l;
16275 listitem_T *li;
16276 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016277 long len;
16278 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279
Bram Moolenaar0d660222005-01-07 21:51:51 +000016280 if (argvars[0].v_type != VAR_LIST)
16281 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 else
16283 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016284 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016285 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016286 return;
16287 rettv->vval.v_list = l;
16288 rettv->v_type = VAR_LIST;
16289 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290
Bram Moolenaar0d660222005-01-07 21:51:51 +000016291 len = list_len(l);
16292 if (len <= 1)
16293 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295 item_compare_ic = FALSE;
16296 item_compare_func = NULL;
16297 if (argvars[1].v_type != VAR_UNKNOWN)
16298 {
16299 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016300 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016301 else
16302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016303 int error = FALSE;
16304
16305 i = get_tv_number_chk(&argvars[1], &error);
16306 if (error)
16307 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308 if (i == 1)
16309 item_compare_ic = TRUE;
16310 else
16311 item_compare_func = get_tv_string(&argvars[1]);
16312 }
16313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314
Bram Moolenaar0d660222005-01-07 21:51:51 +000016315 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016316 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016317 if (ptrs == NULL)
16318 return;
16319 i = 0;
16320 for (li = l->lv_first; li != NULL; li = li->li_next)
16321 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016323 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016324 /* test the compare function */
16325 if (item_compare_func != NULL
16326 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16327 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016328 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016330 {
16331 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016332 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016333 item_compare_func == NULL ? item_compare : item_compare2);
16334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016335 if (!item_compare_func_err)
16336 {
16337 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016338 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016339 l->lv_len = 0;
16340 for (i = 0; i < len; ++i)
16341 list_append(l, ptrs[i]);
16342 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016343 }
16344
16345 vim_free(ptrs);
16346 }
16347}
16348
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016349/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016350 * "soundfold({word})" function
16351 */
16352 static void
16353f_soundfold(argvars, rettv)
16354 typval_T *argvars;
16355 typval_T *rettv;
16356{
16357 char_u *s;
16358
16359 rettv->v_type = VAR_STRING;
16360 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016361#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016362 rettv->vval.v_string = eval_soundfold(s);
16363#else
16364 rettv->vval.v_string = vim_strsave(s);
16365#endif
16366}
16367
16368/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016369 * "spellbadword()" function
16370 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016371 static void
16372f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016373 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016374 typval_T *rettv;
16375{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016376 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016377 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016378 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016379
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016380 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016381 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016382
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016383#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016384 if (argvars[0].v_type == VAR_UNKNOWN)
16385 {
16386 /* Find the start and length of the badly spelled word. */
16387 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16388 if (len != 0)
16389 word = ml_get_cursor();
16390 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016391 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016392 {
16393 char_u *str = get_tv_string_chk(&argvars[0]);
16394 int capcol = -1;
16395
16396 if (str != NULL)
16397 {
16398 /* Check the argument for spelling. */
16399 while (*str != NUL)
16400 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016401 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016402 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016403 {
16404 word = str;
16405 break;
16406 }
16407 str += len;
16408 }
16409 }
16410 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016411#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016412
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016413 list_append_string(rettv->vval.v_list, word, len);
16414 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016415 attr == HLF_SPB ? "bad" :
16416 attr == HLF_SPR ? "rare" :
16417 attr == HLF_SPL ? "local" :
16418 attr == HLF_SPC ? "caps" :
16419 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016420}
16421
16422/*
16423 * "spellsuggest()" function
16424 */
16425 static void
16426f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016427 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016428 typval_T *rettv;
16429{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016430#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016431 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016432 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016433 int maxcount;
16434 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016435 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016436 listitem_T *li;
16437 int need_capital = FALSE;
16438#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016439
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016440 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016441 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016442
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016443#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016444 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016445 {
16446 str = get_tv_string(&argvars[0]);
16447 if (argvars[1].v_type != VAR_UNKNOWN)
16448 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016449 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016450 if (maxcount <= 0)
16451 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016452 if (argvars[2].v_type != VAR_UNKNOWN)
16453 {
16454 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16455 if (typeerr)
16456 return;
16457 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016458 }
16459 else
16460 maxcount = 25;
16461
Bram Moolenaar4770d092006-01-12 23:22:24 +000016462 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016463
16464 for (i = 0; i < ga.ga_len; ++i)
16465 {
16466 str = ((char_u **)ga.ga_data)[i];
16467
16468 li = listitem_alloc();
16469 if (li == NULL)
16470 vim_free(str);
16471 else
16472 {
16473 li->li_tv.v_type = VAR_STRING;
16474 li->li_tv.v_lock = 0;
16475 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016476 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016477 }
16478 }
16479 ga_clear(&ga);
16480 }
16481#endif
16482}
16483
Bram Moolenaar0d660222005-01-07 21:51:51 +000016484 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016485f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016486 typval_T *argvars;
16487 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016488{
16489 char_u *str;
16490 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016491 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016492 regmatch_T regmatch;
16493 char_u patbuf[NUMBUFLEN];
16494 char_u *save_cpo;
16495 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016496 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016497 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016498 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016499
16500 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16501 save_cpo = p_cpo;
16502 p_cpo = (char_u *)"";
16503
16504 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016505 if (argvars[1].v_type != VAR_UNKNOWN)
16506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016507 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16508 if (pat == NULL)
16509 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016510 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016511 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016512 }
16513 if (pat == NULL || *pat == NUL)
16514 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016515
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016516 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016518 if (typeerr)
16519 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520
Bram Moolenaar0d660222005-01-07 21:51:51 +000016521 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16522 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016524 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016525 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016526 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016527 if (*str == NUL)
16528 match = FALSE; /* empty item at the end */
16529 else
16530 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016531 if (match)
16532 end = regmatch.startp[0];
16533 else
16534 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016535 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16536 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016537 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016538 if (list_append_string(rettv->vval.v_list, str,
16539 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016540 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016541 }
16542 if (!match)
16543 break;
16544 /* Advance to just after the match. */
16545 if (regmatch.endp[0] > str)
16546 col = 0;
16547 else
16548 {
16549 /* Don't get stuck at the same match. */
16550#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016551 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016552#else
16553 col = 1;
16554#endif
16555 }
16556 str = regmatch.endp[0];
16557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558
Bram Moolenaar0d660222005-01-07 21:51:51 +000016559 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561
Bram Moolenaar0d660222005-01-07 21:51:51 +000016562 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563}
16564
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016565#ifdef FEAT_FLOAT
16566/*
16567 * "sqrt()" function
16568 */
16569 static void
16570f_sqrt(argvars, rettv)
16571 typval_T *argvars;
16572 typval_T *rettv;
16573{
16574 float_T f;
16575
16576 rettv->v_type = VAR_FLOAT;
16577 if (get_float_arg(argvars, &f) == OK)
16578 rettv->vval.v_float = sqrt(f);
16579 else
16580 rettv->vval.v_float = 0.0;
16581}
16582
16583/*
16584 * "str2float()" function
16585 */
16586 static void
16587f_str2float(argvars, rettv)
16588 typval_T *argvars;
16589 typval_T *rettv;
16590{
16591 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16592
16593 if (*p == '+')
16594 p = skipwhite(p + 1);
16595 (void)string2float(p, &rettv->vval.v_float);
16596 rettv->v_type = VAR_FLOAT;
16597}
16598#endif
16599
Bram Moolenaar2c932302006-03-18 21:42:09 +000016600/*
16601 * "str2nr()" function
16602 */
16603 static void
16604f_str2nr(argvars, rettv)
16605 typval_T *argvars;
16606 typval_T *rettv;
16607{
16608 int base = 10;
16609 char_u *p;
16610 long n;
16611
16612 if (argvars[1].v_type != VAR_UNKNOWN)
16613 {
16614 base = get_tv_number(&argvars[1]);
16615 if (base != 8 && base != 10 && base != 16)
16616 {
16617 EMSG(_(e_invarg));
16618 return;
16619 }
16620 }
16621
16622 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016623 if (*p == '+')
16624 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016625 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16626 rettv->vval.v_number = n;
16627}
16628
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629#ifdef HAVE_STRFTIME
16630/*
16631 * "strftime({format}[, {time}])" function
16632 */
16633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016634f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016635 typval_T *argvars;
16636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637{
16638 char_u result_buf[256];
16639 struct tm *curtime;
16640 time_t seconds;
16641 char_u *p;
16642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016643 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016645 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016646 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016647 seconds = time(NULL);
16648 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016649 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650 curtime = localtime(&seconds);
16651 /* MSVC returns NULL for an invalid value of seconds. */
16652 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016653 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654 else
16655 {
16656# ifdef FEAT_MBYTE
16657 vimconv_T conv;
16658 char_u *enc;
16659
16660 conv.vc_type = CONV_NONE;
16661 enc = enc_locale();
16662 convert_setup(&conv, p_enc, enc);
16663 if (conv.vc_type != CONV_NONE)
16664 p = string_convert(&conv, p, NULL);
16665# endif
16666 if (p != NULL)
16667 (void)strftime((char *)result_buf, sizeof(result_buf),
16668 (char *)p, curtime);
16669 else
16670 result_buf[0] = NUL;
16671
16672# ifdef FEAT_MBYTE
16673 if (conv.vc_type != CONV_NONE)
16674 vim_free(p);
16675 convert_setup(&conv, enc, p_enc);
16676 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016677 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678 else
16679# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016680 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681
16682# ifdef FEAT_MBYTE
16683 /* Release conversion descriptors */
16684 convert_setup(&conv, NULL, NULL);
16685 vim_free(enc);
16686# endif
16687 }
16688}
16689#endif
16690
16691/*
16692 * "stridx()" function
16693 */
16694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016695f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016696 typval_T *argvars;
16697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698{
16699 char_u buf[NUMBUFLEN];
16700 char_u *needle;
16701 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016702 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016704 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016706 needle = get_tv_string_chk(&argvars[1]);
16707 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016708 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016709 if (needle == NULL || haystack == NULL)
16710 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711
Bram Moolenaar33570922005-01-25 22:26:29 +000016712 if (argvars[2].v_type != VAR_UNKNOWN)
16713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016714 int error = FALSE;
16715
16716 start_idx = get_tv_number_chk(&argvars[2], &error);
16717 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016718 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016719 if (start_idx >= 0)
16720 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016721 }
16722
16723 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16724 if (pos != NULL)
16725 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726}
16727
16728/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016729 * "string()" function
16730 */
16731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016732f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016733 typval_T *argvars;
16734 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016735{
16736 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016737 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016739 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016740 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016741 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016742 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016743 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744}
16745
16746/*
16747 * "strlen()" function
16748 */
16749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016750f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016751 typval_T *argvars;
16752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016754 rettv->vval.v_number = (varnumber_T)(STRLEN(
16755 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756}
16757
16758/*
16759 * "strpart()" function
16760 */
16761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016762f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016763 typval_T *argvars;
16764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765{
16766 char_u *p;
16767 int n;
16768 int len;
16769 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016770 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016772 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 slen = (int)STRLEN(p);
16774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016775 n = get_tv_number_chk(&argvars[1], &error);
16776 if (error)
16777 len = 0;
16778 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016779 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780 else
16781 len = slen - n; /* default len: all bytes that are available. */
16782
16783 /*
16784 * Only return the overlap between the specified part and the actual
16785 * string.
16786 */
16787 if (n < 0)
16788 {
16789 len += n;
16790 n = 0;
16791 }
16792 else if (n > slen)
16793 n = slen;
16794 if (len < 0)
16795 len = 0;
16796 else if (n + len > slen)
16797 len = slen - n;
16798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016799 rettv->v_type = VAR_STRING;
16800 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801}
16802
16803/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016804 * "strridx()" function
16805 */
16806 static void
16807f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016808 typval_T *argvars;
16809 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016810{
16811 char_u buf[NUMBUFLEN];
16812 char_u *needle;
16813 char_u *haystack;
16814 char_u *rest;
16815 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016816 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016818 needle = get_tv_string_chk(&argvars[1]);
16819 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016820
16821 rettv->vval.v_number = -1;
16822 if (needle == NULL || haystack == NULL)
16823 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016824
16825 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016826 if (argvars[2].v_type != VAR_UNKNOWN)
16827 {
16828 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016829 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016830 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016831 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016832 }
16833 else
16834 end_idx = haystack_len;
16835
Bram Moolenaar0d660222005-01-07 21:51:51 +000016836 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016837 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016838 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016839 lastmatch = haystack + end_idx;
16840 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016841 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016842 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016843 for (rest = haystack; *rest != '\0'; ++rest)
16844 {
16845 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016846 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016847 break;
16848 lastmatch = rest;
16849 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016850 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016851
16852 if (lastmatch == NULL)
16853 rettv->vval.v_number = -1;
16854 else
16855 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16856}
16857
16858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 * "strtrans()" function
16860 */
16861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016862f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016863 typval_T *argvars;
16864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016866 rettv->v_type = VAR_STRING;
16867 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868}
16869
16870/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016871 * "submatch()" function
16872 */
16873 static void
16874f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016875 typval_T *argvars;
16876 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016877{
16878 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016879 rettv->vval.v_string =
16880 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016881}
16882
16883/*
16884 * "substitute()" function
16885 */
16886 static void
16887f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016888 typval_T *argvars;
16889 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016890{
16891 char_u patbuf[NUMBUFLEN];
16892 char_u subbuf[NUMBUFLEN];
16893 char_u flagsbuf[NUMBUFLEN];
16894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016895 char_u *str = get_tv_string_chk(&argvars[0]);
16896 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16897 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16898 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16899
Bram Moolenaar0d660222005-01-07 21:51:51 +000016900 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016901 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16902 rettv->vval.v_string = NULL;
16903 else
16904 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016905}
16906
16907/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016908 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016911f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016912 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914{
16915 int id = 0;
16916#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016917 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918 long col;
16919 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016920 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016922 lnum = get_tv_lnum(argvars); /* -1 on type error */
16923 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16924 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016926 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016927 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016928 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929#endif
16930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016931 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932}
16933
16934/*
16935 * "synIDattr(id, what [, mode])" function
16936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016938f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016939 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941{
16942 char_u *p = NULL;
16943#ifdef FEAT_SYN_HL
16944 int id;
16945 char_u *what;
16946 char_u *mode;
16947 char_u modebuf[NUMBUFLEN];
16948 int modec;
16949
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016950 id = get_tv_number(&argvars[0]);
16951 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016952 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016954 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020016956 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 modec = 0; /* replace invalid with current */
16958 }
16959 else
16960 {
16961#ifdef FEAT_GUI
16962 if (gui.in_use)
16963 modec = 'g';
16964 else
16965#endif
16966 if (t_colors > 1)
16967 modec = 'c';
16968 else
16969 modec = 't';
16970 }
16971
16972
16973 switch (TOLOWER_ASC(what[0]))
16974 {
16975 case 'b':
16976 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16977 p = highlight_color(id, what, modec);
16978 else /* bold */
16979 p = highlight_has_attr(id, HL_BOLD, modec);
16980 break;
16981
Bram Moolenaar12682fd2010-03-10 13:43:49 +010016982 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 p = highlight_color(id, what, modec);
16984 break;
16985
16986 case 'i':
16987 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
16988 p = highlight_has_attr(id, HL_INVERSE, modec);
16989 else /* italic */
16990 p = highlight_has_attr(id, HL_ITALIC, modec);
16991 break;
16992
16993 case 'n': /* name */
16994 p = get_highlight_name(NULL, id - 1);
16995 break;
16996
16997 case 'r': /* reverse */
16998 p = highlight_has_attr(id, HL_INVERSE, modec);
16999 break;
17000
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017001 case 's':
17002 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17003 p = highlight_color(id, what, modec);
17004 else /* standout */
17005 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017006 break;
17007
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017008 case 'u':
17009 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17010 /* underline */
17011 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17012 else
17013 /* undercurl */
17014 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015 break;
17016 }
17017
17018 if (p != NULL)
17019 p = vim_strsave(p);
17020#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017021 rettv->v_type = VAR_STRING;
17022 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023}
17024
17025/*
17026 * "synIDtrans(id)" function
17027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017029f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017030 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032{
17033 int id;
17034
17035#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017036 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037
17038 if (id > 0)
17039 id = syn_get_final_id(id);
17040 else
17041#endif
17042 id = 0;
17043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017044 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045}
17046
17047/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017048 * "synstack(lnum, col)" function
17049 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017050 static void
17051f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017052 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017053 typval_T *rettv;
17054{
17055#ifdef FEAT_SYN_HL
17056 long lnum;
17057 long col;
17058 int i;
17059 int id;
17060#endif
17061
17062 rettv->v_type = VAR_LIST;
17063 rettv->vval.v_list = NULL;
17064
17065#ifdef FEAT_SYN_HL
17066 lnum = get_tv_lnum(argvars); /* -1 on type error */
17067 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17068
17069 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017070 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017071 && rettv_list_alloc(rettv) != FAIL)
17072 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017073 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017074 for (i = 0; ; ++i)
17075 {
17076 id = syn_get_stack_item(i);
17077 if (id < 0)
17078 break;
17079 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17080 break;
17081 }
17082 }
17083#endif
17084}
17085
17086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087 * "system()" function
17088 */
17089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017090f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017091 typval_T *argvars;
17092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017094 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017096 char_u *infile = NULL;
17097 char_u buf[NUMBUFLEN];
17098 int err = FALSE;
17099 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017101 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017102 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017103
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017104 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017105 {
17106 /*
17107 * Write the string to a temp file, to be used for input of the shell
17108 * command.
17109 */
17110 if ((infile = vim_tempname('i')) == NULL)
17111 {
17112 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017113 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017114 }
17115
17116 fd = mch_fopen((char *)infile, WRITEBIN);
17117 if (fd == NULL)
17118 {
17119 EMSG2(_(e_notopen), infile);
17120 goto done;
17121 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017122 p = get_tv_string_buf_chk(&argvars[1], buf);
17123 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017124 {
17125 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017126 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017127 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017128 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17129 err = TRUE;
17130 if (fclose(fd) != 0)
17131 err = TRUE;
17132 if (err)
17133 {
17134 EMSG(_("E677: Error writing temp file"));
17135 goto done;
17136 }
17137 }
17138
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017139 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17140 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017141
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142#ifdef USE_CR
17143 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017144 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 {
17146 char_u *s;
17147
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017148 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149 {
17150 if (*s == CAR)
17151 *s = NL;
17152 }
17153 }
17154#else
17155# ifdef USE_CRNL
17156 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017157 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 {
17159 char_u *s, *d;
17160
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017161 d = res;
17162 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 {
17164 if (s[0] == CAR && s[1] == NL)
17165 ++s;
17166 *d++ = *s;
17167 }
17168 *d = NUL;
17169 }
17170# endif
17171#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017172
17173done:
17174 if (infile != NULL)
17175 {
17176 mch_remove(infile);
17177 vim_free(infile);
17178 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017179 rettv->v_type = VAR_STRING;
17180 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181}
17182
17183/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017184 * "tabpagebuflist()" function
17185 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017186 static void
17187f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017188 typval_T *argvars UNUSED;
17189 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017190{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017191#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017192 tabpage_T *tp;
17193 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017194
17195 if (argvars[0].v_type == VAR_UNKNOWN)
17196 wp = firstwin;
17197 else
17198 {
17199 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17200 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017201 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017202 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017203 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017204 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017205 for (; wp != NULL; wp = wp->w_next)
17206 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017207 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017208 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017209 }
17210#endif
17211}
17212
17213
17214/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017215 * "tabpagenr()" function
17216 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017217 static void
17218f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017219 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017220 typval_T *rettv;
17221{
17222 int nr = 1;
17223#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017224 char_u *arg;
17225
17226 if (argvars[0].v_type != VAR_UNKNOWN)
17227 {
17228 arg = get_tv_string_chk(&argvars[0]);
17229 nr = 0;
17230 if (arg != NULL)
17231 {
17232 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017233 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017234 else
17235 EMSG2(_(e_invexpr2), arg);
17236 }
17237 }
17238 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017239 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017240#endif
17241 rettv->vval.v_number = nr;
17242}
17243
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017244
17245#ifdef FEAT_WINDOWS
17246static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17247
17248/*
17249 * Common code for tabpagewinnr() and winnr().
17250 */
17251 static int
17252get_winnr(tp, argvar)
17253 tabpage_T *tp;
17254 typval_T *argvar;
17255{
17256 win_T *twin;
17257 int nr = 1;
17258 win_T *wp;
17259 char_u *arg;
17260
17261 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17262 if (argvar->v_type != VAR_UNKNOWN)
17263 {
17264 arg = get_tv_string_chk(argvar);
17265 if (arg == NULL)
17266 nr = 0; /* type error; errmsg already given */
17267 else if (STRCMP(arg, "$") == 0)
17268 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17269 else if (STRCMP(arg, "#") == 0)
17270 {
17271 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17272 if (twin == NULL)
17273 nr = 0;
17274 }
17275 else
17276 {
17277 EMSG2(_(e_invexpr2), arg);
17278 nr = 0;
17279 }
17280 }
17281
17282 if (nr > 0)
17283 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17284 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017285 {
17286 if (wp == NULL)
17287 {
17288 /* didn't find it in this tabpage */
17289 nr = 0;
17290 break;
17291 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017292 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017293 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017294 return nr;
17295}
17296#endif
17297
17298/*
17299 * "tabpagewinnr()" function
17300 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017301 static void
17302f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017303 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017304 typval_T *rettv;
17305{
17306 int nr = 1;
17307#ifdef FEAT_WINDOWS
17308 tabpage_T *tp;
17309
17310 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17311 if (tp == NULL)
17312 nr = 0;
17313 else
17314 nr = get_winnr(tp, &argvars[1]);
17315#endif
17316 rettv->vval.v_number = nr;
17317}
17318
17319
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017320/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017321 * "tagfiles()" function
17322 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017323 static void
17324f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017325 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017326 typval_T *rettv;
17327{
17328 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017329 tagname_T tn;
17330 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017331
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017332 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017333 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017334
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017335 for (first = TRUE; ; first = FALSE)
17336 if (get_tagfname(&tn, first, fname) == FAIL
17337 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017338 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017339 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017340}
17341
17342/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017343 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017344 */
17345 static void
17346f_taglist(argvars, rettv)
17347 typval_T *argvars;
17348 typval_T *rettv;
17349{
17350 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017351
17352 tag_pattern = get_tv_string(&argvars[0]);
17353
17354 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017355 if (*tag_pattern == NUL)
17356 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017357
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017358 if (rettv_list_alloc(rettv) == OK)
17359 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017360}
17361
17362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 * "tempname()" function
17364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017366f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017367 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369{
17370 static int x = 'A';
17371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017372 rettv->v_type = VAR_STRING;
17373 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374
17375 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17376 * names. Skip 'I' and 'O', they are used for shell redirection. */
17377 do
17378 {
17379 if (x == 'Z')
17380 x = '0';
17381 else if (x == '9')
17382 x = 'A';
17383 else
17384 {
17385#ifdef EBCDIC
17386 if (x == 'I')
17387 x = 'J';
17388 else if (x == 'R')
17389 x = 'S';
17390 else
17391#endif
17392 ++x;
17393 }
17394 } while (x == 'I' || x == 'O');
17395}
17396
17397/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017398 * "test(list)" function: Just checking the walls...
17399 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017400 static void
17401f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017402 typval_T *argvars UNUSED;
17403 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017404{
17405 /* Used for unit testing. Change the code below to your liking. */
17406#if 0
17407 listitem_T *li;
17408 list_T *l;
17409 char_u *bad, *good;
17410
17411 if (argvars[0].v_type != VAR_LIST)
17412 return;
17413 l = argvars[0].vval.v_list;
17414 if (l == NULL)
17415 return;
17416 li = l->lv_first;
17417 if (li == NULL)
17418 return;
17419 bad = get_tv_string(&li->li_tv);
17420 li = li->li_next;
17421 if (li == NULL)
17422 return;
17423 good = get_tv_string(&li->li_tv);
17424 rettv->vval.v_number = test_edit_score(bad, good);
17425#endif
17426}
17427
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017428#ifdef FEAT_FLOAT
17429/*
17430 * "tan()" function
17431 */
17432 static void
17433f_tan(argvars, rettv)
17434 typval_T *argvars;
17435 typval_T *rettv;
17436{
17437 float_T f;
17438
17439 rettv->v_type = VAR_FLOAT;
17440 if (get_float_arg(argvars, &f) == OK)
17441 rettv->vval.v_float = tan(f);
17442 else
17443 rettv->vval.v_float = 0.0;
17444}
17445
17446/*
17447 * "tanh()" function
17448 */
17449 static void
17450f_tanh(argvars, rettv)
17451 typval_T *argvars;
17452 typval_T *rettv;
17453{
17454 float_T f;
17455
17456 rettv->v_type = VAR_FLOAT;
17457 if (get_float_arg(argvars, &f) == OK)
17458 rettv->vval.v_float = tanh(f);
17459 else
17460 rettv->vval.v_float = 0.0;
17461}
17462#endif
17463
Bram Moolenaard52d9742005-08-21 22:20:28 +000017464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465 * "tolower(string)" function
17466 */
17467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017468f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017469 typval_T *argvars;
17470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471{
17472 char_u *p;
17473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017474 p = vim_strsave(get_tv_string(&argvars[0]));
17475 rettv->v_type = VAR_STRING;
17476 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477
17478 if (p != NULL)
17479 while (*p != NUL)
17480 {
17481#ifdef FEAT_MBYTE
17482 int l;
17483
17484 if (enc_utf8)
17485 {
17486 int c, lc;
17487
17488 c = utf_ptr2char(p);
17489 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017490 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491 /* TODO: reallocate string when byte count changes. */
17492 if (utf_char2len(lc) == l)
17493 utf_char2bytes(lc, p);
17494 p += l;
17495 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017496 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 p += l; /* skip multi-byte character */
17498 else
17499#endif
17500 {
17501 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17502 ++p;
17503 }
17504 }
17505}
17506
17507/*
17508 * "toupper(string)" function
17509 */
17510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017511f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017512 typval_T *argvars;
17513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017515 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017516 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517}
17518
17519/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017520 * "tr(string, fromstr, tostr)" function
17521 */
17522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017523f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017524 typval_T *argvars;
17525 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017526{
17527 char_u *instr;
17528 char_u *fromstr;
17529 char_u *tostr;
17530 char_u *p;
17531#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017532 int inlen;
17533 int fromlen;
17534 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017535 int idx;
17536 char_u *cpstr;
17537 int cplen;
17538 int first = TRUE;
17539#endif
17540 char_u buf[NUMBUFLEN];
17541 char_u buf2[NUMBUFLEN];
17542 garray_T ga;
17543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017544 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017545 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17546 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017547
17548 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017549 rettv->v_type = VAR_STRING;
17550 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017551 if (fromstr == NULL || tostr == NULL)
17552 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017553 ga_init2(&ga, (int)sizeof(char), 80);
17554
17555#ifdef FEAT_MBYTE
17556 if (!has_mbyte)
17557#endif
17558 /* not multi-byte: fromstr and tostr must be the same length */
17559 if (STRLEN(fromstr) != STRLEN(tostr))
17560 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017561#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017562error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017563#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017564 EMSG2(_(e_invarg2), fromstr);
17565 ga_clear(&ga);
17566 return;
17567 }
17568
17569 /* fromstr and tostr have to contain the same number of chars */
17570 while (*instr != NUL)
17571 {
17572#ifdef FEAT_MBYTE
17573 if (has_mbyte)
17574 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017575 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017576 cpstr = instr;
17577 cplen = inlen;
17578 idx = 0;
17579 for (p = fromstr; *p != NUL; p += fromlen)
17580 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017581 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017582 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17583 {
17584 for (p = tostr; *p != NUL; p += tolen)
17585 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017586 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017587 if (idx-- == 0)
17588 {
17589 cplen = tolen;
17590 cpstr = p;
17591 break;
17592 }
17593 }
17594 if (*p == NUL) /* tostr is shorter than fromstr */
17595 goto error;
17596 break;
17597 }
17598 ++idx;
17599 }
17600
17601 if (first && cpstr == instr)
17602 {
17603 /* Check that fromstr and tostr have the same number of
17604 * (multi-byte) characters. Done only once when a character
17605 * of instr doesn't appear in fromstr. */
17606 first = FALSE;
17607 for (p = tostr; *p != NUL; p += tolen)
17608 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017609 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017610 --idx;
17611 }
17612 if (idx != 0)
17613 goto error;
17614 }
17615
17616 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017617 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017618 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017619
17620 instr += inlen;
17621 }
17622 else
17623#endif
17624 {
17625 /* When not using multi-byte chars we can do it faster. */
17626 p = vim_strchr(fromstr, *instr);
17627 if (p != NULL)
17628 ga_append(&ga, tostr[p - fromstr]);
17629 else
17630 ga_append(&ga, *instr);
17631 ++instr;
17632 }
17633 }
17634
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017635 /* add a terminating NUL */
17636 ga_grow(&ga, 1);
17637 ga_append(&ga, NUL);
17638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017639 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017640}
17641
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017642#ifdef FEAT_FLOAT
17643/*
17644 * "trunc({float})" function
17645 */
17646 static void
17647f_trunc(argvars, rettv)
17648 typval_T *argvars;
17649 typval_T *rettv;
17650{
17651 float_T f;
17652
17653 rettv->v_type = VAR_FLOAT;
17654 if (get_float_arg(argvars, &f) == OK)
17655 /* trunc() is not in C90, use floor() or ceil() instead. */
17656 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17657 else
17658 rettv->vval.v_float = 0.0;
17659}
17660#endif
17661
Bram Moolenaar8299df92004-07-10 09:47:34 +000017662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 * "type(expr)" function
17664 */
17665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017666f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017667 typval_T *argvars;
17668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017670 int n;
17671
17672 switch (argvars[0].v_type)
17673 {
17674 case VAR_NUMBER: n = 0; break;
17675 case VAR_STRING: n = 1; break;
17676 case VAR_FUNC: n = 2; break;
17677 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017678 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017679#ifdef FEAT_FLOAT
17680 case VAR_FLOAT: n = 5; break;
17681#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017682 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17683 }
17684 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685}
17686
17687/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017688 * "undofile(name)" function
17689 */
17690 static void
17691f_undofile(argvars, rettv)
17692 typval_T *argvars;
17693 typval_T *rettv;
17694{
17695 rettv->v_type = VAR_STRING;
17696#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017697 {
17698 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17699
17700 if (ffname != NULL)
17701 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17702 vim_free(ffname);
17703 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017704#else
17705 rettv->vval.v_string = NULL;
17706#endif
17707}
17708
17709/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017710 * "undotree()" function
17711 */
17712 static void
17713f_undotree(argvars, rettv)
17714 typval_T *argvars UNUSED;
17715 typval_T *rettv;
17716{
17717 if (rettv_dict_alloc(rettv) == OK)
17718 {
17719 dict_T *dict = rettv->vval.v_dict;
17720 list_T *list;
17721
Bram Moolenaar730cde92010-06-27 05:18:54 +020017722 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017723 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017724 dict_add_nr_str(dict, "save_last",
17725 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017726 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17727 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017728 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017729
17730 list = list_alloc();
17731 if (list != NULL)
17732 {
17733 u_eval_tree(curbuf->b_u_oldhead, list);
17734 dict_add_list(dict, "entries", list);
17735 }
17736 }
17737}
17738
17739/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017740 * "values(dict)" function
17741 */
17742 static void
17743f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017744 typval_T *argvars;
17745 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017746{
17747 dict_list(argvars, rettv, 1);
17748}
17749
17750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 * "virtcol(string)" function
17752 */
17753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017754f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017755 typval_T *argvars;
17756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757{
17758 colnr_T vcol = 0;
17759 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017760 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017762 fp = var2fpos(&argvars[0], FALSE, &fnum);
17763 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17764 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765 {
17766 getvvcol(curwin, fp, NULL, NULL, &vcol);
17767 ++vcol;
17768 }
17769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017770 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771}
17772
17773/*
17774 * "visualmode()" function
17775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017777f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017778 typval_T *argvars UNUSED;
17779 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780{
17781#ifdef FEAT_VISUAL
17782 char_u str[2];
17783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017784 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785 str[0] = curbuf->b_visual_mode_eval;
17786 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017787 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788
17789 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017790 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792#endif
17793}
17794
17795/*
17796 * "winbufnr(nr)" function
17797 */
17798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017799f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017800 typval_T *argvars;
17801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802{
17803 win_T *wp;
17804
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017805 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017807 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017809 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810}
17811
17812/*
17813 * "wincol()" function
17814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017816f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017817 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819{
17820 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017821 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822}
17823
17824/*
17825 * "winheight(nr)" function
17826 */
17827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017828f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017829 typval_T *argvars;
17830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831{
17832 win_T *wp;
17833
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017834 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017836 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017838 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839}
17840
17841/*
17842 * "winline()" function
17843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017845f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017846 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848{
17849 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017850 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851}
17852
17853/*
17854 * "winnr()" function
17855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017857f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017858 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860{
17861 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017862
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017864 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017866 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867}
17868
17869/*
17870 * "winrestcmd()" function
17871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017873f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017874 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876{
17877#ifdef FEAT_WINDOWS
17878 win_T *wp;
17879 int winnr = 1;
17880 garray_T ga;
17881 char_u buf[50];
17882
17883 ga_init2(&ga, (int)sizeof(char), 70);
17884 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17885 {
17886 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17887 ga_concat(&ga, buf);
17888# ifdef FEAT_VERTSPLIT
17889 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17890 ga_concat(&ga, buf);
17891# endif
17892 ++winnr;
17893 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017894 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017896 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017898 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017900 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901}
17902
17903/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017904 * "winrestview()" function
17905 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017906 static void
17907f_winrestview(argvars, rettv)
17908 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017909 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017910{
17911 dict_T *dict;
17912
17913 if (argvars[0].v_type != VAR_DICT
17914 || (dict = argvars[0].vval.v_dict) == NULL)
17915 EMSG(_(e_invarg));
17916 else
17917 {
17918 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17919 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17920#ifdef FEAT_VIRTUALEDIT
17921 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17922#endif
17923 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017924 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017925
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017926 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017927#ifdef FEAT_DIFF
17928 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17929#endif
17930 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17931 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17932
17933 check_cursor();
17934 changed_cline_bef_curs();
17935 invalidate_botline();
17936 redraw_later(VALID);
17937
17938 if (curwin->w_topline == 0)
17939 curwin->w_topline = 1;
17940 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17941 curwin->w_topline = curbuf->b_ml.ml_line_count;
17942#ifdef FEAT_DIFF
17943 check_topfill(curwin, TRUE);
17944#endif
17945 }
17946}
17947
17948/*
17949 * "winsaveview()" function
17950 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017951 static void
17952f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017953 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017954 typval_T *rettv;
17955{
17956 dict_T *dict;
17957
Bram Moolenaara800b422010-06-27 01:15:55 +020017958 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017959 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020017960 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017961
17962 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17963 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17964#ifdef FEAT_VIRTUALEDIT
17965 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17966#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017967 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017968 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17969
17970 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17971#ifdef FEAT_DIFF
17972 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17973#endif
17974 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17975 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17976}
17977
17978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979 * "winwidth(nr)" function
17980 */
17981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017982f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017983 typval_T *argvars;
17984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985{
17986 win_T *wp;
17987
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017988 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017990 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 else
17992#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017993 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017995 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996#endif
17997}
17998
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018000 * "writefile()" function
18001 */
18002 static void
18003f_writefile(argvars, rettv)
18004 typval_T *argvars;
18005 typval_T *rettv;
18006{
18007 int binary = FALSE;
18008 char_u *fname;
18009 FILE *fd;
18010 listitem_T *li;
18011 char_u *s;
18012 int ret = 0;
18013 int c;
18014
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018015 if (check_restricted() || check_secure())
18016 return;
18017
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018018 if (argvars[0].v_type != VAR_LIST)
18019 {
18020 EMSG2(_(e_listarg), "writefile()");
18021 return;
18022 }
18023 if (argvars[0].vval.v_list == NULL)
18024 return;
18025
18026 if (argvars[2].v_type != VAR_UNKNOWN
18027 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18028 binary = TRUE;
18029
18030 /* Always open the file in binary mode, library functions have a mind of
18031 * their own about CR-LF conversion. */
18032 fname = get_tv_string(&argvars[1]);
18033 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18034 {
18035 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18036 ret = -1;
18037 }
18038 else
18039 {
18040 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18041 li = li->li_next)
18042 {
18043 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18044 {
18045 if (*s == '\n')
18046 c = putc(NUL, fd);
18047 else
18048 c = putc(*s, fd);
18049 if (c == EOF)
18050 {
18051 ret = -1;
18052 break;
18053 }
18054 }
18055 if (!binary || li->li_next != NULL)
18056 if (putc('\n', fd) == EOF)
18057 {
18058 ret = -1;
18059 break;
18060 }
18061 if (ret < 0)
18062 {
18063 EMSG(_(e_write));
18064 break;
18065 }
18066 }
18067 fclose(fd);
18068 }
18069
18070 rettv->vval.v_number = ret;
18071}
18072
18073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018075 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076 */
18077 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018078var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018079 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018080 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018081 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018083 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018085 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086
Bram Moolenaara5525202006-03-02 22:52:09 +000018087 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018088 if (varp->v_type == VAR_LIST)
18089 {
18090 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018091 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018092 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018093 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018094
18095 l = varp->vval.v_list;
18096 if (l == NULL)
18097 return NULL;
18098
18099 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018100 pos.lnum = list_find_nr(l, 0L, &error);
18101 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018102 return NULL; /* invalid line number */
18103
18104 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018105 pos.col = list_find_nr(l, 1L, &error);
18106 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018107 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018108 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018109
18110 /* We accept "$" for the column number: last column. */
18111 li = list_find(l, 1L);
18112 if (li != NULL && li->li_tv.v_type == VAR_STRING
18113 && li->li_tv.vval.v_string != NULL
18114 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18115 pos.col = len + 1;
18116
Bram Moolenaara5525202006-03-02 22:52:09 +000018117 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018118 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018119 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018120 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018121
Bram Moolenaara5525202006-03-02 22:52:09 +000018122#ifdef FEAT_VIRTUALEDIT
18123 /* Get the virtual offset. Defaults to zero. */
18124 pos.coladd = list_find_nr(l, 2L, &error);
18125 if (error)
18126 pos.coladd = 0;
18127#endif
18128
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018129 return &pos;
18130 }
18131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018132 name = get_tv_string_chk(varp);
18133 if (name == NULL)
18134 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018135 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018137#ifdef FEAT_VISUAL
18138 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18139 {
18140 if (VIsual_active)
18141 return &VIsual;
18142 return &curwin->w_cursor;
18143 }
18144#endif
18145 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018147 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18149 return NULL;
18150 return pp;
18151 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018152
18153#ifdef FEAT_VIRTUALEDIT
18154 pos.coladd = 0;
18155#endif
18156
Bram Moolenaar477933c2007-07-17 14:32:23 +000018157 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018158 {
18159 pos.col = 0;
18160 if (name[1] == '0') /* "w0": first visible line */
18161 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018162 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018163 pos.lnum = curwin->w_topline;
18164 return &pos;
18165 }
18166 else if (name[1] == '$') /* "w$": last visible line */
18167 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018168 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018169 pos.lnum = curwin->w_botline - 1;
18170 return &pos;
18171 }
18172 }
18173 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018175 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 {
18177 pos.lnum = curbuf->b_ml.ml_line_count;
18178 pos.col = 0;
18179 }
18180 else
18181 {
18182 pos.lnum = curwin->w_cursor.lnum;
18183 pos.col = (colnr_T)STRLEN(ml_get_curline());
18184 }
18185 return &pos;
18186 }
18187 return NULL;
18188}
18189
18190/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018191 * Convert list in "arg" into a position and optional file number.
18192 * When "fnump" is NULL there is no file number, only 3 items.
18193 * Note that the column is passed on as-is, the caller may want to decrement
18194 * it to use 1 for the first column.
18195 * Return FAIL when conversion is not possible, doesn't check the position for
18196 * validity.
18197 */
18198 static int
18199list2fpos(arg, posp, fnump)
18200 typval_T *arg;
18201 pos_T *posp;
18202 int *fnump;
18203{
18204 list_T *l = arg->vval.v_list;
18205 long i = 0;
18206 long n;
18207
Bram Moolenaarbde35262006-07-23 20:12:24 +000018208 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18209 * when "fnump" isn't NULL and "coladd" is optional. */
18210 if (arg->v_type != VAR_LIST
18211 || l == NULL
18212 || l->lv_len < (fnump == NULL ? 2 : 3)
18213 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018214 return FAIL;
18215
18216 if (fnump != NULL)
18217 {
18218 n = list_find_nr(l, i++, NULL); /* fnum */
18219 if (n < 0)
18220 return FAIL;
18221 if (n == 0)
18222 n = curbuf->b_fnum; /* current buffer */
18223 *fnump = n;
18224 }
18225
18226 n = list_find_nr(l, i++, NULL); /* lnum */
18227 if (n < 0)
18228 return FAIL;
18229 posp->lnum = n;
18230
18231 n = list_find_nr(l, i++, NULL); /* col */
18232 if (n < 0)
18233 return FAIL;
18234 posp->col = n;
18235
18236#ifdef FEAT_VIRTUALEDIT
18237 n = list_find_nr(l, i, NULL);
18238 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018239 posp->coladd = 0;
18240 else
18241 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018242#endif
18243
18244 return OK;
18245}
18246
18247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248 * Get the length of an environment variable name.
18249 * Advance "arg" to the first character after the name.
18250 * Return 0 for error.
18251 */
18252 static int
18253get_env_len(arg)
18254 char_u **arg;
18255{
18256 char_u *p;
18257 int len;
18258
18259 for (p = *arg; vim_isIDc(*p); ++p)
18260 ;
18261 if (p == *arg) /* no name found */
18262 return 0;
18263
18264 len = (int)(p - *arg);
18265 *arg = p;
18266 return len;
18267}
18268
18269/*
18270 * Get the length of the name of a function or internal variable.
18271 * "arg" is advanced to the first non-white character after the name.
18272 * Return 0 if something is wrong.
18273 */
18274 static int
18275get_id_len(arg)
18276 char_u **arg;
18277{
18278 char_u *p;
18279 int len;
18280
18281 /* Find the end of the name. */
18282 for (p = *arg; eval_isnamec(*p); ++p)
18283 ;
18284 if (p == *arg) /* no name found */
18285 return 0;
18286
18287 len = (int)(p - *arg);
18288 *arg = skipwhite(p);
18289
18290 return len;
18291}
18292
18293/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018294 * Get the length of the name of a variable or function.
18295 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018297 * Return -1 if curly braces expansion failed.
18298 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 * If the name contains 'magic' {}'s, expand them and return the
18300 * expanded name in an allocated string via 'alias' - caller must free.
18301 */
18302 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018303get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 char_u **arg;
18305 char_u **alias;
18306 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018307 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018308{
18309 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310 char_u *p;
18311 char_u *expr_start;
18312 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313
18314 *alias = NULL; /* default to no alias */
18315
18316 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18317 && (*arg)[2] == (int)KE_SNR)
18318 {
18319 /* hard coded <SNR>, already translated */
18320 *arg += 3;
18321 return get_id_len(arg) + 3;
18322 }
18323 len = eval_fname_script(*arg);
18324 if (len > 0)
18325 {
18326 /* literal "<SID>", "s:" or "<SNR>" */
18327 *arg += len;
18328 }
18329
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018331 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018333 p = find_name_end(*arg, &expr_start, &expr_end,
18334 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 if (expr_start != NULL)
18336 {
18337 char_u *temp_string;
18338
18339 if (!evaluate)
18340 {
18341 len += (int)(p - *arg);
18342 *arg = skipwhite(p);
18343 return len;
18344 }
18345
18346 /*
18347 * Include any <SID> etc in the expanded string:
18348 * Thus the -len here.
18349 */
18350 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18351 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018352 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 *alias = temp_string;
18354 *arg = skipwhite(p);
18355 return (int)STRLEN(temp_string);
18356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357
18358 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018359 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 EMSG2(_(e_invexpr2), *arg);
18361
18362 return len;
18363}
18364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018365/*
18366 * Find the end of a variable or function name, taking care of magic braces.
18367 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18368 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018369 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018370 * Return a pointer to just after the name. Equal to "arg" if there is no
18371 * valid name.
18372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018374find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 char_u *arg;
18376 char_u **expr_start;
18377 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018378 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018380 int mb_nest = 0;
18381 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 char_u *p;
18383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018384 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018386 *expr_start = NULL;
18387 *expr_end = NULL;
18388 }
18389
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018390 /* Quick check for valid starting character. */
18391 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18392 return arg;
18393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018394 for (p = arg; *p != NUL
18395 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018396 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018397 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018398 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018399 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018400 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018401 if (*p == '\'')
18402 {
18403 /* skip over 'string' to avoid counting [ and ] inside it. */
18404 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18405 ;
18406 if (*p == NUL)
18407 break;
18408 }
18409 else if (*p == '"')
18410 {
18411 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18412 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18413 if (*p == '\\' && p[1] != NUL)
18414 ++p;
18415 if (*p == NUL)
18416 break;
18417 }
18418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018419 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018421 if (*p == '[')
18422 ++br_nest;
18423 else if (*p == ']')
18424 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018427 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429 if (*p == '{')
18430 {
18431 mb_nest++;
18432 if (expr_start != NULL && *expr_start == NULL)
18433 *expr_start = p;
18434 }
18435 else if (*p == '}')
18436 {
18437 mb_nest--;
18438 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18439 *expr_end = p;
18440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442 }
18443
18444 return p;
18445}
18446
18447/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018448 * Expands out the 'magic' {}'s in a variable/function name.
18449 * Note that this can call itself recursively, to deal with
18450 * constructs like foo{bar}{baz}{bam}
18451 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18452 * "in_start" ^
18453 * "expr_start" ^
18454 * "expr_end" ^
18455 * "in_end" ^
18456 *
18457 * Returns a new allocated string, which the caller must free.
18458 * Returns NULL for failure.
18459 */
18460 static char_u *
18461make_expanded_name(in_start, expr_start, expr_end, in_end)
18462 char_u *in_start;
18463 char_u *expr_start;
18464 char_u *expr_end;
18465 char_u *in_end;
18466{
18467 char_u c1;
18468 char_u *retval = NULL;
18469 char_u *temp_result;
18470 char_u *nextcmd = NULL;
18471
18472 if (expr_end == NULL || in_end == NULL)
18473 return NULL;
18474 *expr_start = NUL;
18475 *expr_end = NUL;
18476 c1 = *in_end;
18477 *in_end = NUL;
18478
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018479 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018480 if (temp_result != NULL && nextcmd == NULL)
18481 {
18482 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18483 + (in_end - expr_end) + 1));
18484 if (retval != NULL)
18485 {
18486 STRCPY(retval, in_start);
18487 STRCAT(retval, temp_result);
18488 STRCAT(retval, expr_end + 1);
18489 }
18490 }
18491 vim_free(temp_result);
18492
18493 *in_end = c1; /* put char back for error messages */
18494 *expr_start = '{';
18495 *expr_end = '}';
18496
18497 if (retval != NULL)
18498 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018499 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018500 if (expr_start != NULL)
18501 {
18502 /* Further expansion! */
18503 temp_result = make_expanded_name(retval, expr_start,
18504 expr_end, temp_result);
18505 vim_free(retval);
18506 retval = temp_result;
18507 }
18508 }
18509
18510 return retval;
18511}
18512
18513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018515 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516 */
18517 static int
18518eval_isnamec(c)
18519 int c;
18520{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018521 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18522}
18523
18524/*
18525 * Return TRUE if character "c" can be used as the first character in a
18526 * variable or function name (excluding '{' and '}').
18527 */
18528 static int
18529eval_isnamec1(c)
18530 int c;
18531{
18532 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533}
18534
18535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 * Set number v: variable to "val".
18537 */
18538 void
18539set_vim_var_nr(idx, val)
18540 int idx;
18541 long val;
18542{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018543 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544}
18545
18546/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018547 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548 */
18549 long
18550get_vim_var_nr(idx)
18551 int idx;
18552{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018553 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554}
18555
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018556/*
18557 * Get string v: variable value. Uses a static buffer, can only be used once.
18558 */
18559 char_u *
18560get_vim_var_str(idx)
18561 int idx;
18562{
18563 return get_tv_string(&vimvars[idx].vv_tv);
18564}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018565
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018567 * Get List v: variable value. Caller must take care of reference count when
18568 * needed.
18569 */
18570 list_T *
18571get_vim_var_list(idx)
18572 int idx;
18573{
18574 return vimvars[idx].vv_list;
18575}
18576
18577/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018578 * Set v:char to character "c".
18579 */
18580 void
18581set_vim_var_char(c)
18582 int c;
18583{
18584#ifdef FEAT_MBYTE
18585 char_u buf[MB_MAXBYTES];
18586#else
18587 char_u buf[2];
18588#endif
18589
18590#ifdef FEAT_MBYTE
18591 if (has_mbyte)
18592 buf[(*mb_char2bytes)(c, buf)] = NUL;
18593 else
18594#endif
18595 {
18596 buf[0] = c;
18597 buf[1] = NUL;
18598 }
18599 set_vim_var_string(VV_CHAR, buf, -1);
18600}
18601
18602/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018603 * Set v:count to "count" and v:count1 to "count1".
18604 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 */
18606 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018607set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608 long count;
18609 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018610 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018612 if (set_prevcount)
18613 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018614 vimvars[VV_COUNT].vv_nr = count;
18615 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616}
18617
18618/*
18619 * Set string v: variable to a copy of "val".
18620 */
18621 void
18622set_vim_var_string(idx, val, len)
18623 int idx;
18624 char_u *val;
18625 int len; /* length of "val" to use or -1 (whole string) */
18626{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018627 /* Need to do this (at least) once, since we can't initialize a union.
18628 * Will always be invoked when "v:progname" is set. */
18629 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18630
Bram Moolenaare9a41262005-01-15 22:18:47 +000018631 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018633 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018635 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018637 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638}
18639
18640/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018641 * Set List v: variable to "val".
18642 */
18643 void
18644set_vim_var_list(idx, val)
18645 int idx;
18646 list_T *val;
18647{
18648 list_unref(vimvars[idx].vv_list);
18649 vimvars[idx].vv_list = val;
18650 if (val != NULL)
18651 ++val->lv_refcount;
18652}
18653
18654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 * Set v:register if needed.
18656 */
18657 void
18658set_reg_var(c)
18659 int c;
18660{
18661 char_u regname;
18662
18663 if (c == 0 || c == ' ')
18664 regname = '"';
18665 else
18666 regname = c;
18667 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018668 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669 set_vim_var_string(VV_REG, &regname, 1);
18670}
18671
18672/*
18673 * Get or set v:exception. If "oldval" == NULL, return the current value.
18674 * Otherwise, restore the value to "oldval" and return NULL.
18675 * Must always be called in pairs to save and restore v:exception! Does not
18676 * take care of memory allocations.
18677 */
18678 char_u *
18679v_exception(oldval)
18680 char_u *oldval;
18681{
18682 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018683 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684
Bram Moolenaare9a41262005-01-15 22:18:47 +000018685 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 return NULL;
18687}
18688
18689/*
18690 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18691 * Otherwise, restore the value to "oldval" and return NULL.
18692 * Must always be called in pairs to save and restore v:throwpoint! Does not
18693 * take care of memory allocations.
18694 */
18695 char_u *
18696v_throwpoint(oldval)
18697 char_u *oldval;
18698{
18699 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018700 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701
Bram Moolenaare9a41262005-01-15 22:18:47 +000018702 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 return NULL;
18704}
18705
18706#if defined(FEAT_AUTOCMD) || defined(PROTO)
18707/*
18708 * Set v:cmdarg.
18709 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18710 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18711 * Must always be called in pairs!
18712 */
18713 char_u *
18714set_cmdarg(eap, oldarg)
18715 exarg_T *eap;
18716 char_u *oldarg;
18717{
18718 char_u *oldval;
18719 char_u *newval;
18720 unsigned len;
18721
Bram Moolenaare9a41262005-01-15 22:18:47 +000018722 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018723 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018725 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018726 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018727 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 }
18729
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018730 if (eap->force_bin == FORCE_BIN)
18731 len = 6;
18732 else if (eap->force_bin == FORCE_NOBIN)
18733 len = 8;
18734 else
18735 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018736
18737 if (eap->read_edit)
18738 len += 7;
18739
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018740 if (eap->force_ff != 0)
18741 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18742# ifdef FEAT_MBYTE
18743 if (eap->force_enc != 0)
18744 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018745 if (eap->bad_char != 0)
18746 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018747# endif
18748
18749 newval = alloc(len + 1);
18750 if (newval == NULL)
18751 return NULL;
18752
18753 if (eap->force_bin == FORCE_BIN)
18754 sprintf((char *)newval, " ++bin");
18755 else if (eap->force_bin == FORCE_NOBIN)
18756 sprintf((char *)newval, " ++nobin");
18757 else
18758 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018759
18760 if (eap->read_edit)
18761 STRCAT(newval, " ++edit");
18762
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018763 if (eap->force_ff != 0)
18764 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18765 eap->cmd + eap->force_ff);
18766# ifdef FEAT_MBYTE
18767 if (eap->force_enc != 0)
18768 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18769 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018770 if (eap->bad_char == BAD_KEEP)
18771 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18772 else if (eap->bad_char == BAD_DROP)
18773 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18774 else if (eap->bad_char != 0)
18775 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018776# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018777 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018778 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779}
18780#endif
18781
18782/*
18783 * Get the value of internal variable "name".
18784 * Return OK or FAIL.
18785 */
18786 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018787get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 char_u *name;
18789 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018790 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018791 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792{
18793 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018794 typval_T *tv = NULL;
18795 typval_T atv;
18796 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798
18799 /* truncate the name, so that we can use strcmp() */
18800 cc = name[len];
18801 name[len] = NUL;
18802
18803 /*
18804 * Check for "b:changedtick".
18805 */
18806 if (STRCMP(name, "b:changedtick") == 0)
18807 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018808 atv.v_type = VAR_NUMBER;
18809 atv.vval.v_number = curbuf->b_changedtick;
18810 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 }
18812
18813 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 * Check for user-defined variables.
18815 */
18816 else
18817 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018818 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018820 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 }
18822
Bram Moolenaare9a41262005-01-15 22:18:47 +000018823 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018825 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018826 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827 ret = FAIL;
18828 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018829 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018830 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831
18832 name[len] = cc;
18833
18834 return ret;
18835}
18836
18837/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018838 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18839 * Also handle function call with Funcref variable: func(expr)
18840 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18841 */
18842 static int
18843handle_subscript(arg, rettv, evaluate, verbose)
18844 char_u **arg;
18845 typval_T *rettv;
18846 int evaluate; /* do more than finding the end */
18847 int verbose; /* give error messages */
18848{
18849 int ret = OK;
18850 dict_T *selfdict = NULL;
18851 char_u *s;
18852 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018853 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018854
18855 while (ret == OK
18856 && (**arg == '['
18857 || (**arg == '.' && rettv->v_type == VAR_DICT)
18858 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18859 && !vim_iswhite(*(*arg - 1)))
18860 {
18861 if (**arg == '(')
18862 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018863 /* need to copy the funcref so that we can clear rettv */
18864 functv = *rettv;
18865 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018866
18867 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018868 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018869 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018870 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18871 &len, evaluate, selfdict);
18872
18873 /* Clear the funcref afterwards, so that deleting it while
18874 * evaluating the arguments is possible (see test55). */
18875 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018876
18877 /* Stop the expression evaluation when immediately aborting on
18878 * error, or when an interrupt occurred or an exception was thrown
18879 * but not caught. */
18880 if (aborting())
18881 {
18882 if (ret == OK)
18883 clear_tv(rettv);
18884 ret = FAIL;
18885 }
18886 dict_unref(selfdict);
18887 selfdict = NULL;
18888 }
18889 else /* **arg == '[' || **arg == '.' */
18890 {
18891 dict_unref(selfdict);
18892 if (rettv->v_type == VAR_DICT)
18893 {
18894 selfdict = rettv->vval.v_dict;
18895 if (selfdict != NULL)
18896 ++selfdict->dv_refcount;
18897 }
18898 else
18899 selfdict = NULL;
18900 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18901 {
18902 clear_tv(rettv);
18903 ret = FAIL;
18904 }
18905 }
18906 }
18907 dict_unref(selfdict);
18908 return ret;
18909}
18910
18911/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018912 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018913 * value).
18914 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018915 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018916alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018917{
Bram Moolenaar33570922005-01-25 22:26:29 +000018918 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018919}
18920
18921/*
18922 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 * The string "s" must have been allocated, it is consumed.
18924 * Return NULL for out of memory, the variable otherwise.
18925 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018926 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018927alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 char_u *s;
18929{
Bram Moolenaar33570922005-01-25 22:26:29 +000018930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018932 rettv = alloc_tv();
18933 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018935 rettv->v_type = VAR_STRING;
18936 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 }
18938 else
18939 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018940 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941}
18942
18943/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018944 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018946 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018947free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018948 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949{
18950 if (varp != NULL)
18951 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018952 switch (varp->v_type)
18953 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018954 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018955 func_unref(varp->vval.v_string);
18956 /*FALLTHROUGH*/
18957 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018958 vim_free(varp->vval.v_string);
18959 break;
18960 case VAR_LIST:
18961 list_unref(varp->vval.v_list);
18962 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018963 case VAR_DICT:
18964 dict_unref(varp->vval.v_dict);
18965 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018966 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018967#ifdef FEAT_FLOAT
18968 case VAR_FLOAT:
18969#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018970 case VAR_UNKNOWN:
18971 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018972 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018973 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018974 break;
18975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 vim_free(varp);
18977 }
18978}
18979
18980/*
18981 * Free the memory for a variable value and set the value to NULL or 0.
18982 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000018983 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018984clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018985 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986{
18987 if (varp != NULL)
18988 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018989 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018991 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018992 func_unref(varp->vval.v_string);
18993 /*FALLTHROUGH*/
18994 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018995 vim_free(varp->vval.v_string);
18996 varp->vval.v_string = NULL;
18997 break;
18998 case VAR_LIST:
18999 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019000 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019001 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019002 case VAR_DICT:
19003 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019004 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019005 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019006 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019007 varp->vval.v_number = 0;
19008 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019009#ifdef FEAT_FLOAT
19010 case VAR_FLOAT:
19011 varp->vval.v_float = 0.0;
19012 break;
19013#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019014 case VAR_UNKNOWN:
19015 break;
19016 default:
19017 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019019 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 }
19021}
19022
19023/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019024 * Set the value of a variable to NULL without freeing items.
19025 */
19026 static void
19027init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019028 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019029{
19030 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019031 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019032}
19033
19034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 * Get the number value of a variable.
19036 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019037 * For incompatible types, return 0.
19038 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19039 * caller of incompatible types: it sets *denote to TRUE if "denote"
19040 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 */
19042 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019043get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019044 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019046 int error = FALSE;
19047
19048 return get_tv_number_chk(varp, &error); /* return 0L on error */
19049}
19050
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019051 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019052get_tv_number_chk(varp, denote)
19053 typval_T *varp;
19054 int *denote;
19055{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019056 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019058 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019060 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019061 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019062#ifdef FEAT_FLOAT
19063 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019064 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019065 break;
19066#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019067 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019068 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019069 break;
19070 case VAR_STRING:
19071 if (varp->vval.v_string != NULL)
19072 vim_str2nr(varp->vval.v_string, NULL, NULL,
19073 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019074 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019075 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019076 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019077 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019078 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019079 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019080 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019081 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019082 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019083 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019085 if (denote == NULL) /* useful for values that must be unsigned */
19086 n = -1;
19087 else
19088 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019089 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090}
19091
19092/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019093 * Get the lnum from the first argument.
19094 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019095 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096 */
19097 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019098get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019099 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019100{
Bram Moolenaar33570922005-01-25 22:26:29 +000019101 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 linenr_T lnum;
19103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019104 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 if (lnum == 0) /* no valid number, try using line() */
19106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019107 rettv.v_type = VAR_NUMBER;
19108 f_line(argvars, &rettv);
19109 lnum = rettv.vval.v_number;
19110 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 }
19112 return lnum;
19113}
19114
19115/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019116 * Get the lnum from the first argument.
19117 * Also accepts "$", then "buf" is used.
19118 * Returns 0 on error.
19119 */
19120 static linenr_T
19121get_tv_lnum_buf(argvars, buf)
19122 typval_T *argvars;
19123 buf_T *buf;
19124{
19125 if (argvars[0].v_type == VAR_STRING
19126 && argvars[0].vval.v_string != NULL
19127 && argvars[0].vval.v_string[0] == '$'
19128 && buf != NULL)
19129 return buf->b_ml.ml_line_count;
19130 return get_tv_number_chk(&argvars[0], NULL);
19131}
19132
19133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 * Get the string value of a variable.
19135 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019136 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19137 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 * If the String variable has never been set, return an empty string.
19139 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019140 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19141 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 */
19143 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019144get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019145 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146{
19147 static char_u mybuf[NUMBUFLEN];
19148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019149 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150}
19151
19152 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019153get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019154 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019155 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019157 char_u *res = get_tv_string_buf_chk(varp, buf);
19158
19159 return res != NULL ? res : (char_u *)"";
19160}
19161
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019162 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019163get_tv_string_chk(varp)
19164 typval_T *varp;
19165{
19166 static char_u mybuf[NUMBUFLEN];
19167
19168 return get_tv_string_buf_chk(varp, mybuf);
19169}
19170
19171 static char_u *
19172get_tv_string_buf_chk(varp, buf)
19173 typval_T *varp;
19174 char_u *buf;
19175{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019176 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019178 case VAR_NUMBER:
19179 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19180 return buf;
19181 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019182 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019183 break;
19184 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019185 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019186 break;
19187 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019188 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019189 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019190#ifdef FEAT_FLOAT
19191 case VAR_FLOAT:
19192 EMSG(_("E806: using Float as a String"));
19193 break;
19194#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019195 case VAR_STRING:
19196 if (varp->vval.v_string != NULL)
19197 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019198 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019199 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019200 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019201 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019203 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204}
19205
19206/*
19207 * Find variable "name" in the list of variables.
19208 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019209 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019210 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019211 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019213 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019214find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019216 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019219 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220
Bram Moolenaara7043832005-01-21 11:56:39 +000019221 ht = find_var_ht(name, &varname);
19222 if (htp != NULL)
19223 *htp = ht;
19224 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019226 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227}
19228
19229/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019230 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019231 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019233 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019234find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019235 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019236 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019237 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019238{
Bram Moolenaar33570922005-01-25 22:26:29 +000019239 hashitem_T *hi;
19240
19241 if (*varname == NUL)
19242 {
19243 /* Must be something like "s:", otherwise "ht" would be NULL. */
19244 switch (varname[-2])
19245 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019246 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019247 case 'g': return &globvars_var;
19248 case 'v': return &vimvars_var;
19249 case 'b': return &curbuf->b_bufvar;
19250 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019251#ifdef FEAT_WINDOWS
19252 case 't': return &curtab->tp_winvar;
19253#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019254 case 'l': return current_funccal == NULL
19255 ? NULL : &current_funccal->l_vars_var;
19256 case 'a': return current_funccal == NULL
19257 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019258 }
19259 return NULL;
19260 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019261
19262 hi = hash_find(ht, varname);
19263 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019264 {
19265 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019266 * worked find the variable again. Don't auto-load a script if it was
19267 * loaded already, otherwise it would be loaded every time when
19268 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019269 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019270 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019271 hi = hash_find(ht, varname);
19272 if (HASHITEM_EMPTY(hi))
19273 return NULL;
19274 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019275 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019276}
19277
19278/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019279 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019280 * Set "varname" to the start of name without ':'.
19281 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019282 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019283find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284 char_u *name;
19285 char_u **varname;
19286{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019287 hashitem_T *hi;
19288
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 if (name[1] != ':')
19290 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019291 /* The name must not start with a colon or #. */
19292 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 return NULL;
19294 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019295
19296 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019297 hi = hash_find(&compat_hashtab, name);
19298 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019299 return &compat_hashtab;
19300
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019302 return &globvarht; /* global variable */
19303 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 }
19305 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019306 if (*name == 'g') /* global variable */
19307 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019308 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19309 */
19310 if (vim_strchr(name + 2, ':') != NULL
19311 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019312 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019314 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019316 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019317#ifdef FEAT_WINDOWS
19318 if (*name == 't') /* tab page variable */
19319 return &curtab->tp_vars.dv_hashtab;
19320#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019321 if (*name == 'v') /* v: variable */
19322 return &vimvarht;
19323 if (*name == 'a' && current_funccal != NULL) /* function argument */
19324 return &current_funccal->l_avars.dv_hashtab;
19325 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19326 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 if (*name == 's' /* script variable */
19328 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19329 return &SCRIPT_VARS(current_SID);
19330 return NULL;
19331}
19332
19333/*
19334 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019335 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 * Returns NULL when it doesn't exist.
19337 */
19338 char_u *
19339get_var_value(name)
19340 char_u *name;
19341{
Bram Moolenaar33570922005-01-25 22:26:29 +000019342 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343
Bram Moolenaara7043832005-01-21 11:56:39 +000019344 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 if (v == NULL)
19346 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019347 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348}
19349
19350/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019351 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352 * sourcing this script and when executing functions defined in the script.
19353 */
19354 void
19355new_script_vars(id)
19356 scid_T id;
19357{
Bram Moolenaara7043832005-01-21 11:56:39 +000019358 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019359 hashtab_T *ht;
19360 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019361
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19363 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019364 /* Re-allocating ga_data means that an ht_array pointing to
19365 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019366 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019367 for (i = 1; i <= ga_scripts.ga_len; ++i)
19368 {
19369 ht = &SCRIPT_VARS(i);
19370 if (ht->ht_mask == HT_INIT_SIZE - 1)
19371 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019372 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019373 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019374 }
19375
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 while (ga_scripts.ga_len < id)
19377 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019378 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019379 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019380 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 }
19383 }
19384}
19385
19386/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019387 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19388 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389 */
19390 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019391init_var_dict(dict, dict_var)
19392 dict_T *dict;
19393 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394{
Bram Moolenaar33570922005-01-25 22:26:29 +000019395 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019396 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019397 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019398 dict_var->di_tv.vval.v_dict = dict;
19399 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019400 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019401 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19402 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403}
19404
19405/*
19406 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019407 * Frees all allocated variables and the value they contain.
19408 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 */
19410 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019411vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019412 hashtab_T *ht;
19413{
19414 vars_clear_ext(ht, TRUE);
19415}
19416
19417/*
19418 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19419 */
19420 static void
19421vars_clear_ext(ht, free_val)
19422 hashtab_T *ht;
19423 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424{
Bram Moolenaara7043832005-01-21 11:56:39 +000019425 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019426 hashitem_T *hi;
19427 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019428
Bram Moolenaar33570922005-01-25 22:26:29 +000019429 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019430 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019431 for (hi = ht->ht_array; todo > 0; ++hi)
19432 {
19433 if (!HASHITEM_EMPTY(hi))
19434 {
19435 --todo;
19436
Bram Moolenaar33570922005-01-25 22:26:29 +000019437 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019438 * ht_array might change then. hash_clear() takes care of it
19439 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019440 v = HI2DI(hi);
19441 if (free_val)
19442 clear_tv(&v->di_tv);
19443 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19444 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019445 }
19446 }
19447 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019448 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449}
19450
Bram Moolenaara7043832005-01-21 11:56:39 +000019451/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019452 * Delete a variable from hashtab "ht" at item "hi".
19453 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019456delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019457 hashtab_T *ht;
19458 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459{
Bram Moolenaar33570922005-01-25 22:26:29 +000019460 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019461
19462 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019463 clear_tv(&di->di_tv);
19464 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465}
19466
19467/*
19468 * List the value of one internal variable.
19469 */
19470 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019471list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019472 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019474 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019476 char_u *tofree;
19477 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019478 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019479
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019480 current_copyID += COPYID_INC;
19481 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019482 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019483 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019484 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485}
19486
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019488list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 char_u *prefix;
19490 char_u *name;
19491 int type;
19492 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019493 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494{
Bram Moolenaar31859182007-08-14 20:41:13 +000019495 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19496 msg_start();
19497 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498 if (name != NULL) /* "a:" vars don't have a name stored */
19499 msg_puts(name);
19500 msg_putchar(' ');
19501 msg_advance(22);
19502 if (type == VAR_NUMBER)
19503 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019504 else if (type == VAR_FUNC)
19505 msg_putchar('*');
19506 else if (type == VAR_LIST)
19507 {
19508 msg_putchar('[');
19509 if (*string == '[')
19510 ++string;
19511 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019512 else if (type == VAR_DICT)
19513 {
19514 msg_putchar('{');
19515 if (*string == '{')
19516 ++string;
19517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518 else
19519 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019520
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019522
19523 if (type == VAR_FUNC)
19524 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019525 if (*first)
19526 {
19527 msg_clr_eos();
19528 *first = FALSE;
19529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530}
19531
19532/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019533 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 * If the variable already exists, the value is updated.
19535 * Otherwise the variable is created.
19536 */
19537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019538set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019540 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019541 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542{
Bram Moolenaar33570922005-01-25 22:26:29 +000019543 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019545 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019546 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019548 ht = find_var_ht(name, &varname);
19549 if (ht == NULL || *varname == NUL)
19550 {
19551 EMSG2(_(e_illvar), name);
19552 return;
19553 }
19554 v = find_var_in_ht(ht, varname, TRUE);
19555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019556 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019557 {
19558 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19559 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19560 ? name[2] : name[0]))
19561 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019562 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019563 return;
19564 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019565 /* Don't allow hiding a function. When "v" is not NULL we migth be
19566 * assigning another function to the same var, the type is checked
19567 * below. */
19568 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019569 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019570 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019571 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019572 return;
19573 }
19574 }
19575
Bram Moolenaar33570922005-01-25 22:26:29 +000019576 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019578 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019579 if (var_check_ro(v->di_flags, name)
19580 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019581 return;
19582 if (v->di_tv.v_type != tv->v_type
19583 && !((v->di_tv.v_type == VAR_STRING
19584 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019585 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019586 || tv->v_type == VAR_NUMBER))
19587#ifdef FEAT_FLOAT
19588 && !((v->di_tv.v_type == VAR_NUMBER
19589 || v->di_tv.v_type == VAR_FLOAT)
19590 && (tv->v_type == VAR_NUMBER
19591 || tv->v_type == VAR_FLOAT))
19592#endif
19593 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019594 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019595 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019596 return;
19597 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019598
19599 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019600 * Handle setting internal v: variables separately: we don't change
19601 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019602 */
19603 if (ht == &vimvarht)
19604 {
19605 if (v->di_tv.v_type == VAR_STRING)
19606 {
19607 vim_free(v->di_tv.vval.v_string);
19608 if (copy || tv->v_type != VAR_STRING)
19609 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19610 else
19611 {
19612 /* Take over the string to avoid an extra alloc/free. */
19613 v->di_tv.vval.v_string = tv->vval.v_string;
19614 tv->vval.v_string = NULL;
19615 }
19616 }
19617 else if (v->di_tv.v_type != VAR_NUMBER)
19618 EMSG2(_(e_intern2), "set_var()");
19619 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019620 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019621 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019622 if (STRCMP(varname, "searchforward") == 0)
19623 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19624 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 return;
19626 }
19627
19628 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 }
19630 else /* add a new variable */
19631 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019632 /* Can't add "v:" variable. */
19633 if (ht == &vimvarht)
19634 {
19635 EMSG2(_(e_illvar), name);
19636 return;
19637 }
19638
Bram Moolenaar92124a32005-06-17 22:03:40 +000019639 /* Make sure the variable name is valid. */
19640 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019641 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19642 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019643 {
19644 EMSG2(_(e_illvar), varname);
19645 return;
19646 }
19647
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019648 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19649 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019650 if (v == NULL)
19651 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019653 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019655 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 return;
19657 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019658 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019660
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019661 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019662 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019663 else
19664 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019665 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019666 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019667 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669}
19670
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019671/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019672 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019673 * Also give an error message.
19674 */
19675 static int
19676var_check_ro(flags, name)
19677 int flags;
19678 char_u *name;
19679{
19680 if (flags & DI_FLAGS_RO)
19681 {
19682 EMSG2(_(e_readonlyvar), name);
19683 return TRUE;
19684 }
19685 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19686 {
19687 EMSG2(_(e_readonlysbx), name);
19688 return TRUE;
19689 }
19690 return FALSE;
19691}
19692
19693/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019694 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19695 * Also give an error message.
19696 */
19697 static int
19698var_check_fixed(flags, name)
19699 int flags;
19700 char_u *name;
19701{
19702 if (flags & DI_FLAGS_FIX)
19703 {
19704 EMSG2(_("E795: Cannot delete variable %s"), name);
19705 return TRUE;
19706 }
19707 return FALSE;
19708}
19709
19710/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019711 * Return TRUE if typeval "tv" is set to be locked (immutable).
19712 * Also give an error message, using "name".
19713 */
19714 static int
19715tv_check_lock(lock, name)
19716 int lock;
19717 char_u *name;
19718{
19719 if (lock & VAR_LOCKED)
19720 {
19721 EMSG2(_("E741: Value is locked: %s"),
19722 name == NULL ? (char_u *)_("Unknown") : name);
19723 return TRUE;
19724 }
19725 if (lock & VAR_FIXED)
19726 {
19727 EMSG2(_("E742: Cannot change value of %s"),
19728 name == NULL ? (char_u *)_("Unknown") : name);
19729 return TRUE;
19730 }
19731 return FALSE;
19732}
19733
19734/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019735 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019736 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019737 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019738 * It is OK for "from" and "to" to point to the same item. This is used to
19739 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019740 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019741 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019742copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019743 typval_T *from;
19744 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019746 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019747 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019748 switch (from->v_type)
19749 {
19750 case VAR_NUMBER:
19751 to->vval.v_number = from->vval.v_number;
19752 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019753#ifdef FEAT_FLOAT
19754 case VAR_FLOAT:
19755 to->vval.v_float = from->vval.v_float;
19756 break;
19757#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019758 case VAR_STRING:
19759 case VAR_FUNC:
19760 if (from->vval.v_string == NULL)
19761 to->vval.v_string = NULL;
19762 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019763 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019764 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019765 if (from->v_type == VAR_FUNC)
19766 func_ref(to->vval.v_string);
19767 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019768 break;
19769 case VAR_LIST:
19770 if (from->vval.v_list == NULL)
19771 to->vval.v_list = NULL;
19772 else
19773 {
19774 to->vval.v_list = from->vval.v_list;
19775 ++to->vval.v_list->lv_refcount;
19776 }
19777 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019778 case VAR_DICT:
19779 if (from->vval.v_dict == NULL)
19780 to->vval.v_dict = NULL;
19781 else
19782 {
19783 to->vval.v_dict = from->vval.v_dict;
19784 ++to->vval.v_dict->dv_refcount;
19785 }
19786 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019787 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019788 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019789 break;
19790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791}
19792
19793/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019794 * Make a copy of an item.
19795 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019796 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19797 * reference to an already copied list/dict can be used.
19798 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019799 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019800 static int
19801item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019802 typval_T *from;
19803 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019804 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019805 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019806{
19807 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019808 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019809
Bram Moolenaar33570922005-01-25 22:26:29 +000019810 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019811 {
19812 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019813 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019814 }
19815 ++recurse;
19816
19817 switch (from->v_type)
19818 {
19819 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019820#ifdef FEAT_FLOAT
19821 case VAR_FLOAT:
19822#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019823 case VAR_STRING:
19824 case VAR_FUNC:
19825 copy_tv(from, to);
19826 break;
19827 case VAR_LIST:
19828 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019829 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019830 if (from->vval.v_list == NULL)
19831 to->vval.v_list = NULL;
19832 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19833 {
19834 /* use the copy made earlier */
19835 to->vval.v_list = from->vval.v_list->lv_copylist;
19836 ++to->vval.v_list->lv_refcount;
19837 }
19838 else
19839 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19840 if (to->vval.v_list == NULL)
19841 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019842 break;
19843 case VAR_DICT:
19844 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019845 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019846 if (from->vval.v_dict == NULL)
19847 to->vval.v_dict = NULL;
19848 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19849 {
19850 /* use the copy made earlier */
19851 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19852 ++to->vval.v_dict->dv_refcount;
19853 }
19854 else
19855 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19856 if (to->vval.v_dict == NULL)
19857 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019858 break;
19859 default:
19860 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019861 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019862 }
19863 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019864 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019865}
19866
19867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019868 * ":echo expr1 ..." print each argument separated with a space, add a
19869 * newline at the end.
19870 * ":echon expr1 ..." print each argument plain.
19871 */
19872 void
19873ex_echo(eap)
19874 exarg_T *eap;
19875{
19876 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019877 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019878 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 char_u *p;
19880 int needclr = TRUE;
19881 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019882 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883
19884 if (eap->skip)
19885 ++emsg_skip;
19886 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019888 /* If eval1() causes an error message the text from the command may
19889 * still need to be cleared. E.g., "echo 22,44". */
19890 need_clr_eos = needclr;
19891
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019893 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 {
19895 /*
19896 * Report the invalid expression unless the expression evaluation
19897 * has been cancelled due to an aborting error, an interrupt, or an
19898 * exception.
19899 */
19900 if (!aborting())
19901 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019902 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903 break;
19904 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019905 need_clr_eos = FALSE;
19906
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 if (!eap->skip)
19908 {
19909 if (atstart)
19910 {
19911 atstart = FALSE;
19912 /* Call msg_start() after eval1(), evaluating the expression
19913 * may cause a message to appear. */
19914 if (eap->cmdidx == CMD_echo)
19915 msg_start();
19916 }
19917 else if (eap->cmdidx == CMD_echo)
19918 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019919 current_copyID += COPYID_INC;
19920 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019921 if (p != NULL)
19922 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019924 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019926 if (*p != TAB && needclr)
19927 {
19928 /* remove any text still there from the command */
19929 msg_clr_eos();
19930 needclr = FALSE;
19931 }
19932 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 }
19934 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019935 {
19936#ifdef FEAT_MBYTE
19937 if (has_mbyte)
19938 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019939 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019940
19941 (void)msg_outtrans_len_attr(p, i, echo_attr);
19942 p += i - 1;
19943 }
19944 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019946 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019949 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019951 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 arg = skipwhite(arg);
19953 }
19954 eap->nextcmd = check_nextcmd(arg);
19955
19956 if (eap->skip)
19957 --emsg_skip;
19958 else
19959 {
19960 /* remove text that may still be there from the command */
19961 if (needclr)
19962 msg_clr_eos();
19963 if (eap->cmdidx == CMD_echo)
19964 msg_end();
19965 }
19966}
19967
19968/*
19969 * ":echohl {name}".
19970 */
19971 void
19972ex_echohl(eap)
19973 exarg_T *eap;
19974{
19975 int id;
19976
19977 id = syn_name2id(eap->arg);
19978 if (id == 0)
19979 echo_attr = 0;
19980 else
19981 echo_attr = syn_id2attr(id);
19982}
19983
19984/*
19985 * ":execute expr1 ..." execute the result of an expression.
19986 * ":echomsg expr1 ..." Print a message
19987 * ":echoerr expr1 ..." Print an error
19988 * Each gets spaces around each argument and a newline at the end for
19989 * echo commands
19990 */
19991 void
19992ex_execute(eap)
19993 exarg_T *eap;
19994{
19995 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019996 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 int ret = OK;
19998 char_u *p;
19999 garray_T ga;
20000 int len;
20001 int save_did_emsg;
20002
20003 ga_init2(&ga, 1, 80);
20004
20005 if (eap->skip)
20006 ++emsg_skip;
20007 while (*arg != NUL && *arg != '|' && *arg != '\n')
20008 {
20009 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020010 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 {
20012 /*
20013 * Report the invalid expression unless the expression evaluation
20014 * has been cancelled due to an aborting error, an interrupt, or an
20015 * exception.
20016 */
20017 if (!aborting())
20018 EMSG2(_(e_invexpr2), p);
20019 ret = FAIL;
20020 break;
20021 }
20022
20023 if (!eap->skip)
20024 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020025 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 len = (int)STRLEN(p);
20027 if (ga_grow(&ga, len + 2) == FAIL)
20028 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 ret = FAIL;
20031 break;
20032 }
20033 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 ga.ga_len += len;
20037 }
20038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020039 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 arg = skipwhite(arg);
20041 }
20042
20043 if (ret != FAIL && ga.ga_data != NULL)
20044 {
20045 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020048 out_flush();
20049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050 else if (eap->cmdidx == CMD_echoerr)
20051 {
20052 /* We don't want to abort following commands, restore did_emsg. */
20053 save_did_emsg = did_emsg;
20054 EMSG((char_u *)ga.ga_data);
20055 if (!force_abort)
20056 did_emsg = save_did_emsg;
20057 }
20058 else if (eap->cmdidx == CMD_execute)
20059 do_cmdline((char_u *)ga.ga_data,
20060 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20061 }
20062
20063 ga_clear(&ga);
20064
20065 if (eap->skip)
20066 --emsg_skip;
20067
20068 eap->nextcmd = check_nextcmd(arg);
20069}
20070
20071/*
20072 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20073 * "arg" points to the "&" or '+' when called, to "option" when returning.
20074 * Returns NULL when no option name found. Otherwise pointer to the char
20075 * after the option name.
20076 */
20077 static char_u *
20078find_option_end(arg, opt_flags)
20079 char_u **arg;
20080 int *opt_flags;
20081{
20082 char_u *p = *arg;
20083
20084 ++p;
20085 if (*p == 'g' && p[1] == ':')
20086 {
20087 *opt_flags = OPT_GLOBAL;
20088 p += 2;
20089 }
20090 else if (*p == 'l' && p[1] == ':')
20091 {
20092 *opt_flags = OPT_LOCAL;
20093 p += 2;
20094 }
20095 else
20096 *opt_flags = 0;
20097
20098 if (!ASCII_ISALPHA(*p))
20099 return NULL;
20100 *arg = p;
20101
20102 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20103 p += 4; /* termcap option */
20104 else
20105 while (ASCII_ISALPHA(*p))
20106 ++p;
20107 return p;
20108}
20109
20110/*
20111 * ":function"
20112 */
20113 void
20114ex_function(eap)
20115 exarg_T *eap;
20116{
20117 char_u *theline;
20118 int j;
20119 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 char_u *name = NULL;
20122 char_u *p;
20123 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020124 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 garray_T newargs;
20126 garray_T newlines;
20127 int varargs = FALSE;
20128 int mustend = FALSE;
20129 int flags = 0;
20130 ufunc_T *fp;
20131 int indent;
20132 int nesting;
20133 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020134 dictitem_T *v;
20135 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020136 static int func_nr = 0; /* number for nameless function */
20137 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020138 hashtab_T *ht;
20139 int todo;
20140 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020141 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142
20143 /*
20144 * ":function" without argument: list functions.
20145 */
20146 if (ends_excmd(*eap->arg))
20147 {
20148 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020149 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020150 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020151 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020152 {
20153 if (!HASHITEM_EMPTY(hi))
20154 {
20155 --todo;
20156 fp = HI2UF(hi);
20157 if (!isdigit(*fp->uf_name))
20158 list_func_head(fp, FALSE);
20159 }
20160 }
20161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 eap->nextcmd = check_nextcmd(eap->arg);
20163 return;
20164 }
20165
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020166 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020167 * ":function /pat": list functions matching pattern.
20168 */
20169 if (*eap->arg == '/')
20170 {
20171 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20172 if (!eap->skip)
20173 {
20174 regmatch_T regmatch;
20175
20176 c = *p;
20177 *p = NUL;
20178 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20179 *p = c;
20180 if (regmatch.regprog != NULL)
20181 {
20182 regmatch.rm_ic = p_ic;
20183
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020184 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020185 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20186 {
20187 if (!HASHITEM_EMPTY(hi))
20188 {
20189 --todo;
20190 fp = HI2UF(hi);
20191 if (!isdigit(*fp->uf_name)
20192 && vim_regexec(&regmatch, fp->uf_name, 0))
20193 list_func_head(fp, FALSE);
20194 }
20195 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020196 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020197 }
20198 }
20199 if (*p == '/')
20200 ++p;
20201 eap->nextcmd = check_nextcmd(p);
20202 return;
20203 }
20204
20205 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020206 * Get the function name. There are these situations:
20207 * func normal function name
20208 * "name" == func, "fudi.fd_dict" == NULL
20209 * dict.func new dictionary entry
20210 * "name" == NULL, "fudi.fd_dict" set,
20211 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20212 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020213 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020214 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20215 * dict.func existing dict entry that's not a Funcref
20216 * "name" == NULL, "fudi.fd_dict" set,
20217 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020220 name = trans_function_name(&p, eap->skip, 0, &fudi);
20221 paren = (vim_strchr(p, '(') != NULL);
20222 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 {
20224 /*
20225 * Return on an invalid expression in braces, unless the expression
20226 * evaluation has been cancelled due to an aborting error, an
20227 * interrupt, or an exception.
20228 */
20229 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020230 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020231 if (!eap->skip && fudi.fd_newkey != NULL)
20232 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020233 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236 else
20237 eap->skip = TRUE;
20238 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020239
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 /* An error in a function call during evaluation of an expression in magic
20241 * braces should not cause the function not to be defined. */
20242 saved_did_emsg = did_emsg;
20243 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244
20245 /*
20246 * ":function func" with only function name: list function.
20247 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020248 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 {
20250 if (!ends_excmd(*skipwhite(p)))
20251 {
20252 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020253 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 }
20255 eap->nextcmd = check_nextcmd(p);
20256 if (eap->nextcmd != NULL)
20257 *p = NUL;
20258 if (!eap->skip && !got_int)
20259 {
20260 fp = find_func(name);
20261 if (fp != NULL)
20262 {
20263 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020264 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020266 if (FUNCLINE(fp, j) == NULL)
20267 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268 msg_putchar('\n');
20269 msg_outnum((long)(j + 1));
20270 if (j < 9)
20271 msg_putchar(' ');
20272 if (j < 99)
20273 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020274 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 out_flush(); /* show a line at a time */
20276 ui_breakcheck();
20277 }
20278 if (!got_int)
20279 {
20280 msg_putchar('\n');
20281 msg_puts((char_u *)" endfunction");
20282 }
20283 }
20284 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020285 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020287 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 }
20289
20290 /*
20291 * ":function name(arg1, arg2)" Define function.
20292 */
20293 p = skipwhite(p);
20294 if (*p != '(')
20295 {
20296 if (!eap->skip)
20297 {
20298 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020299 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300 }
20301 /* attempt to continue by skipping some text */
20302 if (vim_strchr(p, '(') != NULL)
20303 p = vim_strchr(p, '(');
20304 }
20305 p = skipwhite(p + 1);
20306
20307 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20308 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20309
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020310 if (!eap->skip)
20311 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020312 /* Check the name of the function. Unless it's a dictionary function
20313 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020314 if (name != NULL)
20315 arg = name;
20316 else
20317 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020318 if (arg != NULL && (fudi.fd_di == NULL
20319 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020320 {
20321 if (*arg == K_SPECIAL)
20322 j = 3;
20323 else
20324 j = 0;
20325 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20326 : eval_isnamec(arg[j])))
20327 ++j;
20328 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020329 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020330 }
20331 }
20332
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 /*
20334 * Isolate the arguments: "arg1, arg2, ...)"
20335 */
20336 while (*p != ')')
20337 {
20338 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20339 {
20340 varargs = TRUE;
20341 p += 3;
20342 mustend = TRUE;
20343 }
20344 else
20345 {
20346 arg = p;
20347 while (ASCII_ISALNUM(*p) || *p == '_')
20348 ++p;
20349 if (arg == p || isdigit(*arg)
20350 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20351 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20352 {
20353 if (!eap->skip)
20354 EMSG2(_("E125: Illegal argument: %s"), arg);
20355 break;
20356 }
20357 if (ga_grow(&newargs, 1) == FAIL)
20358 goto erret;
20359 c = *p;
20360 *p = NUL;
20361 arg = vim_strsave(arg);
20362 if (arg == NULL)
20363 goto erret;
20364 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20365 *p = c;
20366 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 if (*p == ',')
20368 ++p;
20369 else
20370 mustend = TRUE;
20371 }
20372 p = skipwhite(p);
20373 if (mustend && *p != ')')
20374 {
20375 if (!eap->skip)
20376 EMSG2(_(e_invarg2), eap->arg);
20377 break;
20378 }
20379 }
20380 ++p; /* skip the ')' */
20381
Bram Moolenaare9a41262005-01-15 22:18:47 +000020382 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 for (;;)
20384 {
20385 p = skipwhite(p);
20386 if (STRNCMP(p, "range", 5) == 0)
20387 {
20388 flags |= FC_RANGE;
20389 p += 5;
20390 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020391 else if (STRNCMP(p, "dict", 4) == 0)
20392 {
20393 flags |= FC_DICT;
20394 p += 4;
20395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 else if (STRNCMP(p, "abort", 5) == 0)
20397 {
20398 flags |= FC_ABORT;
20399 p += 5;
20400 }
20401 else
20402 break;
20403 }
20404
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020405 /* When there is a line break use what follows for the function body.
20406 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20407 if (*p == '\n')
20408 line_arg = p + 1;
20409 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 EMSG(_(e_trailing));
20411
20412 /*
20413 * Read the body of the function, until ":endfunction" is found.
20414 */
20415 if (KeyTyped)
20416 {
20417 /* Check if the function already exists, don't let the user type the
20418 * whole function before telling him it doesn't work! For a script we
20419 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020420 if (!eap->skip && !eap->forceit)
20421 {
20422 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20423 EMSG(_(e_funcdict));
20424 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020425 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020428 if (!eap->skip && did_emsg)
20429 goto erret;
20430
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431 msg_putchar('\n'); /* don't overwrite the function name */
20432 cmdline_row = msg_row;
20433 }
20434
20435 indent = 2;
20436 nesting = 0;
20437 for (;;)
20438 {
20439 msg_scroll = TRUE;
20440 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020441 sourcing_lnum_off = sourcing_lnum;
20442
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020443 if (line_arg != NULL)
20444 {
20445 /* Use eap->arg, split up in parts by line breaks. */
20446 theline = line_arg;
20447 p = vim_strchr(theline, '\n');
20448 if (p == NULL)
20449 line_arg += STRLEN(line_arg);
20450 else
20451 {
20452 *p = NUL;
20453 line_arg = p + 1;
20454 }
20455 }
20456 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 theline = getcmdline(':', 0L, indent);
20458 else
20459 theline = eap->getline(':', eap->cookie, indent);
20460 if (KeyTyped)
20461 lines_left = Rows - 1;
20462 if (theline == NULL)
20463 {
20464 EMSG(_("E126: Missing :endfunction"));
20465 goto erret;
20466 }
20467
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020468 /* Detect line continuation: sourcing_lnum increased more than one. */
20469 if (sourcing_lnum > sourcing_lnum_off + 1)
20470 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20471 else
20472 sourcing_lnum_off = 0;
20473
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 if (skip_until != NULL)
20475 {
20476 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20477 * don't check for ":endfunc". */
20478 if (STRCMP(theline, skip_until) == 0)
20479 {
20480 vim_free(skip_until);
20481 skip_until = NULL;
20482 }
20483 }
20484 else
20485 {
20486 /* skip ':' and blanks*/
20487 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20488 ;
20489
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020490 /* Check for "endfunction". */
20491 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020493 if (line_arg == NULL)
20494 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 break;
20496 }
20497
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020498 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 * at "end". */
20500 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20501 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020502 else if (STRNCMP(p, "if", 2) == 0
20503 || STRNCMP(p, "wh", 2) == 0
20504 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 || STRNCMP(p, "try", 3) == 0)
20506 indent += 2;
20507
20508 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020509 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020511 if (*p == '!')
20512 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 p += eval_fname_script(p);
20514 if (ASCII_ISALPHA(*p))
20515 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020516 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 if (*skipwhite(p) == '(')
20518 {
20519 ++nesting;
20520 indent += 2;
20521 }
20522 }
20523 }
20524
20525 /* Check for ":append" or ":insert". */
20526 p = skip_range(p, NULL);
20527 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20528 || (p[0] == 'i'
20529 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20530 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20531 skip_until = vim_strsave((char_u *)".");
20532
20533 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20534 arg = skipwhite(skiptowhite(p));
20535 if (arg[0] == '<' && arg[1] =='<'
20536 && ((p[0] == 'p' && p[1] == 'y'
20537 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20538 || (p[0] == 'p' && p[1] == 'e'
20539 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20540 || (p[0] == 't' && p[1] == 'c'
20541 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20542 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20543 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020544 || (p[0] == 'm' && p[1] == 'z'
20545 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 ))
20547 {
20548 /* ":python <<" continues until a dot, like ":append" */
20549 p = skipwhite(arg + 2);
20550 if (*p == NUL)
20551 skip_until = vim_strsave((char_u *)".");
20552 else
20553 skip_until = vim_strsave(p);
20554 }
20555 }
20556
20557 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020558 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020559 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020560 if (line_arg == NULL)
20561 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020563 }
20564
20565 /* Copy the line to newly allocated memory. get_one_sourceline()
20566 * allocates 250 bytes per line, this saves 80% on average. The cost
20567 * is an extra alloc/free. */
20568 p = vim_strsave(theline);
20569 if (p != NULL)
20570 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020571 if (line_arg == NULL)
20572 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020573 theline = p;
20574 }
20575
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020576 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20577
20578 /* Add NULL lines for continuation lines, so that the line count is
20579 * equal to the index in the growarray. */
20580 while (sourcing_lnum_off-- > 0)
20581 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020582
20583 /* Check for end of eap->arg. */
20584 if (line_arg != NULL && *line_arg == NUL)
20585 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 }
20587
20588 /* Don't define the function when skipping commands or when an error was
20589 * detected. */
20590 if (eap->skip || did_emsg)
20591 goto erret;
20592
20593 /*
20594 * If there are no errors, add the function
20595 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020596 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020597 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020598 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020599 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020600 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020601 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020602 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020603 goto erret;
20604 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020605
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020606 fp = find_func(name);
20607 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020609 if (!eap->forceit)
20610 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020611 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020612 goto erret;
20613 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020614 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020615 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020616 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020617 name);
20618 goto erret;
20619 }
20620 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020621 ga_clear_strings(&(fp->uf_args));
20622 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020623 vim_free(name);
20624 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 }
20627 else
20628 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020629 char numbuf[20];
20630
20631 fp = NULL;
20632 if (fudi.fd_newkey == NULL && !eap->forceit)
20633 {
20634 EMSG(_(e_funcdict));
20635 goto erret;
20636 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020637 if (fudi.fd_di == NULL)
20638 {
20639 /* Can't add a function to a locked dictionary */
20640 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20641 goto erret;
20642 }
20643 /* Can't change an existing function if it is locked */
20644 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20645 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020646
20647 /* Give the function a sequential number. Can only be used with a
20648 * Funcref! */
20649 vim_free(name);
20650 sprintf(numbuf, "%d", ++func_nr);
20651 name = vim_strsave((char_u *)numbuf);
20652 if (name == NULL)
20653 goto erret;
20654 }
20655
20656 if (fp == NULL)
20657 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020658 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020659 {
20660 int slen, plen;
20661 char_u *scriptname;
20662
20663 /* Check that the autoload name matches the script name. */
20664 j = FAIL;
20665 if (sourcing_name != NULL)
20666 {
20667 scriptname = autoload_name(name);
20668 if (scriptname != NULL)
20669 {
20670 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020671 plen = (int)STRLEN(p);
20672 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020673 if (slen > plen && fnamecmp(p,
20674 sourcing_name + slen - plen) == 0)
20675 j = OK;
20676 vim_free(scriptname);
20677 }
20678 }
20679 if (j == FAIL)
20680 {
20681 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20682 goto erret;
20683 }
20684 }
20685
20686 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687 if (fp == NULL)
20688 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020689
20690 if (fudi.fd_dict != NULL)
20691 {
20692 if (fudi.fd_di == NULL)
20693 {
20694 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020695 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020696 if (fudi.fd_di == NULL)
20697 {
20698 vim_free(fp);
20699 goto erret;
20700 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020701 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20702 {
20703 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020704 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020705 goto erret;
20706 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020707 }
20708 else
20709 /* overwrite existing dict entry */
20710 clear_tv(&fudi.fd_di->di_tv);
20711 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020712 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020713 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020714 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020715
20716 /* behave like "dict" was used */
20717 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020718 }
20719
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020721 STRCPY(fp->uf_name, name);
20722 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020724 fp->uf_args = newargs;
20725 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020726#ifdef FEAT_PROFILE
20727 fp->uf_tml_count = NULL;
20728 fp->uf_tml_total = NULL;
20729 fp->uf_tml_self = NULL;
20730 fp->uf_profiling = FALSE;
20731 if (prof_def_func())
20732 func_do_profile(fp);
20733#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020734 fp->uf_varargs = varargs;
20735 fp->uf_flags = flags;
20736 fp->uf_calls = 0;
20737 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020738 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739
20740erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 ga_clear_strings(&newargs);
20742 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020743ret_free:
20744 vim_free(skip_until);
20745 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748}
20749
20750/*
20751 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020752 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020754 * flags:
20755 * TFN_INT: internal function name OK
20756 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757 * Advances "pp" to just after the function name (if no error).
20758 */
20759 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020760trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 char_u **pp;
20762 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020763 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020764 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020766 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767 char_u *start;
20768 char_u *end;
20769 int lead;
20770 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020772 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020773
20774 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020775 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020777
20778 /* Check for hard coded <SNR>: already translated function ID (from a user
20779 * command). */
20780 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20781 && (*pp)[2] == (int)KE_SNR)
20782 {
20783 *pp += 3;
20784 len = get_id_len(pp) + 3;
20785 return vim_strnsave(start, len);
20786 }
20787
20788 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20789 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020791 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020793
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020794 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20795 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020796 if (end == start)
20797 {
20798 if (!skip)
20799 EMSG(_("E129: Function name required"));
20800 goto theend;
20801 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020802 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020803 {
20804 /*
20805 * Report an invalid expression in braces, unless the expression
20806 * evaluation has been cancelled due to an aborting error, an
20807 * interrupt, or an exception.
20808 */
20809 if (!aborting())
20810 {
20811 if (end != NULL)
20812 EMSG2(_(e_invarg2), start);
20813 }
20814 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020815 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020816 goto theend;
20817 }
20818
20819 if (lv.ll_tv != NULL)
20820 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020821 if (fdp != NULL)
20822 {
20823 fdp->fd_dict = lv.ll_dict;
20824 fdp->fd_newkey = lv.ll_newkey;
20825 lv.ll_newkey = NULL;
20826 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020827 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020828 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20829 {
20830 name = vim_strsave(lv.ll_tv->vval.v_string);
20831 *pp = end;
20832 }
20833 else
20834 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020835 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20836 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020837 EMSG(_(e_funcref));
20838 else
20839 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020840 name = NULL;
20841 }
20842 goto theend;
20843 }
20844
20845 if (lv.ll_name == NULL)
20846 {
20847 /* Error found, but continue after the function name. */
20848 *pp = end;
20849 goto theend;
20850 }
20851
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020852 /* Check if the name is a Funcref. If so, use the value. */
20853 if (lv.ll_exp_name != NULL)
20854 {
20855 len = (int)STRLEN(lv.ll_exp_name);
20856 name = deref_func_name(lv.ll_exp_name, &len);
20857 if (name == lv.ll_exp_name)
20858 name = NULL;
20859 }
20860 else
20861 {
20862 len = (int)(end - *pp);
20863 name = deref_func_name(*pp, &len);
20864 if (name == *pp)
20865 name = NULL;
20866 }
20867 if (name != NULL)
20868 {
20869 name = vim_strsave(name);
20870 *pp = end;
20871 goto theend;
20872 }
20873
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020874 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020875 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020876 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020877 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20878 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20879 {
20880 /* When there was "s:" already or the name expanded to get a
20881 * leading "s:" then remove it. */
20882 lv.ll_name += 2;
20883 len -= 2;
20884 lead = 2;
20885 }
20886 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020887 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020888 {
20889 if (lead == 2) /* skip over "s:" */
20890 lv.ll_name += 2;
20891 len = (int)(end - lv.ll_name);
20892 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020893
20894 /*
20895 * Copy the function name to allocated memory.
20896 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20897 * Accept <SNR>123_name() outside a script.
20898 */
20899 if (skip)
20900 lead = 0; /* do nothing */
20901 else if (lead > 0)
20902 {
20903 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020904 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20905 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020906 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020907 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020908 if (current_SID <= 0)
20909 {
20910 EMSG(_(e_usingsid));
20911 goto theend;
20912 }
20913 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20914 lead += (int)STRLEN(sid_buf);
20915 }
20916 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020917 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020918 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020919 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020920 goto theend;
20921 }
20922 name = alloc((unsigned)(len + lead + 1));
20923 if (name != NULL)
20924 {
20925 if (lead > 0)
20926 {
20927 name[0] = K_SPECIAL;
20928 name[1] = KS_EXTRA;
20929 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020930 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020931 STRCPY(name + 3, sid_buf);
20932 }
20933 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20934 name[len + lead] = NUL;
20935 }
20936 *pp = end;
20937
20938theend:
20939 clear_lval(&lv);
20940 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941}
20942
20943/*
20944 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20945 * Return 2 if "p" starts with "s:".
20946 * Return 0 otherwise.
20947 */
20948 static int
20949eval_fname_script(p)
20950 char_u *p;
20951{
20952 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20953 || STRNICMP(p + 1, "SNR>", 4) == 0))
20954 return 5;
20955 if (p[0] == 's' && p[1] == ':')
20956 return 2;
20957 return 0;
20958}
20959
20960/*
20961 * Return TRUE if "p" starts with "<SID>" or "s:".
20962 * Only works if eval_fname_script() returned non-zero for "p"!
20963 */
20964 static int
20965eval_fname_sid(p)
20966 char_u *p;
20967{
20968 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20969}
20970
20971/*
20972 * List the head of the function: "name(arg1, arg2)".
20973 */
20974 static void
20975list_func_head(fp, indent)
20976 ufunc_T *fp;
20977 int indent;
20978{
20979 int j;
20980
20981 msg_start();
20982 if (indent)
20983 MSG_PUTS(" ");
20984 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020985 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 {
20987 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020988 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 }
20990 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020991 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020993 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 {
20995 if (j)
20996 MSG_PUTS(", ");
20997 msg_puts(FUNCARG(fp, j));
20998 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020999 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 {
21001 if (j)
21002 MSG_PUTS(", ");
21003 MSG_PUTS("...");
21004 }
21005 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021006 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021007 if (p_verbose > 0)
21008 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009}
21010
21011/*
21012 * Find a function by name, return pointer to it in ufuncs.
21013 * Return NULL for unknown function.
21014 */
21015 static ufunc_T *
21016find_func(name)
21017 char_u *name;
21018{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021019 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021021 hi = hash_find(&func_hashtab, name);
21022 if (!HASHITEM_EMPTY(hi))
21023 return HI2UF(hi);
21024 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025}
21026
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021027#if defined(EXITFREE) || defined(PROTO)
21028 void
21029free_all_functions()
21030{
21031 hashitem_T *hi;
21032
21033 /* Need to start all over every time, because func_free() may change the
21034 * hash table. */
21035 while (func_hashtab.ht_used > 0)
21036 for (hi = func_hashtab.ht_array; ; ++hi)
21037 if (!HASHITEM_EMPTY(hi))
21038 {
21039 func_free(HI2UF(hi));
21040 break;
21041 }
21042}
21043#endif
21044
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021045/*
21046 * Return TRUE if a function "name" exists.
21047 */
21048 static int
21049function_exists(name)
21050 char_u *name;
21051{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021052 char_u *nm = name;
21053 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021054 int n = FALSE;
21055
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021056 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021057 nm = skipwhite(nm);
21058
21059 /* Only accept "funcname", "funcname ", "funcname (..." and
21060 * "funcname(...", not "funcname!...". */
21061 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021062 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021063 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021064 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021065 else
21066 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021067 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021068 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021069 return n;
21070}
21071
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021072/*
21073 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021074 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021075 */
21076 static int
21077builtin_function(name)
21078 char_u *name;
21079{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021080 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21081 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021082}
21083
Bram Moolenaar05159a02005-02-26 23:04:13 +000021084#if defined(FEAT_PROFILE) || defined(PROTO)
21085/*
21086 * Start profiling function "fp".
21087 */
21088 static void
21089func_do_profile(fp)
21090 ufunc_T *fp;
21091{
21092 fp->uf_tm_count = 0;
21093 profile_zero(&fp->uf_tm_self);
21094 profile_zero(&fp->uf_tm_total);
21095 if (fp->uf_tml_count == NULL)
21096 fp->uf_tml_count = (int *)alloc_clear((unsigned)
21097 (sizeof(int) * fp->uf_lines.ga_len));
21098 if (fp->uf_tml_total == NULL)
21099 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
21100 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21101 if (fp->uf_tml_self == NULL)
21102 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
21103 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21104 fp->uf_tml_idx = -1;
21105 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21106 || fp->uf_tml_self == NULL)
21107 return; /* out of memory */
21108
21109 fp->uf_profiling = TRUE;
21110}
21111
21112/*
21113 * Dump the profiling results for all functions in file "fd".
21114 */
21115 void
21116func_dump_profile(fd)
21117 FILE *fd;
21118{
21119 hashitem_T *hi;
21120 int todo;
21121 ufunc_T *fp;
21122 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021123 ufunc_T **sorttab;
21124 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021125
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021126 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021127 if (todo == 0)
21128 return; /* nothing to dump */
21129
Bram Moolenaar73830342005-02-28 22:48:19 +000021130 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21131
Bram Moolenaar05159a02005-02-26 23:04:13 +000021132 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21133 {
21134 if (!HASHITEM_EMPTY(hi))
21135 {
21136 --todo;
21137 fp = HI2UF(hi);
21138 if (fp->uf_profiling)
21139 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021140 if (sorttab != NULL)
21141 sorttab[st_len++] = fp;
21142
Bram Moolenaar05159a02005-02-26 23:04:13 +000021143 if (fp->uf_name[0] == K_SPECIAL)
21144 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21145 else
21146 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21147 if (fp->uf_tm_count == 1)
21148 fprintf(fd, "Called 1 time\n");
21149 else
21150 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21151 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21152 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21153 fprintf(fd, "\n");
21154 fprintf(fd, "count total (s) self (s)\n");
21155
21156 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21157 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021158 if (FUNCLINE(fp, i) == NULL)
21159 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021160 prof_func_line(fd, fp->uf_tml_count[i],
21161 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021162 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21163 }
21164 fprintf(fd, "\n");
21165 }
21166 }
21167 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021168
21169 if (sorttab != NULL && st_len > 0)
21170 {
21171 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21172 prof_total_cmp);
21173 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21174 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21175 prof_self_cmp);
21176 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21177 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021178
21179 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021180}
Bram Moolenaar73830342005-02-28 22:48:19 +000021181
21182 static void
21183prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21184 FILE *fd;
21185 ufunc_T **sorttab;
21186 int st_len;
21187 char *title;
21188 int prefer_self; /* when equal print only self time */
21189{
21190 int i;
21191 ufunc_T *fp;
21192
21193 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21194 fprintf(fd, "count total (s) self (s) function\n");
21195 for (i = 0; i < 20 && i < st_len; ++i)
21196 {
21197 fp = sorttab[i];
21198 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21199 prefer_self);
21200 if (fp->uf_name[0] == K_SPECIAL)
21201 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21202 else
21203 fprintf(fd, " %s()\n", fp->uf_name);
21204 }
21205 fprintf(fd, "\n");
21206}
21207
21208/*
21209 * Print the count and times for one function or function line.
21210 */
21211 static void
21212prof_func_line(fd, count, total, self, prefer_self)
21213 FILE *fd;
21214 int count;
21215 proftime_T *total;
21216 proftime_T *self;
21217 int prefer_self; /* when equal print only self time */
21218{
21219 if (count > 0)
21220 {
21221 fprintf(fd, "%5d ", count);
21222 if (prefer_self && profile_equal(total, self))
21223 fprintf(fd, " ");
21224 else
21225 fprintf(fd, "%s ", profile_msg(total));
21226 if (!prefer_self && profile_equal(total, self))
21227 fprintf(fd, " ");
21228 else
21229 fprintf(fd, "%s ", profile_msg(self));
21230 }
21231 else
21232 fprintf(fd, " ");
21233}
21234
21235/*
21236 * Compare function for total time sorting.
21237 */
21238 static int
21239#ifdef __BORLANDC__
21240_RTLENTRYF
21241#endif
21242prof_total_cmp(s1, s2)
21243 const void *s1;
21244 const void *s2;
21245{
21246 ufunc_T *p1, *p2;
21247
21248 p1 = *(ufunc_T **)s1;
21249 p2 = *(ufunc_T **)s2;
21250 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21251}
21252
21253/*
21254 * Compare function for self time sorting.
21255 */
21256 static int
21257#ifdef __BORLANDC__
21258_RTLENTRYF
21259#endif
21260prof_self_cmp(s1, s2)
21261 const void *s1;
21262 const void *s2;
21263{
21264 ufunc_T *p1, *p2;
21265
21266 p1 = *(ufunc_T **)s1;
21267 p2 = *(ufunc_T **)s2;
21268 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21269}
21270
Bram Moolenaar05159a02005-02-26 23:04:13 +000021271#endif
21272
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021273/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021274 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021275 * Return TRUE if a package was loaded.
21276 */
21277 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021278script_autoload(name, reload)
21279 char_u *name;
21280 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021281{
21282 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021283 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021284 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021285 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021286
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021287 /* Return quickly when autoload disabled. */
21288 if (no_autoload)
21289 return FALSE;
21290
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021291 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021292 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021293 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021294 return FALSE;
21295
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021296 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021297
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021298 /* Find the name in the list of previously loaded package names. Skip
21299 * "autoload/", it's always the same. */
21300 for (i = 0; i < ga_loaded.ga_len; ++i)
21301 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21302 break;
21303 if (!reload && i < ga_loaded.ga_len)
21304 ret = FALSE; /* was loaded already */
21305 else
21306 {
21307 /* Remember the name if it wasn't loaded already. */
21308 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21309 {
21310 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21311 tofree = NULL;
21312 }
21313
21314 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021315 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021316 ret = TRUE;
21317 }
21318
21319 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021320 return ret;
21321}
21322
21323/*
21324 * Return the autoload script name for a function or variable name.
21325 * Returns NULL when out of memory.
21326 */
21327 static char_u *
21328autoload_name(name)
21329 char_u *name;
21330{
21331 char_u *p;
21332 char_u *scriptname;
21333
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021334 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021335 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21336 if (scriptname == NULL)
21337 return FALSE;
21338 STRCPY(scriptname, "autoload/");
21339 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021340 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021341 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021342 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021343 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021344 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021345}
21346
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21348
21349/*
21350 * Function given to ExpandGeneric() to obtain the list of user defined
21351 * function names.
21352 */
21353 char_u *
21354get_user_func_name(xp, idx)
21355 expand_T *xp;
21356 int idx;
21357{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021358 static long_u done;
21359 static hashitem_T *hi;
21360 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361
21362 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021364 done = 0;
21365 hi = func_hashtab.ht_array;
21366 }
21367 if (done < func_hashtab.ht_used)
21368 {
21369 if (done++ > 0)
21370 ++hi;
21371 while (HASHITEM_EMPTY(hi))
21372 ++hi;
21373 fp = HI2UF(hi);
21374
21375 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21376 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377
21378 cat_func_name(IObuff, fp);
21379 if (xp->xp_context != EXPAND_USER_FUNC)
21380 {
21381 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021382 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 STRCAT(IObuff, ")");
21384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 return IObuff;
21386 }
21387 return NULL;
21388}
21389
21390#endif /* FEAT_CMDL_COMPL */
21391
21392/*
21393 * Copy the function name of "fp" to buffer "buf".
21394 * "buf" must be able to hold the function name plus three bytes.
21395 * Takes care of script-local function names.
21396 */
21397 static void
21398cat_func_name(buf, fp)
21399 char_u *buf;
21400 ufunc_T *fp;
21401{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021402 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 {
21404 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021405 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406 }
21407 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021408 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409}
21410
21411/*
21412 * ":delfunction {name}"
21413 */
21414 void
21415ex_delfunction(eap)
21416 exarg_T *eap;
21417{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021418 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 char_u *p;
21420 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021421 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422
21423 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021424 name = trans_function_name(&p, eap->skip, 0, &fudi);
21425 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021427 {
21428 if (fudi.fd_dict != NULL && !eap->skip)
21429 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 if (!ends_excmd(*skipwhite(p)))
21433 {
21434 vim_free(name);
21435 EMSG(_(e_trailing));
21436 return;
21437 }
21438 eap->nextcmd = check_nextcmd(p);
21439 if (eap->nextcmd != NULL)
21440 *p = NUL;
21441
21442 if (!eap->skip)
21443 fp = find_func(name);
21444 vim_free(name);
21445
21446 if (!eap->skip)
21447 {
21448 if (fp == NULL)
21449 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021450 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451 return;
21452 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021453 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 {
21455 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21456 return;
21457 }
21458
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021459 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021461 /* Delete the dict item that refers to the function, it will
21462 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021463 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021465 else
21466 func_free(fp);
21467 }
21468}
21469
21470/*
21471 * Free a function and remove it from the list of functions.
21472 */
21473 static void
21474func_free(fp)
21475 ufunc_T *fp;
21476{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021477 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021478
21479 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021480 ga_clear_strings(&(fp->uf_args));
21481 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021482#ifdef FEAT_PROFILE
21483 vim_free(fp->uf_tml_count);
21484 vim_free(fp->uf_tml_total);
21485 vim_free(fp->uf_tml_self);
21486#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021487
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021488 /* remove the function from the function hashtable */
21489 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21490 if (HASHITEM_EMPTY(hi))
21491 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021492 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021493 hash_remove(&func_hashtab, hi);
21494
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021495 vim_free(fp);
21496}
21497
21498/*
21499 * Unreference a Function: decrement the reference count and free it when it
21500 * becomes zero. Only for numbered functions.
21501 */
21502 static void
21503func_unref(name)
21504 char_u *name;
21505{
21506 ufunc_T *fp;
21507
21508 if (name != NULL && isdigit(*name))
21509 {
21510 fp = find_func(name);
21511 if (fp == NULL)
21512 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021513 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021514 {
21515 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021516 * when "uf_calls" becomes zero. */
21517 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021518 func_free(fp);
21519 }
21520 }
21521}
21522
21523/*
21524 * Count a reference to a Function.
21525 */
21526 static void
21527func_ref(name)
21528 char_u *name;
21529{
21530 ufunc_T *fp;
21531
21532 if (name != NULL && isdigit(*name))
21533 {
21534 fp = find_func(name);
21535 if (fp == NULL)
21536 EMSG2(_(e_intern2), "func_ref()");
21537 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021538 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539 }
21540}
21541
21542/*
21543 * Call a user function.
21544 */
21545 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021546call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 ufunc_T *fp; /* pointer to function */
21548 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021549 typval_T *argvars; /* arguments */
21550 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551 linenr_T firstline; /* first line of range */
21552 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021553 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554{
Bram Moolenaar33570922005-01-25 22:26:29 +000021555 char_u *save_sourcing_name;
21556 linenr_T save_sourcing_lnum;
21557 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021558 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021559 int save_did_emsg;
21560 static int depth = 0;
21561 dictitem_T *v;
21562 int fixvar_idx = 0; /* index in fixvar[] */
21563 int i;
21564 int ai;
21565 char_u numbuf[NUMBUFLEN];
21566 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021567#ifdef FEAT_PROFILE
21568 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021569 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021571
21572 /* If depth of calling is getting too high, don't execute the function */
21573 if (depth >= p_mfd)
21574 {
21575 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021576 rettv->v_type = VAR_NUMBER;
21577 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 return;
21579 }
21580 ++depth;
21581
21582 line_breakcheck(); /* check for CTRL-C hit */
21583
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021584 fc = (funccall_T *)alloc(sizeof(funccall_T));
21585 fc->caller = current_funccal;
21586 current_funccal = fc;
21587 fc->func = fp;
21588 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021589 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021590 fc->linenr = 0;
21591 fc->returned = FALSE;
21592 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021594 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21595 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596
Bram Moolenaar33570922005-01-25 22:26:29 +000021597 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021598 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021599 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21600 * each argument variable and saves a lot of time.
21601 */
21602 /*
21603 * Init l: variables.
21604 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021605 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021606 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021607 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021608 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21609 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021610 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021611 name = v->di_key;
21612 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021613 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021614 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021615 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021616 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021617 v->di_tv.vval.v_dict = selfdict;
21618 ++selfdict->dv_refcount;
21619 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021620
Bram Moolenaar33570922005-01-25 22:26:29 +000021621 /*
21622 * Init a: variables.
21623 * Set a:0 to "argcount".
21624 * Set a:000 to a list with room for the "..." arguments.
21625 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021626 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21627 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021628 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021629 /* Use "name" to avoid a warning from some compiler that checks the
21630 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021631 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021632 name = v->di_key;
21633 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021634 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021635 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021636 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021637 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021638 v->di_tv.vval.v_list = &fc->l_varlist;
21639 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21640 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21641 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021642
21643 /*
21644 * Set a:firstline to "firstline" and a:lastline to "lastline".
21645 * Set a:name to named arguments.
21646 * Set a:N to the "..." arguments.
21647 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021648 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021649 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021650 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021651 (varnumber_T)lastline);
21652 for (i = 0; i < argcount; ++i)
21653 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021654 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021655 if (ai < 0)
21656 /* named argument a:name */
21657 name = FUNCARG(fp, i);
21658 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021659 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021660 /* "..." argument a:1, a:2, etc. */
21661 sprintf((char *)numbuf, "%d", ai + 1);
21662 name = numbuf;
21663 }
21664 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21665 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021666 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021667 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21668 }
21669 else
21670 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021671 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21672 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021673 if (v == NULL)
21674 break;
21675 v->di_flags = DI_FLAGS_RO;
21676 }
21677 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021678 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021679
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021680 /* Note: the values are copied directly to avoid alloc/free.
21681 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021682 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021683 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021684
21685 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21686 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021687 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21688 fc->l_listitems[ai].li_tv = argvars[i];
21689 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021690 }
21691 }
21692
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 /* Don't redraw while executing the function. */
21694 ++RedrawingDisabled;
21695 save_sourcing_name = sourcing_name;
21696 save_sourcing_lnum = sourcing_lnum;
21697 sourcing_lnum = 1;
21698 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021699 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 if (sourcing_name != NULL)
21701 {
21702 if (save_sourcing_name != NULL
21703 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21704 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21705 else
21706 STRCPY(sourcing_name, "function ");
21707 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21708
21709 if (p_verbose >= 12)
21710 {
21711 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021712 verbose_enter_scroll();
21713
Bram Moolenaar555b2802005-05-19 21:08:39 +000021714 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715 if (p_verbose >= 14)
21716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021718 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021719 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021720 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721
21722 msg_puts((char_u *)"(");
21723 for (i = 0; i < argcount; ++i)
21724 {
21725 if (i > 0)
21726 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021727 if (argvars[i].v_type == VAR_NUMBER)
21728 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 else
21730 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021731 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21732 if (s != NULL)
21733 {
21734 trunc_string(s, buf, MSG_BUF_CLEN);
21735 msg_puts(buf);
21736 vim_free(tofree);
21737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 }
21739 }
21740 msg_puts((char_u *)")");
21741 }
21742 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021743
21744 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 --no_wait_return;
21746 }
21747 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021748#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021749 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021750 {
21751 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21752 func_do_profile(fp);
21753 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021754 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021755 {
21756 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021757 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021758 profile_zero(&fp->uf_tm_children);
21759 }
21760 script_prof_save(&wait_start);
21761 }
21762#endif
21763
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021765 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766 save_did_emsg = did_emsg;
21767 did_emsg = FALSE;
21768
21769 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021770 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21772
21773 --RedrawingDisabled;
21774
21775 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021776 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021778 clear_tv(rettv);
21779 rettv->v_type = VAR_NUMBER;
21780 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 }
21782
Bram Moolenaar05159a02005-02-26 23:04:13 +000021783#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021784 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021785 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021786 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021787 profile_end(&call_start);
21788 profile_sub_wait(&wait_start, &call_start);
21789 profile_add(&fp->uf_tm_total, &call_start);
21790 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021791 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021792 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021793 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21794 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021795 }
21796 }
21797#endif
21798
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799 /* when being verbose, mention the return value */
21800 if (p_verbose >= 12)
21801 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021803 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021806 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021807 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021808 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021809 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021810 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021812 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021813 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021814 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021815 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021816
Bram Moolenaar555b2802005-05-19 21:08:39 +000021817 /* The value may be very long. Skip the middle part, so that we
21818 * have some idea how it starts and ends. smsg() would always
21819 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021820 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021821 if (s != NULL)
21822 {
21823 trunc_string(s, buf, MSG_BUF_CLEN);
21824 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21825 vim_free(tofree);
21826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 }
21828 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021829
21830 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831 --no_wait_return;
21832 }
21833
21834 vim_free(sourcing_name);
21835 sourcing_name = save_sourcing_name;
21836 sourcing_lnum = save_sourcing_lnum;
21837 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021838#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021839 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021840 script_prof_restore(&wait_start);
21841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842
21843 if (p_verbose >= 12 && sourcing_name != NULL)
21844 {
21845 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021846 verbose_enter_scroll();
21847
Bram Moolenaar555b2802005-05-19 21:08:39 +000021848 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021850
21851 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 --no_wait_return;
21853 }
21854
21855 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021856 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021858
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021859 /* If the a:000 list and the l: and a: dicts are not referenced we can
21860 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021861 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21862 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21863 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21864 {
21865 free_funccal(fc, FALSE);
21866 }
21867 else
21868 {
21869 hashitem_T *hi;
21870 listitem_T *li;
21871 int todo;
21872
21873 /* "fc" is still in use. This can happen when returning "a:000" or
21874 * assigning "l:" to a global variable.
21875 * Link "fc" in the list for garbage collection later. */
21876 fc->caller = previous_funccal;
21877 previous_funccal = fc;
21878
21879 /* Make a copy of the a: variables, since we didn't do that above. */
21880 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21881 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21882 {
21883 if (!HASHITEM_EMPTY(hi))
21884 {
21885 --todo;
21886 v = HI2DI(hi);
21887 copy_tv(&v->di_tv, &v->di_tv);
21888 }
21889 }
21890
21891 /* Make a copy of the a:000 items, since we didn't do that above. */
21892 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21893 copy_tv(&li->li_tv, &li->li_tv);
21894 }
21895}
21896
21897/*
21898 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021899 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021900 */
21901 static int
21902can_free_funccal(fc, copyID)
21903 funccall_T *fc;
21904 int copyID;
21905{
21906 return (fc->l_varlist.lv_copyID != copyID
21907 && fc->l_vars.dv_copyID != copyID
21908 && fc->l_avars.dv_copyID != copyID);
21909}
21910
21911/*
21912 * Free "fc" and what it contains.
21913 */
21914 static void
21915free_funccal(fc, free_val)
21916 funccall_T *fc;
21917 int free_val; /* a: vars were allocated */
21918{
21919 listitem_T *li;
21920
21921 /* The a: variables typevals may not have been allocated, only free the
21922 * allocated variables. */
21923 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21924
21925 /* free all l: variables */
21926 vars_clear(&fc->l_vars.dv_hashtab);
21927
21928 /* Free the a:000 variables if they were allocated. */
21929 if (free_val)
21930 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21931 clear_tv(&li->li_tv);
21932
21933 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934}
21935
21936/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021937 * Add a number variable "name" to dict "dp" with value "nr".
21938 */
21939 static void
21940add_nr_var(dp, v, name, nr)
21941 dict_T *dp;
21942 dictitem_T *v;
21943 char *name;
21944 varnumber_T nr;
21945{
21946 STRCPY(v->di_key, name);
21947 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21948 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21949 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021950 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021951 v->di_tv.vval.v_number = nr;
21952}
21953
21954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 * ":return [expr]"
21956 */
21957 void
21958ex_return(eap)
21959 exarg_T *eap;
21960{
21961 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021962 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963 int returning = FALSE;
21964
21965 if (current_funccal == NULL)
21966 {
21967 EMSG(_("E133: :return not inside a function"));
21968 return;
21969 }
21970
21971 if (eap->skip)
21972 ++emsg_skip;
21973
21974 eap->nextcmd = NULL;
21975 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021976 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 {
21978 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021979 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021981 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982 }
21983 /* It's safer to return also on error. */
21984 else if (!eap->skip)
21985 {
21986 /*
21987 * Return unless the expression evaluation has been cancelled due to an
21988 * aborting error, an interrupt, or an exception.
21989 */
21990 if (!aborting())
21991 returning = do_return(eap, FALSE, TRUE, NULL);
21992 }
21993
21994 /* When skipping or the return gets pending, advance to the next command
21995 * in this line (!returning). Otherwise, ignore the rest of the line.
21996 * Following lines will be ignored by get_func_line(). */
21997 if (returning)
21998 eap->nextcmd = NULL;
21999 else if (eap->nextcmd == NULL) /* no argument */
22000 eap->nextcmd = check_nextcmd(arg);
22001
22002 if (eap->skip)
22003 --emsg_skip;
22004}
22005
22006/*
22007 * Return from a function. Possibly makes the return pending. Also called
22008 * for a pending return at the ":endtry" or after returning from an extra
22009 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022010 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022011 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 * FALSE when the return gets pending.
22013 */
22014 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022015do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 exarg_T *eap;
22017 int reanimate;
22018 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022019 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020{
22021 int idx;
22022 struct condstack *cstack = eap->cstack;
22023
22024 if (reanimate)
22025 /* Undo the return. */
22026 current_funccal->returned = FALSE;
22027
22028 /*
22029 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22030 * not in its finally clause (which then is to be executed next) is found.
22031 * In this case, make the ":return" pending for execution at the ":endtry".
22032 * Otherwise, return normally.
22033 */
22034 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22035 if (idx >= 0)
22036 {
22037 cstack->cs_pending[idx] = CSTP_RETURN;
22038
22039 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022040 /* A pending return again gets pending. "rettv" points to an
22041 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022043 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 else
22045 {
22046 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022047 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022049 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022051 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 {
22053 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022054 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022055 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056 else
22057 EMSG(_(e_outofmem));
22058 }
22059 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022060 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061
22062 if (reanimate)
22063 {
22064 /* The pending return value could be overwritten by a ":return"
22065 * without argument in a finally clause; reset the default
22066 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022067 current_funccal->rettv->v_type = VAR_NUMBER;
22068 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069 }
22070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022071 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072 }
22073 else
22074 {
22075 current_funccal->returned = TRUE;
22076
22077 /* If the return is carried out now, store the return value. For
22078 * a return immediately after reanimation, the value is already
22079 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022080 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022082 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022083 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022085 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086 }
22087 }
22088
22089 return idx < 0;
22090}
22091
22092/*
22093 * Free the variable with a pending return value.
22094 */
22095 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022096discard_pending_return(rettv)
22097 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098{
Bram Moolenaar33570922005-01-25 22:26:29 +000022099 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100}
22101
22102/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022103 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104 * is an allocated string. Used by report_pending() for verbose messages.
22105 */
22106 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022107get_return_cmd(rettv)
22108 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022110 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022111 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022112 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022114 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022115 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022116 if (s == NULL)
22117 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022118
22119 STRCPY(IObuff, ":return ");
22120 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22121 if (STRLEN(s) + 8 >= IOSIZE)
22122 STRCPY(IObuff + IOSIZE - 4, "...");
22123 vim_free(tofree);
22124 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125}
22126
22127/*
22128 * Get next function line.
22129 * Called by do_cmdline() to get the next line.
22130 * Returns allocated string, or NULL for end of function.
22131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 char_u *
22133get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022134 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022136 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137{
Bram Moolenaar33570922005-01-25 22:26:29 +000022138 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022139 ufunc_T *fp = fcp->func;
22140 char_u *retval;
22141 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142
22143 /* If breakpoints have been added/deleted need to check for it. */
22144 if (fcp->dbg_tick != debug_tick)
22145 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022146 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147 sourcing_lnum);
22148 fcp->dbg_tick = debug_tick;
22149 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022150#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022151 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022152 func_line_end(cookie);
22153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154
Bram Moolenaar05159a02005-02-26 23:04:13 +000022155 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022156 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22157 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 retval = NULL;
22159 else
22160 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022161 /* Skip NULL lines (continuation lines). */
22162 while (fcp->linenr < gap->ga_len
22163 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22164 ++fcp->linenr;
22165 if (fcp->linenr >= gap->ga_len)
22166 retval = NULL;
22167 else
22168 {
22169 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22170 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022171#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022172 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022173 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022174#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176 }
22177
22178 /* Did we encounter a breakpoint? */
22179 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22180 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022181 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022183 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 sourcing_lnum);
22185 fcp->dbg_tick = debug_tick;
22186 }
22187
22188 return retval;
22189}
22190
Bram Moolenaar05159a02005-02-26 23:04:13 +000022191#if defined(FEAT_PROFILE) || defined(PROTO)
22192/*
22193 * Called when starting to read a function line.
22194 * "sourcing_lnum" must be correct!
22195 * When skipping lines it may not actually be executed, but we won't find out
22196 * until later and we need to store the time now.
22197 */
22198 void
22199func_line_start(cookie)
22200 void *cookie;
22201{
22202 funccall_T *fcp = (funccall_T *)cookie;
22203 ufunc_T *fp = fcp->func;
22204
22205 if (fp->uf_profiling && sourcing_lnum >= 1
22206 && sourcing_lnum <= fp->uf_lines.ga_len)
22207 {
22208 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022209 /* Skip continuation lines. */
22210 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22211 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022212 fp->uf_tml_execed = FALSE;
22213 profile_start(&fp->uf_tml_start);
22214 profile_zero(&fp->uf_tml_children);
22215 profile_get_wait(&fp->uf_tml_wait);
22216 }
22217}
22218
22219/*
22220 * Called when actually executing a function line.
22221 */
22222 void
22223func_line_exec(cookie)
22224 void *cookie;
22225{
22226 funccall_T *fcp = (funccall_T *)cookie;
22227 ufunc_T *fp = fcp->func;
22228
22229 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22230 fp->uf_tml_execed = TRUE;
22231}
22232
22233/*
22234 * Called when done with a function line.
22235 */
22236 void
22237func_line_end(cookie)
22238 void *cookie;
22239{
22240 funccall_T *fcp = (funccall_T *)cookie;
22241 ufunc_T *fp = fcp->func;
22242
22243 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22244 {
22245 if (fp->uf_tml_execed)
22246 {
22247 ++fp->uf_tml_count[fp->uf_tml_idx];
22248 profile_end(&fp->uf_tml_start);
22249 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022250 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022251 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22252 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022253 }
22254 fp->uf_tml_idx = -1;
22255 }
22256}
22257#endif
22258
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259/*
22260 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022261 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 */
22263 int
22264func_has_ended(cookie)
22265 void *cookie;
22266{
Bram Moolenaar33570922005-01-25 22:26:29 +000022267 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268
22269 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22270 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022271 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 || fcp->returned);
22273}
22274
22275/*
22276 * return TRUE if cookie indicates a function which "abort"s on errors.
22277 */
22278 int
22279func_has_abort(cookie)
22280 void *cookie;
22281{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022282 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283}
22284
22285#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22286typedef enum
22287{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022288 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22289 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22290 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291} var_flavour_T;
22292
22293static var_flavour_T var_flavour __ARGS((char_u *varname));
22294
22295 static var_flavour_T
22296var_flavour(varname)
22297 char_u *varname;
22298{
22299 char_u *p = varname;
22300
22301 if (ASCII_ISUPPER(*p))
22302 {
22303 while (*(++p))
22304 if (ASCII_ISLOWER(*p))
22305 return VAR_FLAVOUR_SESSION;
22306 return VAR_FLAVOUR_VIMINFO;
22307 }
22308 else
22309 return VAR_FLAVOUR_DEFAULT;
22310}
22311#endif
22312
22313#if defined(FEAT_VIMINFO) || defined(PROTO)
22314/*
22315 * Restore global vars that start with a capital from the viminfo file
22316 */
22317 int
22318read_viminfo_varlist(virp, writing)
22319 vir_T *virp;
22320 int writing;
22321{
22322 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022323 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022324 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325
22326 if (!writing && (find_viminfo_parameter('!') != NULL))
22327 {
22328 tab = vim_strchr(virp->vir_line + 1, '\t');
22329 if (tab != NULL)
22330 {
22331 *tab++ = '\0'; /* isolate the variable name */
22332 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022333 type = VAR_STRING;
22334#ifdef FEAT_FLOAT
22335 else if (*tab == 'F')
22336 type = VAR_FLOAT;
22337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338
22339 tab = vim_strchr(tab, '\t');
22340 if (tab != NULL)
22341 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022342 tv.v_type = type;
22343 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022344 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022346#ifdef FEAT_FLOAT
22347 else if (type == VAR_FLOAT)
22348 (void)string2float(tab + 1, &tv.vval.v_float);
22349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022351 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022352 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022353 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022354 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 }
22356 }
22357 }
22358
22359 return viminfo_readline(virp);
22360}
22361
22362/*
22363 * Write global vars that start with a capital to the viminfo file
22364 */
22365 void
22366write_viminfo_varlist(fp)
22367 FILE *fp;
22368{
Bram Moolenaar33570922005-01-25 22:26:29 +000022369 hashitem_T *hi;
22370 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022371 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022372 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022373 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022374 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022375 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376
22377 if (find_viminfo_parameter('!') == NULL)
22378 return;
22379
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022380 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022381
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022382 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022383 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022385 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022387 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022388 this_var = HI2DI(hi);
22389 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022390 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022391 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022392 {
22393 case VAR_STRING: s = "STR"; break;
22394 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022395#ifdef FEAT_FLOAT
22396 case VAR_FLOAT: s = "FLO"; break;
22397#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022398 default: continue;
22399 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022400 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022401 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022402 if (p != NULL)
22403 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022404 vim_free(tofree);
22405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 }
22407 }
22408}
22409#endif
22410
22411#if defined(FEAT_SESSION) || defined(PROTO)
22412 int
22413store_session_globals(fd)
22414 FILE *fd;
22415{
Bram Moolenaar33570922005-01-25 22:26:29 +000022416 hashitem_T *hi;
22417 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022418 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 char_u *p, *t;
22420
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022421 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022422 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022424 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022426 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022427 this_var = HI2DI(hi);
22428 if ((this_var->di_tv.v_type == VAR_NUMBER
22429 || this_var->di_tv.v_type == VAR_STRING)
22430 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022431 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022432 /* Escape special characters with a backslash. Turn a LF and
22433 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022434 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022435 (char_u *)"\\\"\n\r");
22436 if (p == NULL) /* out of memory */
22437 break;
22438 for (t = p; *t != NUL; ++t)
22439 if (*t == '\n')
22440 *t = 'n';
22441 else if (*t == '\r')
22442 *t = 'r';
22443 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022444 this_var->di_key,
22445 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22446 : ' ',
22447 p,
22448 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22449 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022450 || put_eol(fd) == FAIL)
22451 {
22452 vim_free(p);
22453 return FAIL;
22454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 vim_free(p);
22456 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022457#ifdef FEAT_FLOAT
22458 else if (this_var->di_tv.v_type == VAR_FLOAT
22459 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22460 {
22461 float_T f = this_var->di_tv.vval.v_float;
22462 int sign = ' ';
22463
22464 if (f < 0)
22465 {
22466 f = -f;
22467 sign = '-';
22468 }
22469 if ((fprintf(fd, "let %s = %c&%f",
22470 this_var->di_key, sign, f) < 0)
22471 || put_eol(fd) == FAIL)
22472 return FAIL;
22473 }
22474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 }
22476 }
22477 return OK;
22478}
22479#endif
22480
Bram Moolenaar661b1822005-07-28 22:36:45 +000022481/*
22482 * Display script name where an item was last set.
22483 * Should only be invoked when 'verbose' is non-zero.
22484 */
22485 void
22486last_set_msg(scriptID)
22487 scid_T scriptID;
22488{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022489 char_u *p;
22490
Bram Moolenaar661b1822005-07-28 22:36:45 +000022491 if (scriptID != 0)
22492 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022493 p = home_replace_save(NULL, get_scriptname(scriptID));
22494 if (p != NULL)
22495 {
22496 verbose_enter();
22497 MSG_PUTS(_("\n\tLast set from "));
22498 MSG_PUTS(p);
22499 vim_free(p);
22500 verbose_leave();
22501 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022502 }
22503}
22504
Bram Moolenaard812df62008-11-09 12:46:09 +000022505/*
22506 * List v:oldfiles in a nice way.
22507 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022508 void
22509ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022510 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022511{
22512 list_T *l = vimvars[VV_OLDFILES].vv_list;
22513 listitem_T *li;
22514 int nr = 0;
22515
22516 if (l == NULL)
22517 msg((char_u *)_("No old files"));
22518 else
22519 {
22520 msg_start();
22521 msg_scroll = TRUE;
22522 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22523 {
22524 msg_outnum((long)++nr);
22525 MSG_PUTS(": ");
22526 msg_outtrans(get_tv_string(&li->li_tv));
22527 msg_putchar('\n');
22528 out_flush(); /* output one line at a time */
22529 ui_breakcheck();
22530 }
22531 /* Assume "got_int" was set to truncate the listing. */
22532 got_int = FALSE;
22533
22534#ifdef FEAT_BROWSE_CMD
22535 if (cmdmod.browse)
22536 {
22537 quit_more = FALSE;
22538 nr = prompt_for_number(FALSE);
22539 msg_starthere();
22540 if (nr > 0)
22541 {
22542 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22543 (long)nr);
22544
22545 if (p != NULL)
22546 {
22547 p = expand_env_save(p);
22548 eap->arg = p;
22549 eap->cmdidx = CMD_edit;
22550 cmdmod.browse = FALSE;
22551 do_exedit(eap, NULL);
22552 vim_free(p);
22553 }
22554 }
22555 }
22556#endif
22557 }
22558}
22559
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560#endif /* FEAT_EVAL */
22561
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022563#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564
22565#ifdef WIN3264
22566/*
22567 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22568 */
22569static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22570static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22571static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22572
22573/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022574 * Get the short path (8.3) for the filename in "fnamep".
22575 * Only works for a valid file name.
22576 * When the path gets longer "fnamep" is changed and the allocated buffer
22577 * is put in "bufp".
22578 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22579 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 */
22581 static int
22582get_short_pathname(fnamep, bufp, fnamelen)
22583 char_u **fnamep;
22584 char_u **bufp;
22585 int *fnamelen;
22586{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022587 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588 char_u *newbuf;
22589
22590 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591 l = GetShortPathName(*fnamep, *fnamep, len);
22592 if (l > len - 1)
22593 {
22594 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022595 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 newbuf = vim_strnsave(*fnamep, l);
22597 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022598 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599
22600 vim_free(*bufp);
22601 *fnamep = *bufp = newbuf;
22602
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022603 /* Really should always succeed, as the buffer is big enough. */
22604 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 }
22606
22607 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022608 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609}
22610
22611/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022612 * Get the short path (8.3) for the filename in "fname". The converted
22613 * path is returned in "bufp".
22614 *
22615 * Some of the directories specified in "fname" may not exist. This function
22616 * will shorten the existing directories at the beginning of the path and then
22617 * append the remaining non-existing path.
22618 *
22619 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022620 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022621 * bufp - Pointer to an allocated buffer for the filename.
22622 * fnamelen - Length of the filename pointed to by fname
22623 *
22624 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625 */
22626 static int
22627shortpath_for_invalid_fname(fname, bufp, fnamelen)
22628 char_u **fname;
22629 char_u **bufp;
22630 int *fnamelen;
22631{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022632 char_u *short_fname, *save_fname, *pbuf_unused;
22633 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022635 int old_len, len;
22636 int new_len, sfx_len;
22637 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638
22639 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022640 old_len = *fnamelen;
22641 save_fname = vim_strnsave(*fname, old_len);
22642 pbuf_unused = NULL;
22643 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022645 endp = save_fname + old_len - 1; /* Find the end of the copy */
22646 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022648 /*
22649 * Try shortening the supplied path till it succeeds by removing one
22650 * directory at a time from the tail of the path.
22651 */
22652 len = 0;
22653 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022655 /* go back one path-separator */
22656 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22657 --endp;
22658 if (endp <= save_fname)
22659 break; /* processed the complete path */
22660
22661 /*
22662 * Replace the path separator with a NUL and try to shorten the
22663 * resulting path.
22664 */
22665 ch = *endp;
22666 *endp = 0;
22667 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022668 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022669 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22670 {
22671 retval = FAIL;
22672 goto theend;
22673 }
22674 *endp = ch; /* preserve the string */
22675
22676 if (len > 0)
22677 break; /* successfully shortened the path */
22678
22679 /* failed to shorten the path. Skip the path separator */
22680 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 }
22682
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022683 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022685 /*
22686 * Succeeded in shortening the path. Now concatenate the shortened
22687 * path with the remaining path at the tail.
22688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022690 /* Compute the length of the new path. */
22691 sfx_len = (int)(save_endp - endp) + 1;
22692 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022694 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022696 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022698 /* There is not enough space in the currently allocated string,
22699 * copy it to a buffer big enough. */
22700 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022702 {
22703 retval = FAIL;
22704 goto theend;
22705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 }
22707 else
22708 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022709 /* Transfer short_fname to the main buffer (it's big enough),
22710 * unless get_short_pathname() did its work in-place. */
22711 *fname = *bufp = save_fname;
22712 if (short_fname != save_fname)
22713 vim_strncpy(save_fname, short_fname, len);
22714 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022716
22717 /* concat the not-shortened part of the path */
22718 vim_strncpy(*fname + len, endp, sfx_len);
22719 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022721
22722theend:
22723 vim_free(pbuf_unused);
22724 vim_free(save_fname);
22725
22726 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727}
22728
22729/*
22730 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022731 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 */
22733 static int
22734shortpath_for_partial(fnamep, bufp, fnamelen)
22735 char_u **fnamep;
22736 char_u **bufp;
22737 int *fnamelen;
22738{
22739 int sepcount, len, tflen;
22740 char_u *p;
22741 char_u *pbuf, *tfname;
22742 int hasTilde;
22743
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022744 /* Count up the path separators from the RHS.. so we know which part
22745 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022747 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 if (vim_ispathsep(*p))
22749 ++sepcount;
22750
22751 /* Need full path first (use expand_env() to remove a "~/") */
22752 hasTilde = (**fnamep == '~');
22753 if (hasTilde)
22754 pbuf = tfname = expand_env_save(*fnamep);
22755 else
22756 pbuf = tfname = FullName_save(*fnamep, FALSE);
22757
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022758 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022760 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22761 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762
22763 if (len == 0)
22764 {
22765 /* Don't have a valid filename, so shorten the rest of the
22766 * path if we can. This CAN give us invalid 8.3 filenames, but
22767 * there's not a lot of point in guessing what it might be.
22768 */
22769 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022770 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22771 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 }
22773
22774 /* Count the paths backward to find the beginning of the desired string. */
22775 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022776 {
22777#ifdef FEAT_MBYTE
22778 if (has_mbyte)
22779 p -= mb_head_off(tfname, p);
22780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781 if (vim_ispathsep(*p))
22782 {
22783 if (sepcount == 0 || (hasTilde && sepcount == 1))
22784 break;
22785 else
22786 sepcount --;
22787 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 if (hasTilde)
22790 {
22791 --p;
22792 if (p >= tfname)
22793 *p = '~';
22794 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022795 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 }
22797 else
22798 ++p;
22799
22800 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22801 vim_free(*bufp);
22802 *fnamelen = (int)STRLEN(p);
22803 *bufp = pbuf;
22804 *fnamep = p;
22805
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022806 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807}
22808#endif /* WIN3264 */
22809
22810/*
22811 * Adjust a filename, according to a string of modifiers.
22812 * *fnamep must be NUL terminated when called. When returning, the length is
22813 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022814 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 * When there is an error, *fnamep is set to NULL.
22816 */
22817 int
22818modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22819 char_u *src; /* string with modifiers */
22820 int *usedlen; /* characters after src that are used */
22821 char_u **fnamep; /* file name so far */
22822 char_u **bufp; /* buffer for allocated file name or NULL */
22823 int *fnamelen; /* length of fnamep */
22824{
22825 int valid = 0;
22826 char_u *tail;
22827 char_u *s, *p, *pbuf;
22828 char_u dirname[MAXPATHL];
22829 int c;
22830 int has_fullname = 0;
22831#ifdef WIN3264
22832 int has_shortname = 0;
22833#endif
22834
22835repeat:
22836 /* ":p" - full path/file_name */
22837 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22838 {
22839 has_fullname = 1;
22840
22841 valid |= VALID_PATH;
22842 *usedlen += 2;
22843
22844 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22845 if ((*fnamep)[0] == '~'
22846#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22847 && ((*fnamep)[1] == '/'
22848# ifdef BACKSLASH_IN_FILENAME
22849 || (*fnamep)[1] == '\\'
22850# endif
22851 || (*fnamep)[1] == NUL)
22852
22853#endif
22854 )
22855 {
22856 *fnamep = expand_env_save(*fnamep);
22857 vim_free(*bufp); /* free any allocated file name */
22858 *bufp = *fnamep;
22859 if (*fnamep == NULL)
22860 return -1;
22861 }
22862
22863 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022864 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 {
22866 if (vim_ispathsep(*p)
22867 && p[1] == '.'
22868 && (p[2] == NUL
22869 || vim_ispathsep(p[2])
22870 || (p[2] == '.'
22871 && (p[3] == NUL || vim_ispathsep(p[3])))))
22872 break;
22873 }
22874
22875 /* FullName_save() is slow, don't use it when not needed. */
22876 if (*p != NUL || !vim_isAbsName(*fnamep))
22877 {
22878 *fnamep = FullName_save(*fnamep, *p != NUL);
22879 vim_free(*bufp); /* free any allocated file name */
22880 *bufp = *fnamep;
22881 if (*fnamep == NULL)
22882 return -1;
22883 }
22884
22885 /* Append a path separator to a directory. */
22886 if (mch_isdir(*fnamep))
22887 {
22888 /* Make room for one or two extra characters. */
22889 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22890 vim_free(*bufp); /* free any allocated file name */
22891 *bufp = *fnamep;
22892 if (*fnamep == NULL)
22893 return -1;
22894 add_pathsep(*fnamep);
22895 }
22896 }
22897
22898 /* ":." - path relative to the current directory */
22899 /* ":~" - path relative to the home directory */
22900 /* ":8" - shortname path - postponed till after */
22901 while (src[*usedlen] == ':'
22902 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22903 {
22904 *usedlen += 2;
22905 if (c == '8')
22906 {
22907#ifdef WIN3264
22908 has_shortname = 1; /* Postpone this. */
22909#endif
22910 continue;
22911 }
22912 pbuf = NULL;
22913 /* Need full path first (use expand_env() to remove a "~/") */
22914 if (!has_fullname)
22915 {
22916 if (c == '.' && **fnamep == '~')
22917 p = pbuf = expand_env_save(*fnamep);
22918 else
22919 p = pbuf = FullName_save(*fnamep, FALSE);
22920 }
22921 else
22922 p = *fnamep;
22923
22924 has_fullname = 0;
22925
22926 if (p != NULL)
22927 {
22928 if (c == '.')
22929 {
22930 mch_dirname(dirname, MAXPATHL);
22931 s = shorten_fname(p, dirname);
22932 if (s != NULL)
22933 {
22934 *fnamep = s;
22935 if (pbuf != NULL)
22936 {
22937 vim_free(*bufp); /* free any allocated file name */
22938 *bufp = pbuf;
22939 pbuf = NULL;
22940 }
22941 }
22942 }
22943 else
22944 {
22945 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22946 /* Only replace it when it starts with '~' */
22947 if (*dirname == '~')
22948 {
22949 s = vim_strsave(dirname);
22950 if (s != NULL)
22951 {
22952 *fnamep = s;
22953 vim_free(*bufp);
22954 *bufp = s;
22955 }
22956 }
22957 }
22958 vim_free(pbuf);
22959 }
22960 }
22961
22962 tail = gettail(*fnamep);
22963 *fnamelen = (int)STRLEN(*fnamep);
22964
22965 /* ":h" - head, remove "/file_name", can be repeated */
22966 /* Don't remove the first "/" or "c:\" */
22967 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22968 {
22969 valid |= VALID_HEAD;
22970 *usedlen += 2;
22971 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022972 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022973 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 *fnamelen = (int)(tail - *fnamep);
22975#ifdef VMS
22976 if (*fnamelen > 0)
22977 *fnamelen += 1; /* the path separator is part of the path */
22978#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022979 if (*fnamelen == 0)
22980 {
22981 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22982 p = vim_strsave((char_u *)".");
22983 if (p == NULL)
22984 return -1;
22985 vim_free(*bufp);
22986 *bufp = *fnamep = tail = p;
22987 *fnamelen = 1;
22988 }
22989 else
22990 {
22991 while (tail > s && !after_pathsep(s, tail))
22992 mb_ptr_back(*fnamep, tail);
22993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994 }
22995
22996 /* ":8" - shortname */
22997 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
22998 {
22999 *usedlen += 2;
23000#ifdef WIN3264
23001 has_shortname = 1;
23002#endif
23003 }
23004
23005#ifdef WIN3264
23006 /* Check shortname after we have done 'heads' and before we do 'tails'
23007 */
23008 if (has_shortname)
23009 {
23010 pbuf = NULL;
23011 /* Copy the string if it is shortened by :h */
23012 if (*fnamelen < (int)STRLEN(*fnamep))
23013 {
23014 p = vim_strnsave(*fnamep, *fnamelen);
23015 if (p == 0)
23016 return -1;
23017 vim_free(*bufp);
23018 *bufp = *fnamep = p;
23019 }
23020
23021 /* Split into two implementations - makes it easier. First is where
23022 * there isn't a full name already, second is where there is.
23023 */
23024 if (!has_fullname && !vim_isAbsName(*fnamep))
23025 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023026 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 return -1;
23028 }
23029 else
23030 {
23031 int l;
23032
23033 /* Simple case, already have the full-name
23034 * Nearly always shorter, so try first time. */
23035 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023036 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 return -1;
23038
23039 if (l == 0)
23040 {
23041 /* Couldn't find the filename.. search the paths.
23042 */
23043 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023044 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045 return -1;
23046 }
23047 *fnamelen = l;
23048 }
23049 }
23050#endif /* WIN3264 */
23051
23052 /* ":t" - tail, just the basename */
23053 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23054 {
23055 *usedlen += 2;
23056 *fnamelen -= (int)(tail - *fnamep);
23057 *fnamep = tail;
23058 }
23059
23060 /* ":e" - extension, can be repeated */
23061 /* ":r" - root, without extension, can be repeated */
23062 while (src[*usedlen] == ':'
23063 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23064 {
23065 /* find a '.' in the tail:
23066 * - for second :e: before the current fname
23067 * - otherwise: The last '.'
23068 */
23069 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23070 s = *fnamep - 2;
23071 else
23072 s = *fnamep + *fnamelen - 1;
23073 for ( ; s > tail; --s)
23074 if (s[0] == '.')
23075 break;
23076 if (src[*usedlen + 1] == 'e') /* :e */
23077 {
23078 if (s > tail)
23079 {
23080 *fnamelen += (int)(*fnamep - (s + 1));
23081 *fnamep = s + 1;
23082#ifdef VMS
23083 /* cut version from the extension */
23084 s = *fnamep + *fnamelen - 1;
23085 for ( ; s > *fnamep; --s)
23086 if (s[0] == ';')
23087 break;
23088 if (s > *fnamep)
23089 *fnamelen = s - *fnamep;
23090#endif
23091 }
23092 else if (*fnamep <= tail)
23093 *fnamelen = 0;
23094 }
23095 else /* :r */
23096 {
23097 if (s > tail) /* remove one extension */
23098 *fnamelen = (int)(s - *fnamep);
23099 }
23100 *usedlen += 2;
23101 }
23102
23103 /* ":s?pat?foo?" - substitute */
23104 /* ":gs?pat?foo?" - global substitute */
23105 if (src[*usedlen] == ':'
23106 && (src[*usedlen + 1] == 's'
23107 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23108 {
23109 char_u *str;
23110 char_u *pat;
23111 char_u *sub;
23112 int sep;
23113 char_u *flags;
23114 int didit = FALSE;
23115
23116 flags = (char_u *)"";
23117 s = src + *usedlen + 2;
23118 if (src[*usedlen + 1] == 'g')
23119 {
23120 flags = (char_u *)"g";
23121 ++s;
23122 }
23123
23124 sep = *s++;
23125 if (sep)
23126 {
23127 /* find end of pattern */
23128 p = vim_strchr(s, sep);
23129 if (p != NULL)
23130 {
23131 pat = vim_strnsave(s, (int)(p - s));
23132 if (pat != NULL)
23133 {
23134 s = p + 1;
23135 /* find end of substitution */
23136 p = vim_strchr(s, sep);
23137 if (p != NULL)
23138 {
23139 sub = vim_strnsave(s, (int)(p - s));
23140 str = vim_strnsave(*fnamep, *fnamelen);
23141 if (sub != NULL && str != NULL)
23142 {
23143 *usedlen = (int)(p + 1 - src);
23144 s = do_string_sub(str, pat, sub, flags);
23145 if (s != NULL)
23146 {
23147 *fnamep = s;
23148 *fnamelen = (int)STRLEN(s);
23149 vim_free(*bufp);
23150 *bufp = s;
23151 didit = TRUE;
23152 }
23153 }
23154 vim_free(sub);
23155 vim_free(str);
23156 }
23157 vim_free(pat);
23158 }
23159 }
23160 /* after using ":s", repeat all the modifiers */
23161 if (didit)
23162 goto repeat;
23163 }
23164 }
23165
23166 return valid;
23167}
23168
23169/*
23170 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23171 * "flags" can be "g" to do a global substitute.
23172 * Returns an allocated string, NULL for error.
23173 */
23174 char_u *
23175do_string_sub(str, pat, sub, flags)
23176 char_u *str;
23177 char_u *pat;
23178 char_u *sub;
23179 char_u *flags;
23180{
23181 int sublen;
23182 regmatch_T regmatch;
23183 int i;
23184 int do_all;
23185 char_u *tail;
23186 garray_T ga;
23187 char_u *ret;
23188 char_u *save_cpo;
23189
23190 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23191 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023192 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193
23194 ga_init2(&ga, 1, 200);
23195
23196 do_all = (flags[0] == 'g');
23197
23198 regmatch.rm_ic = p_ic;
23199 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23200 if (regmatch.regprog != NULL)
23201 {
23202 tail = str;
23203 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23204 {
23205 /*
23206 * Get some space for a temporary buffer to do the substitution
23207 * into. It will contain:
23208 * - The text up to where the match is.
23209 * - The substituted text.
23210 * - The text after the match.
23211 */
23212 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23213 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23214 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23215 {
23216 ga_clear(&ga);
23217 break;
23218 }
23219
23220 /* copy the text up to where the match is */
23221 i = (int)(regmatch.startp[0] - tail);
23222 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23223 /* add the substituted text */
23224 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23225 + ga.ga_len + i, TRUE, TRUE, FALSE);
23226 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227 /* avoid getting stuck on a match with an empty string */
23228 if (tail == regmatch.endp[0])
23229 {
23230 if (*tail == NUL)
23231 break;
23232 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23233 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234 }
23235 else
23236 {
23237 tail = regmatch.endp[0];
23238 if (*tail == NUL)
23239 break;
23240 }
23241 if (!do_all)
23242 break;
23243 }
23244
23245 if (ga.ga_data != NULL)
23246 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23247
23248 vim_free(regmatch.regprog);
23249 }
23250
23251 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23252 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023253 if (p_cpo == empty_option)
23254 p_cpo = save_cpo;
23255 else
23256 /* Darn, evaluating {sub} expression changed the value. */
23257 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258
23259 return ret;
23260}
23261
23262#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */