blob: 58c89c88d19a1b6e03c71742026fbf349a9fd7c7 [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 Moolenaar72597a52010-07-18 15:31:08 +0200703static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704#ifdef HAVE_STRFTIME
705static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
706#endif
707static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200713static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200714static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000715static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000720static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200721static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000723static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000724static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000726static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000727static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000729static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200730#ifdef FEAT_FLOAT
731static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
733#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000737#ifdef FEAT_FLOAT
738static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
739#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200741static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200742static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000752static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000755static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000757static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000758static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static int get_env_len __ARGS((char_u **arg));
760static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000761static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000762static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
763#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
764#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
765 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000766static 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 +0000767static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000769static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
770static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static typval_T *alloc_tv __ARGS((void));
772static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static void init_tv __ARGS((typval_T *varp));
774static long get_tv_number __ARGS((typval_T *varp));
775static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000776static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static char_u *get_tv_string __ARGS((typval_T *varp));
778static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000779static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000781static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
783static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
784static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000785static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
786static 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 +0000787static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
788static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000789static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000790static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000791static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
793static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
794static int eval_fname_script __ARGS((char_u *p));
795static int eval_fname_sid __ARGS((char_u *p));
796static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static ufunc_T *find_func __ARGS((char_u *name));
798static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000799static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800#ifdef FEAT_PROFILE
801static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000802static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
803static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
804static int
805# ifdef __BORLANDC__
806 _RTLENTRYF
807# endif
808 prof_total_cmp __ARGS((const void *s1, const void *s2));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000815static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000816static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000817static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static void func_free __ARGS((ufunc_T *fp));
819static void func_unref __ARGS((char_u *name));
820static void func_ref __ARGS((char_u *name));
821static 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 +0000822static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
823static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000825static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
826static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000827static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000828static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200831
832#ifdef EBCDIC
833static int compare_func_name __ARGS((const void *s1, const void *s2));
834static void sortFunctions __ARGS(());
835#endif
836
837
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000838/* Character used as separated in autoload function/variable names. */
839#define AUTOLOAD_CHAR '#'
840
Bram Moolenaar33570922005-01-25 22:26:29 +0000841/*
842 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000843 */
844 void
845eval_init()
846{
Bram Moolenaar33570922005-01-25 22:26:29 +0000847 int i;
848 struct vimvar *p;
849
850 init_var_dict(&globvardict, &globvars_var);
851 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000852 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000853 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000854
855 for (i = 0; i < VV_LEN; ++i)
856 {
857 p = &vimvars[i];
858 STRCPY(p->vv_di.di_key, p->vv_name);
859 if (p->vv_flags & VV_RO)
860 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
861 else if (p->vv_flags & VV_RO_SBX)
862 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
863 else
864 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000865
866 /* add to v: scope dict, unless the value is not always available */
867 if (p->vv_type != VAR_UNKNOWN)
868 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000870 /* add to compat scope dict */
871 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000872 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000873 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200874
875#ifdef EBCDIC
876 /*
877 * Sort the function table, to enable binary sort.
878 */
879 sortFunctions();
880#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000881}
882
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000883#if defined(EXITFREE) || defined(PROTO)
884 void
885eval_clear()
886{
887 int i;
888 struct vimvar *p;
889
890 for (i = 0; i < VV_LEN; ++i)
891 {
892 p = &vimvars[i];
893 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000894 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000895 vim_free(p->vv_str);
896 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000897 }
898 else if (p->vv_di.di_tv.v_type == VAR_LIST)
899 {
900 list_unref(p->vv_list);
901 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000902 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000903 }
904 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000905 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000906 hash_clear(&compat_hashtab);
907
Bram Moolenaard9fba312005-06-26 22:34:35 +0000908 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000909
910 /* global variables */
911 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000913 /* autoloaded script names */
914 ga_clear_strings(&ga_loaded);
915
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200916 /* script-local variables */
917 for (i = 1; i <= ga_scripts.ga_len; ++i)
918 {
919 vars_clear(&SCRIPT_VARS(i));
920 vim_free(SCRIPT_SV(i));
921 }
922 ga_clear(&ga_scripts);
923
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000924 /* unreferenced lists and dicts */
925 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000926
927 /* functions */
928 free_all_functions();
929 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930}
931#endif
932
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 * Return the name of the executed function.
935 */
936 char_u *
937func_name(cookie)
938 void *cookie;
939{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000940 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941}
942
943/*
944 * Return the address holding the next breakpoint line for a funccall cookie.
945 */
946 linenr_T *
947func_breakpoint(cookie)
948 void *cookie;
949{
Bram Moolenaar33570922005-01-25 22:26:29 +0000950 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951}
952
953/*
954 * Return the address holding the debug tick for a funccall cookie.
955 */
956 int *
957func_dbg_tick(cookie)
958 void *cookie;
959{
Bram Moolenaar33570922005-01-25 22:26:29 +0000960 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961}
962
963/*
964 * Return the nesting level for a funccall cookie.
965 */
966 int
967func_level(cookie)
968 void *cookie;
969{
Bram Moolenaar33570922005-01-25 22:26:29 +0000970 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000974funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000976/* pointer to list of previously used funccal, still around because some
977 * item in it is still being used. */
978funccall_T *previous_funccal = NULL;
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980/*
981 * Return TRUE when a function was ended by a ":return" command.
982 */
983 int
984current_func_returned()
985{
986 return current_funccal->returned;
987}
988
989
990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 * Set an internal variable to a string value. Creates the variable if it does
992 * not already exist.
993 */
994 void
995set_internal_string_var(name, value)
996 char_u *name;
997 char_u *value;
998{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000999 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001000 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002 val = vim_strsave(value);
1003 if (val != NULL)
1004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001005 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001006 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 }
1012}
1013
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001014static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001015static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static char_u *redir_endp = NULL;
1017static char_u *redir_varname = NULL;
1018
1019/*
1020 * Start recording command output to a variable
1021 * Returns OK if successfully completed the setup. FAIL otherwise.
1022 */
1023 int
1024var_redir_start(name, append)
1025 char_u *name;
1026 int append; /* append to an existing variable */
1027{
1028 int save_emsg;
1029 int err;
1030 typval_T tv;
1031
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001032 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001033 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001034 {
1035 EMSG(_(e_invarg));
1036 return FAIL;
1037 }
1038
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001039 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040 redir_varname = vim_strsave(name);
1041 if (redir_varname == NULL)
1042 return FAIL;
1043
1044 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1045 if (redir_lval == NULL)
1046 {
1047 var_redir_stop();
1048 return FAIL;
1049 }
1050
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001051 /* The output is stored in growarray "redir_ga" until redirection ends. */
1052 ga_init2(&redir_ga, (int)sizeof(char), 500);
1053
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1056 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1058 {
1059 if (redir_endp != NULL && *redir_endp != NUL)
1060 /* Trailing characters are present after the variable name */
1061 EMSG(_(e_trailing));
1062 else
1063 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001064 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 var_redir_stop();
1066 return FAIL;
1067 }
1068
1069 /* check if we can write to the variable: set it to or append an empty
1070 * string */
1071 save_emsg = did_emsg;
1072 did_emsg = FALSE;
1073 tv.v_type = VAR_STRING;
1074 tv.vval.v_string = (char_u *)"";
1075 if (append)
1076 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1077 else
1078 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1079 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001080 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 if (err)
1082 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001083 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 var_redir_stop();
1085 return FAIL;
1086 }
1087 if (redir_lval->ll_newkey != NULL)
1088 {
1089 /* Dictionary item was created, don't do it again. */
1090 vim_free(redir_lval->ll_newkey);
1091 redir_lval->ll_newkey = NULL;
1092 }
1093
1094 return OK;
1095}
1096
1097/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001098 * Append "value[value_len]" to the variable set by var_redir_start().
1099 * The actual appending is postponed until redirection ends, because the value
1100 * appended may in fact be the string we write to, changing it may cause freed
1101 * memory to be used:
1102 * :redir => foo
1103 * :let foo
1104 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 */
1106 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001111 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112
1113 if (redir_lval == NULL)
1114 return;
1115
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001121 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122 {
1123 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125 }
1126 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128}
1129
1130/*
1131 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001132 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 */
1134 void
1135var_redir_stop()
1136{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001137 typval_T tv;
1138
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 if (redir_lval != NULL)
1140 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001141 /* If there was no error: assign the text to the variable. */
1142 if (redir_endp != NULL)
1143 {
1144 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1145 tv.v_type = VAR_STRING;
1146 tv.vval.v_string = redir_ga.ga_data;
1147 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1148 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001149
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 /* free the collected output */
1151 vim_free(redir_ga.ga_data);
1152 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 clear_lval(redir_lval);
1155 vim_free(redir_lval);
1156 redir_lval = NULL;
1157 }
1158 vim_free(redir_varname);
1159 redir_varname = NULL;
1160}
1161
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162# if defined(FEAT_MBYTE) || defined(PROTO)
1163 int
1164eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1165 char_u *enc_from;
1166 char_u *enc_to;
1167 char_u *fname_from;
1168 char_u *fname_to;
1169{
1170 int err = FALSE;
1171
1172 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1173 set_vim_var_string(VV_CC_TO, enc_to, -1);
1174 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1175 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1176 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1177 err = TRUE;
1178 set_vim_var_string(VV_CC_FROM, NULL, -1);
1179 set_vim_var_string(VV_CC_TO, NULL, -1);
1180 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1181 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1182
1183 if (err)
1184 return FAIL;
1185 return OK;
1186}
1187# endif
1188
1189# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1190 int
1191eval_printexpr(fname, args)
1192 char_u *fname;
1193 char_u *args;
1194{
1195 int err = FALSE;
1196
1197 set_vim_var_string(VV_FNAME_IN, fname, -1);
1198 set_vim_var_string(VV_CMDARG, args, -1);
1199 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1200 err = TRUE;
1201 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1202 set_vim_var_string(VV_CMDARG, NULL, -1);
1203
1204 if (err)
1205 {
1206 mch_remove(fname);
1207 return FAIL;
1208 }
1209 return OK;
1210}
1211# endif
1212
1213# if defined(FEAT_DIFF) || defined(PROTO)
1214 void
1215eval_diff(origfile, newfile, outfile)
1216 char_u *origfile;
1217 char_u *newfile;
1218 char_u *outfile;
1219{
1220 int err = FALSE;
1221
1222 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1223 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1224 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1225 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1226 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1227 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1228 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1229}
1230
1231 void
1232eval_patch(origfile, difffile, outfile)
1233 char_u *origfile;
1234 char_u *difffile;
1235 char_u *outfile;
1236{
1237 int err;
1238
1239 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1240 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1241 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1242 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1243 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1244 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1245 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1246}
1247# endif
1248
1249/*
1250 * Top level evaluation function, returning a boolean.
1251 * Sets "error" to TRUE if there was an error.
1252 * Return TRUE or FALSE.
1253 */
1254 int
1255eval_to_bool(arg, error, nextcmd, skip)
1256 char_u *arg;
1257 int *error;
1258 char_u **nextcmd;
1259 int skip; /* only parse, don't execute */
1260{
Bram Moolenaar33570922005-01-25 22:26:29 +00001261 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 int retval = FALSE;
1263
1264 if (skip)
1265 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 else
1269 {
1270 *error = FALSE;
1271 if (!skip)
1272 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001273 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001274 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
1276 }
1277 if (skip)
1278 --emsg_skip;
1279
1280 return retval;
1281}
1282
1283/*
1284 * Top level evaluation function, returning a string. If "skip" is TRUE,
1285 * only parsing to "nextcmd" is done, without reporting errors. Return
1286 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1287 */
1288 char_u *
1289eval_to_string_skip(arg, nextcmd, skip)
1290 char_u *arg;
1291 char_u **nextcmd;
1292 int skip; /* only parse, don't execute */
1293{
Bram Moolenaar33570922005-01-25 22:26:29 +00001294 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 char_u *retval;
1296
1297 if (skip)
1298 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001299 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 retval = NULL;
1301 else
1302 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 retval = vim_strsave(get_tv_string(&tv));
1304 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306 if (skip)
1307 --emsg_skip;
1308
1309 return retval;
1310}
1311
1312/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001313 * Skip over an expression at "*pp".
1314 * Return FAIL for an error, OK otherwise.
1315 */
1316 int
1317skip_expr(pp)
1318 char_u **pp;
1319{
Bram Moolenaar33570922005-01-25 22:26:29 +00001320 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001321
1322 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001323 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324}
1325
1326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001328 * When "convert" is TRUE convert a List into a sequence of lines and convert
1329 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * Return pointer to allocated memory, or NULL for failure.
1331 */
1332 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001333eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 char_u *arg;
1335 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar33570922005-01-25 22:26:29 +00001338 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001340 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001341#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001342 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 retval = NULL;
1347 else
1348 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001350 {
1351 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001352 if (tv.vval.v_list != NULL)
1353 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001354 ga_append(&ga, NUL);
1355 retval = (char_u *)ga.ga_data;
1356 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001357#ifdef FEAT_FLOAT
1358 else if (convert && tv.v_type == VAR_FLOAT)
1359 {
1360 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1361 retval = vim_strsave(numbuf);
1362 }
1363#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001364 else
1365 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368
1369 return retval;
1370}
1371
1372/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001373 * Call eval_to_string() without using current local variables and using
1374 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 */
1376 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001377eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 char_u *arg;
1379 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001380 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
1382 char_u *retval;
1383 void *save_funccalp;
1384
1385 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001386 if (use_sandbox)
1387 ++sandbox;
1388 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001389 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001390 if (use_sandbox)
1391 --sandbox;
1392 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 restore_funccal(save_funccalp);
1394 return retval;
1395}
1396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397/*
1398 * Top level evaluation function, returning a number.
1399 * Evaluates "expr" silently.
1400 * Returns -1 for an error.
1401 */
1402 int
1403eval_to_number(expr)
1404 char_u *expr;
1405{
Bram Moolenaar33570922005-01-25 22:26:29 +00001406 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001408 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
1410 ++emsg_off;
1411
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 retval = -1;
1414 else
1415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001416 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 --emsg_off;
1420
1421 return retval;
1422}
1423
Bram Moolenaara40058a2005-07-11 22:42:07 +00001424/*
1425 * Prepare v: variable "idx" to be used.
1426 * Save the current typeval in "save_tv".
1427 * When not used yet add the variable to the v: hashtable.
1428 */
1429 static void
1430prepare_vimvar(idx, save_tv)
1431 int idx;
1432 typval_T *save_tv;
1433{
1434 *save_tv = vimvars[idx].vv_tv;
1435 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1436 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1437}
1438
1439/*
1440 * Restore v: variable "idx" to typeval "save_tv".
1441 * When no longer defined, remove the variable from the v: hashtable.
1442 */
1443 static void
1444restore_vimvar(idx, save_tv)
1445 int idx;
1446 typval_T *save_tv;
1447{
1448 hashitem_T *hi;
1449
Bram Moolenaara40058a2005-07-11 22:42:07 +00001450 vimvars[idx].vv_tv = *save_tv;
1451 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1452 {
1453 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1454 if (HASHITEM_EMPTY(hi))
1455 EMSG2(_(e_intern2), "restore_vimvar()");
1456 else
1457 hash_remove(&vimvarht, hi);
1458 }
1459}
1460
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001461#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001462/*
1463 * Evaluate an expression to a list with suggestions.
1464 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001465 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001466 */
1467 list_T *
1468eval_spell_expr(badword, expr)
1469 char_u *badword;
1470 char_u *expr;
1471{
1472 typval_T save_val;
1473 typval_T rettv;
1474 list_T *list = NULL;
1475 char_u *p = skipwhite(expr);
1476
1477 /* Set "v:val" to the bad word. */
1478 prepare_vimvar(VV_VAL, &save_val);
1479 vimvars[VV_VAL].vv_type = VAR_STRING;
1480 vimvars[VV_VAL].vv_str = badword;
1481 if (p_verbose == 0)
1482 ++emsg_off;
1483
1484 if (eval1(&p, &rettv, TRUE) == OK)
1485 {
1486 if (rettv.v_type != VAR_LIST)
1487 clear_tv(&rettv);
1488 else
1489 list = rettv.vval.v_list;
1490 }
1491
1492 if (p_verbose == 0)
1493 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001494 restore_vimvar(VV_VAL, &save_val);
1495
1496 return list;
1497}
1498
1499/*
1500 * "list" is supposed to contain two items: a word and a number. Return the
1501 * word in "pp" and the number as the return value.
1502 * Return -1 if anything isn't right.
1503 * Used to get the good word and score from the eval_spell_expr() result.
1504 */
1505 int
1506get_spellword(list, pp)
1507 list_T *list;
1508 char_u **pp;
1509{
1510 listitem_T *li;
1511
1512 li = list->lv_first;
1513 if (li == NULL)
1514 return -1;
1515 *pp = get_tv_string(&li->li_tv);
1516
1517 li = li->li_next;
1518 if (li == NULL)
1519 return -1;
1520 return get_tv_number(&li->li_tv);
1521}
1522#endif
1523
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001524/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001525 * Top level evaluation function.
1526 * Returns an allocated typval_T with the result.
1527 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001528 */
1529 typval_T *
1530eval_expr(arg, nextcmd)
1531 char_u *arg;
1532 char_u **nextcmd;
1533{
1534 typval_T *tv;
1535
1536 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001537 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001538 {
1539 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001540 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001541 }
1542
1543 return tv;
1544}
1545
1546
Bram Moolenaar4f688582007-07-24 12:34:30 +00001547#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1548 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001550 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001551 * Uses argv[argc] for the function arguments. Only Number and String
1552 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 static int
1556call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 char_u *func;
1558 int argc;
1559 char_u **argv;
1560 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562{
Bram Moolenaar33570922005-01-25 22:26:29 +00001563 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 long n;
1565 int len;
1566 int i;
1567 int doesrange;
1568 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001569 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001571 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001573 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575 for (i = 0; i < argc; i++)
1576 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001577 /* Pass a NULL or empty argument as an empty string */
1578 if (argv[i] == NULL || *argv[i] == NUL)
1579 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001580 argvars[i].v_type = VAR_STRING;
1581 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001582 continue;
1583 }
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 /* Recognize a number argument, the others must be strings. */
1586 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1587 if (len != 0 && len == (int)STRLEN(argv[i]))
1588 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001589 argvars[i].v_type = VAR_NUMBER;
1590 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592 else
1593 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001594 argvars[i].v_type = VAR_STRING;
1595 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 }
1598
1599 if (safe)
1600 {
1601 save_funccalp = save_funccal();
1602 ++sandbox;
1603 }
1604
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001605 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1606 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001608 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (safe)
1610 {
1611 --sandbox;
1612 restore_funccal(save_funccalp);
1613 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 vim_free(argvars);
1615
1616 if (ret == FAIL)
1617 clear_tv(rettv);
1618
1619 return ret;
1620}
1621
Bram Moolenaar4f688582007-07-24 12:34:30 +00001622# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001624 * Call vimL function "func" and return the result as a string.
1625 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 * Uses argv[argc] for the function arguments.
1627 */
1628 void *
1629call_func_retstr(func, argc, argv, safe)
1630 char_u *func;
1631 int argc;
1632 char_u **argv;
1633 int safe; /* use the sandbox */
1634{
1635 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001636 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637
1638 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1639 return NULL;
1640
1641 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 return retval;
1644}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001645# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001646
Bram Moolenaar4f688582007-07-24 12:34:30 +00001647# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001649 * Call vimL function "func" and return the result as a number.
1650 * Returns -1 when calling the function fails.
1651 * Uses argv[argc] for the function arguments.
1652 */
1653 long
1654call_func_retnr(func, argc, argv, safe)
1655 char_u *func;
1656 int argc;
1657 char_u **argv;
1658 int safe; /* use the sandbox */
1659{
1660 typval_T rettv;
1661 long retval;
1662
1663 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1664 return -1;
1665
1666 retval = get_tv_number_chk(&rettv, NULL);
1667 clear_tv(&rettv);
1668 return retval;
1669}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001670# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001671
1672/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001673 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001675 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 */
1677 void *
1678call_func_retlist(func, argc, argv, safe)
1679 char_u *func;
1680 int argc;
1681 char_u **argv;
1682 int safe; /* use the sandbox */
1683{
1684 typval_T rettv;
1685
1686 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1687 return NULL;
1688
1689 if (rettv.v_type != VAR_LIST)
1690 {
1691 clear_tv(&rettv);
1692 return NULL;
1693 }
1694
1695 return rettv.vval.v_list;
1696}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697#endif
1698
Bram Moolenaar4f688582007-07-24 12:34:30 +00001699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700/*
1701 * Save the current function call pointer, and set it to NULL.
1702 * Used when executing autocommands and for ":source".
1703 */
1704 void *
1705save_funccal()
1706{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001707 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 current_funccal = NULL;
1710 return (void *)fc;
1711}
1712
1713 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001714restore_funccal(vfc)
1715 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001717 funccall_T *fc = (funccall_T *)vfc;
1718
1719 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720}
1721
Bram Moolenaar05159a02005-02-26 23:04:13 +00001722#if defined(FEAT_PROFILE) || defined(PROTO)
1723/*
1724 * Prepare profiling for entering a child or something else that is not
1725 * counted for the script/function itself.
1726 * Should always be called in pair with prof_child_exit().
1727 */
1728 void
1729prof_child_enter(tm)
1730 proftime_T *tm; /* place to store waittime */
1731{
1732 funccall_T *fc = current_funccal;
1733
1734 if (fc != NULL && fc->func->uf_profiling)
1735 profile_start(&fc->prof_child);
1736 script_prof_save(tm);
1737}
1738
1739/*
1740 * Take care of time spent in a child.
1741 * Should always be called after prof_child_enter().
1742 */
1743 void
1744prof_child_exit(tm)
1745 proftime_T *tm; /* where waittime was stored */
1746{
1747 funccall_T *fc = current_funccal;
1748
1749 if (fc != NULL && fc->func->uf_profiling)
1750 {
1751 profile_end(&fc->prof_child);
1752 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1753 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1754 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1755 }
1756 script_prof_restore(tm);
1757}
1758#endif
1759
1760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761#ifdef FEAT_FOLDING
1762/*
1763 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1764 * it in "*cp". Doesn't give error messages.
1765 */
1766 int
1767eval_foldexpr(arg, cp)
1768 char_u *arg;
1769 int *cp;
1770{
Bram Moolenaar33570922005-01-25 22:26:29 +00001771 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 int retval;
1773 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001774 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1775 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001778 if (use_sandbox)
1779 ++sandbox;
1780 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001782 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 retval = 0;
1784 else
1785 {
1786 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 if (tv.v_type == VAR_NUMBER)
1788 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001789 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 retval = 0;
1791 else
1792 {
1793 /* If the result is a string, check if there is a non-digit before
1794 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (!VIM_ISDIGIT(*s) && *s != '-')
1797 *cp = *s++;
1798 retval = atol((char *)s);
1799 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 }
1802 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001803 if (use_sandbox)
1804 --sandbox;
1805 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807 return retval;
1808}
1809#endif
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 * ":let" list all variable values
1813 * ":let var1 var2" list variable values
1814 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001815 * ":let var += expr" assignment command.
1816 * ":let var -= expr" assignment command.
1817 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 */
1820 void
1821ex_let(eap)
1822 exarg_T *eap;
1823{
1824 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001826 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001828 int var_count = 0;
1829 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001830 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001831 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001832 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
Bram Moolenaardb552d602006-03-23 22:59:57 +00001834 argend = skip_var_list(arg, &var_count, &semicolon);
1835 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001837 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1838 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001839 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001842 /*
1843 * ":let" without "=": list variables
1844 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 if (*arg == '[')
1846 EMSG(_(e_invarg));
1847 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001849 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001853 list_glob_vars(&first);
1854 list_buf_vars(&first);
1855 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001856#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001857 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001858#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001859 list_script_vars(&first);
1860 list_func_vars(&first);
1861 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 eap->nextcmd = check_nextcmd(arg);
1864 }
1865 else
1866 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001867 op[0] = '=';
1868 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001869 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001870 {
1871 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1872 op[0] = expr[-1]; /* +=, -= or .= */
1873 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 if (eap->skip)
1877 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (eap->skip)
1880 {
1881 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 --emsg_skip;
1884 }
1885 else if (i != FAIL)
1886 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001888 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 }
1891 }
1892}
1893
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894/*
1895 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1896 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 * When "nextchars" is not NULL it points to a string with characters that
1898 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1899 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 * Returns OK or FAIL;
1901 */
1902 static int
1903ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1904 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001905 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 int copy; /* copy values from "tv", don't move */
1907 int semicolon; /* from skip_var_list() */
1908 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910{
1911 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001914 listitem_T *item;
1915 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916
1917 if (*arg != '[')
1918 {
1919 /*
1920 * ":let var = expr" or ":for var in list"
1921 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 return FAIL;
1924 return OK;
1925 }
1926
1927 /*
1928 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1929 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001930 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 {
1932 EMSG(_(e_listreq));
1933 return FAIL;
1934 }
1935
1936 i = list_len(l);
1937 if (semicolon == 0 && var_count < i)
1938 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001939 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940 return FAIL;
1941 }
1942 if (var_count - semicolon > i)
1943 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001944 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 return FAIL;
1946 }
1947
1948 item = l->lv_first;
1949 while (*arg != ']')
1950 {
1951 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001952 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 item = item->li_next;
1954 if (arg == NULL)
1955 return FAIL;
1956
1957 arg = skipwhite(arg);
1958 if (*arg == ';')
1959 {
1960 /* Put the rest of the list (may be empty) in the var after ';'.
1961 * Create a new list for this. */
1962 l = list_alloc();
1963 if (l == NULL)
1964 return FAIL;
1965 while (item != NULL)
1966 {
1967 list_append_tv(l, &item->li_tv);
1968 item = item->li_next;
1969 }
1970
1971 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001972 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 ltv.vval.v_list = l;
1974 l->lv_refcount = 1;
1975
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1977 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 clear_tv(&ltv);
1979 if (arg == NULL)
1980 return FAIL;
1981 break;
1982 }
1983 else if (*arg != ',' && *arg != ']')
1984 {
1985 EMSG2(_(e_intern2), "ex_let_vars()");
1986 return FAIL;
1987 }
1988 }
1989
1990 return OK;
1991}
1992
1993/*
1994 * Skip over assignable variable "var" or list of variables "[var, var]".
1995 * Used for ":let varvar = expr" and ":for varvar in expr".
1996 * For "[var, var]" increment "*var_count" for each variable.
1997 * for "[var, var; var]" set "semicolon".
1998 * Return NULL for an error.
1999 */
2000 static char_u *
2001skip_var_list(arg, var_count, semicolon)
2002 char_u *arg;
2003 int *var_count;
2004 int *semicolon;
2005{
2006 char_u *p, *s;
2007
2008 if (*arg == '[')
2009 {
2010 /* "[var, var]": find the matching ']'. */
2011 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002012 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 {
2014 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2015 s = skip_var_one(p);
2016 if (s == p)
2017 {
2018 EMSG2(_(e_invarg2), p);
2019 return NULL;
2020 }
2021 ++*var_count;
2022
2023 p = skipwhite(s);
2024 if (*p == ']')
2025 break;
2026 else if (*p == ';')
2027 {
2028 if (*semicolon == 1)
2029 {
2030 EMSG(_("Double ; in list of variables"));
2031 return NULL;
2032 }
2033 *semicolon = 1;
2034 }
2035 else if (*p != ',')
2036 {
2037 EMSG2(_(e_invarg2), p);
2038 return NULL;
2039 }
2040 }
2041 return p + 1;
2042 }
2043 else
2044 return skip_var_one(arg);
2045}
2046
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002047/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002048 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002049 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002050 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051 static char_u *
2052skip_var_one(arg)
2053 char_u *arg;
2054{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002055 if (*arg == '@' && arg[1] != NUL)
2056 return arg + 2;
2057 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2058 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059}
2060
Bram Moolenaara7043832005-01-21 11:56:39 +00002061/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002062 * List variables for hashtab "ht" with prefix "prefix".
2063 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002064 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002066list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002067 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002068 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002069 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002070 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002071{
Bram Moolenaar33570922005-01-25 22:26:29 +00002072 hashitem_T *hi;
2073 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 int todo;
2075
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002076 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002077 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2078 {
2079 if (!HASHITEM_EMPTY(hi))
2080 {
2081 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002082 di = HI2DI(hi);
2083 if (empty || di->di_tv.v_type != VAR_STRING
2084 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002085 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 }
2087 }
2088}
2089
2090/*
2091 * List global variables.
2092 */
2093 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094list_glob_vars(first)
2095 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002098}
2099
2100/*
2101 * List buffer variables.
2102 */
2103 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104list_buf_vars(first)
2105 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107 char_u numbuf[NUMBUFLEN];
2108
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2110 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002111
2112 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2114 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002115}
2116
2117/*
2118 * List window variables.
2119 */
2120 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002121list_win_vars(first)
2122 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002123{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002124 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2125 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002126}
2127
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002128#ifdef FEAT_WINDOWS
2129/*
2130 * List tab page variables.
2131 */
2132 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133list_tab_vars(first)
2134 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002135{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2137 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002138}
2139#endif
2140
Bram Moolenaara7043832005-01-21 11:56:39 +00002141/*
2142 * List Vim variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_vim_vars(first)
2146 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149}
2150
2151/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002152 * List script-local variables, if there is a script.
2153 */
2154 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155list_script_vars(first)
2156 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002157{
2158 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2160 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002161}
2162
2163/*
2164 * List function variables, if there is a function.
2165 */
2166 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167list_func_vars(first)
2168 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002169{
2170 if (current_funccal != NULL)
2171 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002173}
2174
2175/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176 * List variables in "arg".
2177 */
2178 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 exarg_T *eap;
2181 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183{
2184 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002185 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002187 char_u *name_start;
2188 char_u *arg_subsc;
2189 char_u *tofree;
2190 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191
2192 while (!ends_excmd(*arg) && !got_int)
2193 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002194 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002196 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2198 {
2199 emsg_severe = TRUE;
2200 EMSG(_(e_trailing));
2201 break;
2202 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 /* get_name_len() takes care of expanding curly braces */
2207 name_start = name = arg;
2208 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2209 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 /* This is mainly to keep test 49 working: when expanding
2212 * curly braces fails overrule the exception error message. */
2213 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 emsg_severe = TRUE;
2216 EMSG2(_(e_invarg2), arg);
2217 break;
2218 }
2219 error = TRUE;
2220 }
2221 else
2222 {
2223 if (tofree != NULL)
2224 name = tofree;
2225 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 else
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 /* handle d.key, l[idx], f(expr) */
2230 arg_subsc = arg;
2231 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002232 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002234 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002236 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002238 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002239 case 'g': list_glob_vars(first); break;
2240 case 'b': list_buf_vars(first); break;
2241 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002242#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002243 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002244#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002245 case 'v': list_vim_vars(first); break;
2246 case 's': list_script_vars(first); break;
2247 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 default:
2249 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002250 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 }
2252 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 {
2254 char_u numbuf[NUMBUFLEN];
2255 char_u *tf;
2256 int c;
2257 char_u *s;
2258
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002259 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 c = *arg;
2261 *arg = NUL;
2262 list_one_var_a((char_u *)"",
2263 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 tv.v_type,
2265 s == NULL ? (char_u *)"" : s,
2266 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 *arg = c;
2268 vim_free(tf);
2269 }
2270 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 }
2273 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274
2275 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277
2278 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
2280
2281 return arg;
2282}
2283
2284/*
2285 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2286 * Returns a pointer to the char just after the var name.
2287 * Returns NULL if there is an error.
2288 */
2289 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002290ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002292 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002294 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296{
2297 int c1;
2298 char_u *name;
2299 char_u *p;
2300 char_u *arg_end = NULL;
2301 int len;
2302 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304
2305 /*
2306 * ":let $VAR = expr": Set environment variable.
2307 */
2308 if (*arg == '$')
2309 {
2310 /* Find the end of the name. */
2311 ++arg;
2312 name = arg;
2313 len = get_env_len(&arg);
2314 if (len == 0)
2315 EMSG2(_(e_invarg2), name - 1);
2316 else
2317 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002318 if (op != NULL && (*op == '+' || *op == '-'))
2319 EMSG2(_(e_letwrong), op);
2320 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002321 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 EMSG(_(e_letunexp));
2323 else
2324 {
2325 c1 = name[len];
2326 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002327 p = get_tv_string_chk(tv);
2328 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 {
2330 int mustfree = FALSE;
2331 char_u *s = vim_getenv(name, &mustfree);
2332
2333 if (s != NULL)
2334 {
2335 p = tofree = concat_str(s, p);
2336 if (mustfree)
2337 vim_free(s);
2338 }
2339 }
2340 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002341 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002343 if (STRICMP(name, "HOME") == 0)
2344 init_homedir();
2345 else if (didset_vim && STRICMP(name, "VIM") == 0)
2346 didset_vim = FALSE;
2347 else if (didset_vimruntime
2348 && STRICMP(name, "VIMRUNTIME") == 0)
2349 didset_vimruntime = FALSE;
2350 arg_end = arg;
2351 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354 }
2355 }
2356 }
2357
2358 /*
2359 * ":let &option = expr": Set option value.
2360 * ":let &l:option = expr": Set local option value.
2361 * ":let &g:option = expr": Set global option value.
2362 */
2363 else if (*arg == '&')
2364 {
2365 /* Find the end of the name. */
2366 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002367 if (p == NULL || (endchars != NULL
2368 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 EMSG(_(e_letunexp));
2370 else
2371 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 long n;
2373 int opt_type;
2374 long numval;
2375 char_u *stringval = NULL;
2376 char_u *s;
2377
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 c1 = *p;
2379 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380
2381 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382 s = get_tv_string_chk(tv); /* != NULL if number or string */
2383 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 {
2385 opt_type = get_option_value(arg, &numval,
2386 &stringval, opt_flags);
2387 if ((opt_type == 1 && *op == '.')
2388 || (opt_type == 0 && *op != '.'))
2389 EMSG2(_(e_letwrong), op);
2390 else
2391 {
2392 if (opt_type == 1) /* number */
2393 {
2394 if (*op == '+')
2395 n = numval + n;
2396 else
2397 n = numval - n;
2398 }
2399 else if (opt_type == 0 && stringval != NULL) /* string */
2400 {
2401 s = concat_str(stringval, s);
2402 vim_free(stringval);
2403 stringval = s;
2404 }
2405 }
2406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002407 if (s != NULL)
2408 {
2409 set_option_value(arg, n, s, opt_flags);
2410 arg_end = p;
2411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 }
2415 }
2416
2417 /*
2418 * ":let @r = expr": Set register contents.
2419 */
2420 else if (*arg == '@')
2421 {
2422 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 if (op != NULL && (*op == '+' || *op == '-'))
2424 EMSG2(_(e_letwrong), op);
2425 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002426 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 EMSG(_(e_letunexp));
2428 else
2429 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002430 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 char_u *s;
2432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002433 p = get_tv_string_chk(tv);
2434 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002436 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 if (s != NULL)
2438 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002439 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 vim_free(s);
2441 }
2442 }
2443 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002446 arg_end = arg + 1;
2447 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002448 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002454 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002456 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002458 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002459
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002460 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002463 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2464 EMSG(_(e_letunexp));
2465 else
2466 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002467 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 arg_end = p;
2469 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 }
2473
2474 else
2475 EMSG2(_(e_invarg2), arg);
2476
2477 return arg_end;
2478}
2479
2480/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002481 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2482 */
2483 static int
2484check_changedtick(arg)
2485 char_u *arg;
2486{
2487 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2488 {
2489 EMSG2(_(e_readonlyvar), arg);
2490 return TRUE;
2491 }
2492 return FALSE;
2493}
2494
2495/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 * Get an lval: variable, Dict item or List item that can be assigned a value
2497 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2498 * "name.key", "name.key[expr]" etc.
2499 * Indexing only works if "name" is an existing List or Dictionary.
2500 * "name" points to the start of the name.
2501 * If "rettv" is not NULL it points to the value to be assigned.
2502 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2503 * wrong; must end in space or cmd separator.
2504 *
2505 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002506 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 */
2509 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002510get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002512 typval_T *rettv;
2513 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 int unlet;
2515 int skip;
2516 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002517 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 char_u *expr_start, *expr_end;
2521 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002522 dictitem_T *v;
2523 typval_T var1;
2524 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002526 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002527 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002528 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533
2534 if (skip)
2535 {
2536 /* When skipping just find the end of the name. */
2537 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002538 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 }
2540
2541 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002542 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 if (expr_start != NULL)
2544 {
2545 /* Don't expand the name when we already know there is an error. */
2546 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2547 && *p != '[' && *p != '.')
2548 {
2549 EMSG(_(e_trailing));
2550 return NULL;
2551 }
2552
2553 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2554 if (lp->ll_exp_name == NULL)
2555 {
2556 /* Report an invalid expression in braces, unless the
2557 * expression evaluation has been cancelled due to an
2558 * aborting error, an interrupt, or an exception. */
2559 if (!aborting() && !quiet)
2560 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002561 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 EMSG2(_(e_invarg2), name);
2563 return NULL;
2564 }
2565 }
2566 lp->ll_name = lp->ll_exp_name;
2567 }
2568 else
2569 lp->ll_name = name;
2570
2571 /* Without [idx] or .key we are done. */
2572 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2573 return p;
2574
2575 cc = *p;
2576 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002577 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 if (v == NULL && !quiet)
2579 EMSG2(_(e_undefvar), lp->ll_name);
2580 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 if (v == NULL)
2582 return NULL;
2583
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 /*
2585 * Loop until no more [idx] or .key is following.
2586 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2591 && !(lp->ll_tv->v_type == VAR_DICT
2592 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 if (!quiet)
2595 EMSG(_("E689: Can only index a List or Dictionary"));
2596 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002597 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (!quiet)
2601 EMSG(_("E708: [:] must come last"));
2602 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002604
Bram Moolenaar8c711452005-01-14 21:53:12 +00002605 len = -1;
2606 if (*p == '.')
2607 {
2608 key = p + 1;
2609 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2610 ;
2611 if (len == 0)
2612 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (!quiet)
2614 EMSG(_(e_emptykey));
2615 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 }
2617 p = key + len;
2618 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002619 else
2620 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002622 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 if (*p == ':')
2624 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 else
2626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 empty1 = FALSE;
2628 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002630 if (get_tv_string_chk(&var1) == NULL)
2631 {
2632 /* not a number or string */
2633 clear_tv(&var1);
2634 return NULL;
2635 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 }
2637
2638 /* Optionally get the second index [ :expr]. */
2639 if (*p == ':')
2640 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002644 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002645 if (!empty1)
2646 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002648 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (rettv != NULL && (rettv->v_type != VAR_LIST
2650 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (!quiet)
2653 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 if (!empty1)
2655 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 }
2658 p = skipwhite(p + 1);
2659 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 else
2662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2665 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 if (!empty1)
2667 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002670 if (get_tv_string_chk(&var2) == NULL)
2671 {
2672 /* not a number or string */
2673 if (!empty1)
2674 clear_tv(&var1);
2675 clear_tv(&var2);
2676 return NULL;
2677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (!quiet)
2687 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 if (!empty1)
2689 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
2694
2695 /* Skip to past ']'. */
2696 ++p;
2697 }
2698
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 {
2701 if (len == -1)
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002704 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 if (*key == NUL)
2706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (!quiet)
2708 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
2712 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002713 lp->ll_list = NULL;
2714 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002715 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002718 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002722 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (len == -1)
2724 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (len == -1)
2732 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 p = NULL;
2735 break;
2736 }
2737 if (len == -1)
2738 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 }
2741 else
2742 {
2743 /*
2744 * Get the number and item for the only or first index of the List.
2745 */
2746 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 else
2749 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002750 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 clear_tv(&var1);
2752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 lp->ll_list = lp->ll_tv->vval.v_list;
2755 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2756 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002758 if (lp->ll_n1 < 0)
2759 {
2760 lp->ll_n1 = 0;
2761 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2762 }
2763 }
2764 if (lp->ll_li == NULL)
2765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 }
2770
2771 /*
2772 * May need to find the item or absolute index for the second
2773 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 * When no index given: "lp->ll_empty2" is TRUE.
2775 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002779 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 }
2788
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2790 if (lp->ll_n1 < 0)
2791 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2792 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002794 }
2795
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002797 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002798 }
2799
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 return p;
2801}
2802
2803/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002804 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 */
2806 static void
2807clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002808 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809{
2810 vim_free(lp->ll_exp_name);
2811 vim_free(lp->ll_newkey);
2812}
2813
2814/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002815 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002817 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 */
2819 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002821 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002823 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002825 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826{
2827 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002828 listitem_T *ri;
2829 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830
2831 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002832 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002834 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 cc = *endp;
2836 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002837 if (op != NULL && *op != '=')
2838 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002839 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002840
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002841 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002842 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002843 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002844 {
2845 if (tv_op(&tv, rettv, op) == OK)
2846 set_var(lp->ll_name, &tv, FALSE);
2847 clear_tv(&tv);
2848 }
2849 }
2850 else
2851 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002853 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002855 else if (tv_check_lock(lp->ll_newkey == NULL
2856 ? lp->ll_tv->v_lock
2857 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2858 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 else if (lp->ll_range)
2860 {
2861 /*
2862 * Assign the List values to the list items.
2863 */
2864 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002865 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002866 if (op != NULL && *op != '=')
2867 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2868 else
2869 {
2870 clear_tv(&lp->ll_li->li_tv);
2871 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2872 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 ri = ri->li_next;
2874 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2875 break;
2876 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002879 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 ri = NULL;
2882 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 lp->ll_li = lp->ll_li->li_next;
2886 ++lp->ll_n1;
2887 }
2888 if (ri != NULL)
2889 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002890 else if (lp->ll_empty2
2891 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 : lp->ll_n1 != lp->ll_n2)
2893 EMSG(_("E711: List value has not enough items"));
2894 }
2895 else
2896 {
2897 /*
2898 * Assign to a List or Dictionary item.
2899 */
2900 if (lp->ll_newkey != NULL)
2901 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002902 if (op != NULL && *op != '=')
2903 {
2904 EMSG2(_(e_letwrong), op);
2905 return;
2906 }
2907
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002909 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 if (di == NULL)
2911 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002912 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2913 {
2914 vim_free(di);
2915 return;
2916 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002918 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002919 else if (op != NULL && *op != '=')
2920 {
2921 tv_op(lp->ll_tv, rettv, op);
2922 return;
2923 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002926
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 /*
2928 * Assign the value to the variable or list item.
2929 */
2930 if (copy)
2931 copy_tv(rettv, lp->ll_tv);
2932 else
2933 {
2934 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002935 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002937 }
2938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002939}
2940
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002941/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2943 * Returns OK or FAIL.
2944 */
2945 static int
2946tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002947 typval_T *tv1;
2948 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 char_u *op;
2950{
2951 long n;
2952 char_u numbuf[NUMBUFLEN];
2953 char_u *s;
2954
2955 /* Can't do anything with a Funcref or a Dict on the right. */
2956 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2957 {
2958 switch (tv1->v_type)
2959 {
2960 case VAR_DICT:
2961 case VAR_FUNC:
2962 break;
2963
2964 case VAR_LIST:
2965 if (*op != '+' || tv2->v_type != VAR_LIST)
2966 break;
2967 /* List += List */
2968 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2969 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2970 return OK;
2971
2972 case VAR_NUMBER:
2973 case VAR_STRING:
2974 if (tv2->v_type == VAR_LIST)
2975 break;
2976 if (*op == '+' || *op == '-')
2977 {
2978 /* nr += nr or nr -= nr*/
2979 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002980#ifdef FEAT_FLOAT
2981 if (tv2->v_type == VAR_FLOAT)
2982 {
2983 float_T f = n;
2984
2985 if (*op == '+')
2986 f += tv2->vval.v_float;
2987 else
2988 f -= tv2->vval.v_float;
2989 clear_tv(tv1);
2990 tv1->v_type = VAR_FLOAT;
2991 tv1->vval.v_float = f;
2992 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002994#endif
2995 {
2996 if (*op == '+')
2997 n += get_tv_number(tv2);
2998 else
2999 n -= get_tv_number(tv2);
3000 clear_tv(tv1);
3001 tv1->v_type = VAR_NUMBER;
3002 tv1->vval.v_number = n;
3003 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003004 }
3005 else
3006 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003007 if (tv2->v_type == VAR_FLOAT)
3008 break;
3009
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003010 /* str .= str */
3011 s = get_tv_string(tv1);
3012 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3013 clear_tv(tv1);
3014 tv1->v_type = VAR_STRING;
3015 tv1->vval.v_string = s;
3016 }
3017 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003018
3019#ifdef FEAT_FLOAT
3020 case VAR_FLOAT:
3021 {
3022 float_T f;
3023
3024 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3025 && tv2->v_type != VAR_NUMBER
3026 && tv2->v_type != VAR_STRING))
3027 break;
3028 if (tv2->v_type == VAR_FLOAT)
3029 f = tv2->vval.v_float;
3030 else
3031 f = get_tv_number(tv2);
3032 if (*op == '+')
3033 tv1->vval.v_float += f;
3034 else
3035 tv1->vval.v_float -= f;
3036 }
3037 return OK;
3038#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003039 }
3040 }
3041
3042 EMSG2(_(e_letwrong), op);
3043 return FAIL;
3044}
3045
3046/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003047 * Add a watcher to a list.
3048 */
3049 static void
3050list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003051 list_T *l;
3052 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003053{
3054 lw->lw_next = l->lv_watch;
3055 l->lv_watch = lw;
3056}
3057
3058/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003059 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003060 * No warning when it isn't found...
3061 */
3062 static void
3063list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003064 list_T *l;
3065 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066{
Bram Moolenaar33570922005-01-25 22:26:29 +00003067 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003068
3069 lwp = &l->lv_watch;
3070 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3071 {
3072 if (lw == lwrem)
3073 {
3074 *lwp = lw->lw_next;
3075 break;
3076 }
3077 lwp = &lw->lw_next;
3078 }
3079}
3080
3081/*
3082 * Just before removing an item from a list: advance watchers to the next
3083 * item.
3084 */
3085 static void
3086list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003087 list_T *l;
3088 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003089{
Bram Moolenaar33570922005-01-25 22:26:29 +00003090 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003091
3092 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3093 if (lw->lw_item == item)
3094 lw->lw_item = item->li_next;
3095}
3096
3097/*
3098 * Evaluate the expression used in a ":for var in expr" command.
3099 * "arg" points to "var".
3100 * Set "*errp" to TRUE for an error, FALSE otherwise;
3101 * Return a pointer that holds the info. Null when there is an error.
3102 */
3103 void *
3104eval_for_line(arg, errp, nextcmdp, skip)
3105 char_u *arg;
3106 int *errp;
3107 char_u **nextcmdp;
3108 int skip;
3109{
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003111 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003112 typval_T tv;
3113 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114
3115 *errp = TRUE; /* default: there is an error */
3116
Bram Moolenaar33570922005-01-25 22:26:29 +00003117 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118 if (fi == NULL)
3119 return NULL;
3120
3121 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3122 if (expr == NULL)
3123 return fi;
3124
3125 expr = skipwhite(expr);
3126 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3127 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003128 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003129 return fi;
3130 }
3131
3132 if (skip)
3133 ++emsg_skip;
3134 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3135 {
3136 *errp = FALSE;
3137 if (!skip)
3138 {
3139 l = tv.vval.v_list;
3140 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003141 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003142 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003143 clear_tv(&tv);
3144 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145 else
3146 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003147 /* No need to increment the refcount, it's already set for the
3148 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003149 fi->fi_list = l;
3150 list_add_watch(l, &fi->fi_lw);
3151 fi->fi_lw.lw_item = l->lv_first;
3152 }
3153 }
3154 }
3155 if (skip)
3156 --emsg_skip;
3157
3158 return fi;
3159}
3160
3161/*
3162 * Use the first item in a ":for" list. Advance to the next.
3163 * Assign the values to the variable (list). "arg" points to the first one.
3164 * Return TRUE when a valid item was found, FALSE when at end of list or
3165 * something wrong.
3166 */
3167 int
3168next_for_item(fi_void, arg)
3169 void *fi_void;
3170 char_u *arg;
3171{
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003174 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175
3176 item = fi->fi_lw.lw_item;
3177 if (item == NULL)
3178 result = FALSE;
3179 else
3180 {
3181 fi->fi_lw.lw_item = item->li_next;
3182 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3183 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3184 }
3185 return result;
3186}
3187
3188/*
3189 * Free the structure used to store info used by ":for".
3190 */
3191 void
3192free_for_info(fi_void)
3193 void *fi_void;
3194{
Bram Moolenaar33570922005-01-25 22:26:29 +00003195 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003196
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003197 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003198 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003200 list_unref(fi->fi_list);
3201 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 vim_free(fi);
3203}
3204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3206
3207 void
3208set_context_for_expression(xp, arg, cmdidx)
3209 expand_T *xp;
3210 char_u *arg;
3211 cmdidx_T cmdidx;
3212{
3213 int got_eq = FALSE;
3214 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 if (cmdidx == CMD_let)
3218 {
3219 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003220 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221 {
3222 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003223 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224 {
3225 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 if (vim_iswhite(*p))
3228 break;
3229 }
3230 return;
3231 }
3232 }
3233 else
3234 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3235 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 while ((xp->xp_pattern = vim_strpbrk(arg,
3237 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3238 {
3239 c = *xp->xp_pattern;
3240 if (c == '&')
3241 {
3242 c = xp->xp_pattern[1];
3243 if (c == '&')
3244 {
3245 ++xp->xp_pattern;
3246 xp->xp_context = cmdidx != CMD_let || got_eq
3247 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3248 }
3249 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003252 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3253 xp->xp_pattern += 2;
3254
3255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 }
3257 else if (c == '$')
3258 {
3259 /* environment variable */
3260 xp->xp_context = EXPAND_ENV_VARS;
3261 }
3262 else if (c == '=')
3263 {
3264 got_eq = TRUE;
3265 xp->xp_context = EXPAND_EXPRESSION;
3266 }
3267 else if (c == '<'
3268 && xp->xp_context == EXPAND_FUNCTIONS
3269 && vim_strchr(xp->xp_pattern, '(') == NULL)
3270 {
3271 /* Function name can start with "<SNR>" */
3272 break;
3273 }
3274 else if (cmdidx != CMD_let || got_eq)
3275 {
3276 if (c == '"') /* string */
3277 {
3278 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3279 if (c == '\\' && xp->xp_pattern[1] != NUL)
3280 ++xp->xp_pattern;
3281 xp->xp_context = EXPAND_NOTHING;
3282 }
3283 else if (c == '\'') /* literal string */
3284 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003285 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3287 /* skip */ ;
3288 xp->xp_context = EXPAND_NOTHING;
3289 }
3290 else if (c == '|')
3291 {
3292 if (xp->xp_pattern[1] == '|')
3293 {
3294 ++xp->xp_pattern;
3295 xp->xp_context = EXPAND_EXPRESSION;
3296 }
3297 else
3298 xp->xp_context = EXPAND_COMMANDS;
3299 }
3300 else
3301 xp->xp_context = EXPAND_EXPRESSION;
3302 }
3303 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 /* Doesn't look like something valid, expand as an expression
3305 * anyway. */
3306 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 arg = xp->xp_pattern;
3308 if (*arg != NUL)
3309 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3310 /* skip */ ;
3311 }
3312 xp->xp_pattern = arg;
3313}
3314
3315#endif /* FEAT_CMDL_COMPL */
3316
3317/*
3318 * ":1,25call func(arg1, arg2)" function call.
3319 */
3320 void
3321ex_call(eap)
3322 exarg_T *eap;
3323{
3324 char_u *arg = eap->arg;
3325 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003327 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003329 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 linenr_T lnum;
3331 int doesrange;
3332 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003333 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003335 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003336 if (fudi.fd_newkey != NULL)
3337 {
3338 /* Still need to give an error message for missing key. */
3339 EMSG2(_(e_dictkey), fudi.fd_newkey);
3340 vim_free(fudi.fd_newkey);
3341 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003342 if (tofree == NULL)
3343 return;
3344
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003345 /* Increase refcount on dictionary, it could get deleted when evaluating
3346 * the arguments. */
3347 if (fudi.fd_dict != NULL)
3348 ++fudi.fd_dict->dv_refcount;
3349
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003350 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003351 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003352 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
Bram Moolenaar532c7802005-01-27 14:44:31 +00003354 /* Skip white space to allow ":call func ()". Not good, but required for
3355 * backward compatibility. */
3356 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003357 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
3359 if (*startarg != '(')
3360 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003361 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 goto end;
3363 }
3364
3365 /*
3366 * When skipping, evaluate the function once, to find the end of the
3367 * arguments.
3368 * When the function takes a range, this is discovered after the first
3369 * call, and the loop is broken.
3370 */
3371 if (eap->skip)
3372 {
3373 ++emsg_skip;
3374 lnum = eap->line2; /* do it once, also with an invalid range */
3375 }
3376 else
3377 lnum = eap->line1;
3378 for ( ; lnum <= eap->line2; ++lnum)
3379 {
3380 if (!eap->skip && eap->addr_count > 0)
3381 {
3382 curwin->w_cursor.lnum = lnum;
3383 curwin->w_cursor.col = 0;
3384 }
3385 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003386 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003387 eap->line1, eap->line2, &doesrange,
3388 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
3390 failed = TRUE;
3391 break;
3392 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003393
3394 /* Handle a function returning a Funcref, Dictionary or List. */
3395 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3396 {
3397 failed = TRUE;
3398 break;
3399 }
3400
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003401 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (doesrange || eap->skip)
3403 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003404
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003406 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003407 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003408 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (aborting())
3410 break;
3411 }
3412 if (eap->skip)
3413 --emsg_skip;
3414
3415 if (!failed)
3416 {
3417 /* Check for trailing illegal characters and a following command. */
3418 if (!ends_excmd(*arg))
3419 {
3420 emsg_severe = TRUE;
3421 EMSG(_(e_trailing));
3422 }
3423 else
3424 eap->nextcmd = check_nextcmd(arg);
3425 }
3426
3427end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003428 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003429 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430}
3431
3432/*
3433 * ":unlet[!] var1 ... " command.
3434 */
3435 void
3436ex_unlet(eap)
3437 exarg_T *eap;
3438{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003439 ex_unletlock(eap, eap->arg, 0);
3440}
3441
3442/*
3443 * ":lockvar" and ":unlockvar" commands
3444 */
3445 void
3446ex_lockvar(eap)
3447 exarg_T *eap;
3448{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003450 int deep = 2;
3451
3452 if (eap->forceit)
3453 deep = -1;
3454 else if (vim_isdigit(*arg))
3455 {
3456 deep = getdigits(&arg);
3457 arg = skipwhite(arg);
3458 }
3459
3460 ex_unletlock(eap, arg, deep);
3461}
3462
3463/*
3464 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3465 */
3466 static void
3467ex_unletlock(eap, argstart, deep)
3468 exarg_T *eap;
3469 char_u *argstart;
3470 int deep;
3471{
3472 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003475 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
3477 do
3478 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003479 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003480 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3481 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003482 if (lv.ll_name == NULL)
3483 error = TRUE; /* error but continue parsing */
3484 if (name_end == NULL || (!vim_iswhite(*name_end)
3485 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003487 if (name_end != NULL)
3488 {
3489 emsg_severe = TRUE;
3490 EMSG(_(e_trailing));
3491 }
3492 if (!(eap->skip || error))
3493 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 break;
3495 }
3496
3497 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003498 {
3499 if (eap->cmdidx == CMD_unlet)
3500 {
3501 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3502 error = TRUE;
3503 }
3504 else
3505 {
3506 if (do_lock_var(&lv, name_end, deep,
3507 eap->cmdidx == CMD_lockvar) == FAIL)
3508 error = TRUE;
3509 }
3510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003512 if (!eap->skip)
3513 clear_lval(&lv);
3514
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 arg = skipwhite(name_end);
3516 } while (!ends_excmd(*arg));
3517
3518 eap->nextcmd = check_nextcmd(arg);
3519}
3520
Bram Moolenaar8c711452005-01-14 21:53:12 +00003521 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003522do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003523 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003524 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003525 int forceit;
3526{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003527 int ret = OK;
3528 int cc;
3529
3530 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003531 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003532 cc = *name_end;
3533 *name_end = NUL;
3534
3535 /* Normal name or expanded name. */
3536 if (check_changedtick(lp->ll_name))
3537 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003538 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003539 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003540 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003541 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003542 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3543 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003544 else if (lp->ll_range)
3545 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003546 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003547
3548 /* Delete a range of List items. */
3549 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3550 {
3551 li = lp->ll_li->li_next;
3552 listitem_remove(lp->ll_list, lp->ll_li);
3553 lp->ll_li = li;
3554 ++lp->ll_n1;
3555 }
3556 }
3557 else
3558 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003559 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003560 /* unlet a List item. */
3561 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003562 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003563 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003564 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 }
3566
3567 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003568}
3569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570/*
3571 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003572 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 */
3574 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003575do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
Bram Moolenaar33570922005-01-25 22:26:29 +00003579 hashtab_T *ht;
3580 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003581 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003582 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
Bram Moolenaar33570922005-01-25 22:26:29 +00003584 ht = find_var_ht(name, &varname);
3585 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003587 hi = hash_find(ht, varname);
3588 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003589 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003590 di = HI2DI(hi);
3591 if (var_check_fixed(di->di_flags, name)
3592 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593 return FAIL;
3594 delete_var(ht, hi);
3595 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598 if (forceit)
3599 return OK;
3600 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 return FAIL;
3602}
3603
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003604/*
3605 * Lock or unlock variable indicated by "lp".
3606 * "deep" is the levels to go (-1 for unlimited);
3607 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3608 */
3609 static int
3610do_lock_var(lp, name_end, deep, lock)
3611 lval_T *lp;
3612 char_u *name_end;
3613 int deep;
3614 int lock;
3615{
3616 int ret = OK;
3617 int cc;
3618 dictitem_T *di;
3619
3620 if (deep == 0) /* nothing to do */
3621 return OK;
3622
3623 if (lp->ll_tv == NULL)
3624 {
3625 cc = *name_end;
3626 *name_end = NUL;
3627
3628 /* Normal name or expanded name. */
3629 if (check_changedtick(lp->ll_name))
3630 ret = FAIL;
3631 else
3632 {
3633 di = find_var(lp->ll_name, NULL);
3634 if (di == NULL)
3635 ret = FAIL;
3636 else
3637 {
3638 if (lock)
3639 di->di_flags |= DI_FLAGS_LOCK;
3640 else
3641 di->di_flags &= ~DI_FLAGS_LOCK;
3642 item_lock(&di->di_tv, deep, lock);
3643 }
3644 }
3645 *name_end = cc;
3646 }
3647 else if (lp->ll_range)
3648 {
3649 listitem_T *li = lp->ll_li;
3650
3651 /* (un)lock a range of List items. */
3652 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3653 {
3654 item_lock(&li->li_tv, deep, lock);
3655 li = li->li_next;
3656 ++lp->ll_n1;
3657 }
3658 }
3659 else if (lp->ll_list != NULL)
3660 /* (un)lock a List item. */
3661 item_lock(&lp->ll_li->li_tv, deep, lock);
3662 else
3663 /* un(lock) a Dictionary item. */
3664 item_lock(&lp->ll_di->di_tv, deep, lock);
3665
3666 return ret;
3667}
3668
3669/*
3670 * Lock or unlock an item. "deep" is nr of levels to go.
3671 */
3672 static void
3673item_lock(tv, deep, lock)
3674 typval_T *tv;
3675 int deep;
3676 int lock;
3677{
3678 static int recurse = 0;
3679 list_T *l;
3680 listitem_T *li;
3681 dict_T *d;
3682 hashitem_T *hi;
3683 int todo;
3684
3685 if (recurse >= DICT_MAXNEST)
3686 {
3687 EMSG(_("E743: variable nested too deep for (un)lock"));
3688 return;
3689 }
3690 if (deep == 0)
3691 return;
3692 ++recurse;
3693
3694 /* lock/unlock the item itself */
3695 if (lock)
3696 tv->v_lock |= VAR_LOCKED;
3697 else
3698 tv->v_lock &= ~VAR_LOCKED;
3699
3700 switch (tv->v_type)
3701 {
3702 case VAR_LIST:
3703 if ((l = tv->vval.v_list) != NULL)
3704 {
3705 if (lock)
3706 l->lv_lock |= VAR_LOCKED;
3707 else
3708 l->lv_lock &= ~VAR_LOCKED;
3709 if (deep < 0 || deep > 1)
3710 /* recursive: lock/unlock the items the List contains */
3711 for (li = l->lv_first; li != NULL; li = li->li_next)
3712 item_lock(&li->li_tv, deep - 1, lock);
3713 }
3714 break;
3715 case VAR_DICT:
3716 if ((d = tv->vval.v_dict) != NULL)
3717 {
3718 if (lock)
3719 d->dv_lock |= VAR_LOCKED;
3720 else
3721 d->dv_lock &= ~VAR_LOCKED;
3722 if (deep < 0 || deep > 1)
3723 {
3724 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003725 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3727 {
3728 if (!HASHITEM_EMPTY(hi))
3729 {
3730 --todo;
3731 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3732 }
3733 }
3734 }
3735 }
3736 }
3737 --recurse;
3738}
3739
Bram Moolenaara40058a2005-07-11 22:42:07 +00003740/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003741 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3742 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003743 */
3744 static int
3745tv_islocked(tv)
3746 typval_T *tv;
3747{
3748 return (tv->v_lock & VAR_LOCKED)
3749 || (tv->v_type == VAR_LIST
3750 && tv->vval.v_list != NULL
3751 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3752 || (tv->v_type == VAR_DICT
3753 && tv->vval.v_dict != NULL
3754 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3755}
3756
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3758/*
3759 * Delete all "menutrans_" variables.
3760 */
3761 void
3762del_menutrans_vars()
3763{
Bram Moolenaar33570922005-01-25 22:26:29 +00003764 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003765 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
Bram Moolenaar33570922005-01-25 22:26:29 +00003767 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003768 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003769 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003770 {
3771 if (!HASHITEM_EMPTY(hi))
3772 {
3773 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003774 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3775 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003776 }
3777 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003778 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779}
3780#endif
3781
3782#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3783
3784/*
3785 * Local string buffer for the next two functions to store a variable name
3786 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3787 * get_user_var_name().
3788 */
3789
3790static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3791
3792static char_u *varnamebuf = NULL;
3793static int varnamebuflen = 0;
3794
3795/*
3796 * Function to concatenate a prefix and a variable name.
3797 */
3798 static char_u *
3799cat_prefix_varname(prefix, name)
3800 int prefix;
3801 char_u *name;
3802{
3803 int len;
3804
3805 len = (int)STRLEN(name) + 3;
3806 if (len > varnamebuflen)
3807 {
3808 vim_free(varnamebuf);
3809 len += 10; /* some additional space */
3810 varnamebuf = alloc(len);
3811 if (varnamebuf == NULL)
3812 {
3813 varnamebuflen = 0;
3814 return NULL;
3815 }
3816 varnamebuflen = len;
3817 }
3818 *varnamebuf = prefix;
3819 varnamebuf[1] = ':';
3820 STRCPY(varnamebuf + 2, name);
3821 return varnamebuf;
3822}
3823
3824/*
3825 * Function given to ExpandGeneric() to obtain the list of user defined
3826 * (global/buffer/window/built-in) variable names.
3827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 char_u *
3829get_user_var_name(xp, idx)
3830 expand_T *xp;
3831 int idx;
3832{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003833 static long_u gdone;
3834 static long_u bdone;
3835 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003836#ifdef FEAT_WINDOWS
3837 static long_u tdone;
3838#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003839 static int vidx;
3840 static hashitem_T *hi;
3841 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
3843 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003844 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003845 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003846#ifdef FEAT_WINDOWS
3847 tdone = 0;
3848#endif
3849 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003850
3851 /* Global variables */
3852 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003854 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003856 else
3857 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003858 while (HASHITEM_EMPTY(hi))
3859 ++hi;
3860 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3861 return cat_prefix_varname('g', hi->hi_key);
3862 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003864
3865 /* b: variables */
3866 ht = &curbuf->b_vars.dv_hashtab;
3867 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003869 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003870 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003871 else
3872 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003873 while (HASHITEM_EMPTY(hi))
3874 ++hi;
3875 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003877 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003879 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 return (char_u *)"b:changedtick";
3881 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003882
3883 /* w: variables */
3884 ht = &curwin->w_vars.dv_hashtab;
3885 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003887 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003888 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003889 else
3890 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003891 while (HASHITEM_EMPTY(hi))
3892 ++hi;
3893 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003895
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003896#ifdef FEAT_WINDOWS
3897 /* t: variables */
3898 ht = &curtab->tp_vars.dv_hashtab;
3899 if (tdone < ht->ht_used)
3900 {
3901 if (tdone++ == 0)
3902 hi = ht->ht_array;
3903 else
3904 ++hi;
3905 while (HASHITEM_EMPTY(hi))
3906 ++hi;
3907 return cat_prefix_varname('t', hi->hi_key);
3908 }
3909#endif
3910
Bram Moolenaar33570922005-01-25 22:26:29 +00003911 /* v: variables */
3912 if (vidx < VV_LEN)
3913 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
3915 vim_free(varnamebuf);
3916 varnamebuf = NULL;
3917 varnamebuflen = 0;
3918 return NULL;
3919}
3920
3921#endif /* FEAT_CMDL_COMPL */
3922
3923/*
3924 * types for expressions.
3925 */
3926typedef enum
3927{
3928 TYPE_UNKNOWN = 0
3929 , TYPE_EQUAL /* == */
3930 , TYPE_NEQUAL /* != */
3931 , TYPE_GREATER /* > */
3932 , TYPE_GEQUAL /* >= */
3933 , TYPE_SMALLER /* < */
3934 , TYPE_SEQUAL /* <= */
3935 , TYPE_MATCH /* =~ */
3936 , TYPE_NOMATCH /* !~ */
3937} exptype_T;
3938
3939/*
3940 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003941 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3943 */
3944
3945/*
3946 * Handle zero level expression.
3947 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003948 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003949 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 * Return OK or FAIL.
3951 */
3952 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 char_u **nextcmd;
3957 int evaluate;
3958{
3959 int ret;
3960 char_u *p;
3961
3962 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003963 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 if (ret == FAIL || !ends_excmd(*p))
3965 {
3966 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003967 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 /*
3969 * Report the invalid expression unless the expression evaluation has
3970 * been cancelled due to an aborting error, an interrupt, or an
3971 * exception.
3972 */
3973 if (!aborting())
3974 EMSG2(_(e_invexpr2), arg);
3975 ret = FAIL;
3976 }
3977 if (nextcmd != NULL)
3978 *nextcmd = check_nextcmd(p);
3979
3980 return ret;
3981}
3982
3983/*
3984 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003985 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 *
3987 * "arg" must point to the first non-white of the expression.
3988 * "arg" is advanced to the next non-white after the recognized expression.
3989 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003990 * Note: "rettv.v_lock" is not set.
3991 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 * Return OK or FAIL.
3993 */
3994 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003995eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 int evaluate;
3999{
4000 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004001 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
4003 /*
4004 * Get the first variable.
4005 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004006 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 return FAIL;
4008
4009 if ((*arg)[0] == '?')
4010 {
4011 result = FALSE;
4012 if (evaluate)
4013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004014 int error = FALSE;
4015
4016 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004019 if (error)
4020 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022
4023 /*
4024 * Get the second variable.
4025 */
4026 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 return FAIL;
4029
4030 /*
4031 * Check for the ":".
4032 */
4033 if ((*arg)[0] != ':')
4034 {
4035 EMSG(_("E109: Missing ':' after '?'"));
4036 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 return FAIL;
4039 }
4040
4041 /*
4042 * Get the third variable.
4043 */
4044 *arg = skipwhite(*arg + 1);
4045 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4046 {
4047 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 return FAIL;
4050 }
4051 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004052 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
4054
4055 return OK;
4056}
4057
4058/*
4059 * Handle first level expression:
4060 * expr2 || expr2 || expr2 logical OR
4061 *
4062 * "arg" must point to the first non-white of the expression.
4063 * "arg" is advanced to the next non-white after the recognized expression.
4064 *
4065 * Return OK or FAIL.
4066 */
4067 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004068eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 int evaluate;
4072{
Bram Moolenaar33570922005-01-25 22:26:29 +00004073 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 long result;
4075 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004076 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078 /*
4079 * Get the first variable.
4080 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 return FAIL;
4083
4084 /*
4085 * Repeat until there is no following "||".
4086 */
4087 first = TRUE;
4088 result = FALSE;
4089 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4090 {
4091 if (evaluate && first)
4092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004095 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004096 if (error)
4097 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 first = FALSE;
4099 }
4100
4101 /*
4102 * Get the second variable.
4103 */
4104 *arg = skipwhite(*arg + 2);
4105 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4106 return FAIL;
4107
4108 /*
4109 * Compute the result.
4110 */
4111 if (evaluate && !result)
4112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004113 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004116 if (error)
4117 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
4119 if (evaluate)
4120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 rettv->v_type = VAR_NUMBER;
4122 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124 }
4125
4126 return OK;
4127}
4128
4129/*
4130 * Handle second level expression:
4131 * expr3 && expr3 && expr3 logical AND
4132 *
4133 * "arg" must point to the first non-white of the expression.
4134 * "arg" is advanced to the next non-white after the recognized expression.
4135 *
4136 * Return OK or FAIL.
4137 */
4138 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 int evaluate;
4143{
Bram Moolenaar33570922005-01-25 22:26:29 +00004144 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 long result;
4146 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004147 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 /*
4150 * Get the first variable.
4151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 return FAIL;
4154
4155 /*
4156 * Repeat until there is no following "&&".
4157 */
4158 first = TRUE;
4159 result = TRUE;
4160 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4161 {
4162 if (evaluate && first)
4163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004164 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 if (error)
4168 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 first = FALSE;
4170 }
4171
4172 /*
4173 * Get the second variable.
4174 */
4175 *arg = skipwhite(*arg + 2);
4176 if (eval4(arg, &var2, evaluate && result) == FAIL)
4177 return FAIL;
4178
4179 /*
4180 * Compute the result.
4181 */
4182 if (evaluate && result)
4183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (error)
4188 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 if (evaluate)
4191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 rettv->v_type = VAR_NUMBER;
4193 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 }
4196
4197 return OK;
4198}
4199
4200/*
4201 * Handle third level expression:
4202 * var1 == var2
4203 * var1 =~ var2
4204 * var1 != var2
4205 * var1 !~ var2
4206 * var1 > var2
4207 * var1 >= var2
4208 * var1 < var2
4209 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004210 * var1 is var2
4211 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 *
4213 * "arg" must point to the first non-white of the expression.
4214 * "arg" is advanced to the next non-white after the recognized expression.
4215 *
4216 * Return OK or FAIL.
4217 */
4218 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 int evaluate;
4223{
Bram Moolenaar33570922005-01-25 22:26:29 +00004224 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 char_u *p;
4226 int i;
4227 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004228 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 int len = 2;
4230 long n1, n2;
4231 char_u *s1, *s2;
4232 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4233 regmatch_T regmatch;
4234 int ic;
4235 char_u *save_cpo;
4236
4237 /*
4238 * Get the first variable.
4239 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 return FAIL;
4242
4243 p = *arg;
4244 switch (p[0])
4245 {
4246 case '=': if (p[1] == '=')
4247 type = TYPE_EQUAL;
4248 else if (p[1] == '~')
4249 type = TYPE_MATCH;
4250 break;
4251 case '!': if (p[1] == '=')
4252 type = TYPE_NEQUAL;
4253 else if (p[1] == '~')
4254 type = TYPE_NOMATCH;
4255 break;
4256 case '>': if (p[1] != '=')
4257 {
4258 type = TYPE_GREATER;
4259 len = 1;
4260 }
4261 else
4262 type = TYPE_GEQUAL;
4263 break;
4264 case '<': if (p[1] != '=')
4265 {
4266 type = TYPE_SMALLER;
4267 len = 1;
4268 }
4269 else
4270 type = TYPE_SEQUAL;
4271 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004272 case 'i': if (p[1] == 's')
4273 {
4274 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4275 len = 5;
4276 if (!vim_isIDc(p[len]))
4277 {
4278 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4279 type_is = TRUE;
4280 }
4281 }
4282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284
4285 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004286 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 */
4288 if (type != TYPE_UNKNOWN)
4289 {
4290 /* extra question mark appended: ignore case */
4291 if (p[len] == '?')
4292 {
4293 ic = TRUE;
4294 ++len;
4295 }
4296 /* extra '#' appended: match case */
4297 else if (p[len] == '#')
4298 {
4299 ic = FALSE;
4300 ++len;
4301 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004302 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 else
4304 ic = p_ic;
4305
4306 /*
4307 * Get the second variable.
4308 */
4309 *arg = skipwhite(p + len);
4310 if (eval5(arg, &var2, evaluate) == FAIL)
4311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return FAIL;
4314 }
4315
4316 if (evaluate)
4317 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004318 if (type_is && rettv->v_type != var2.v_type)
4319 {
4320 /* For "is" a different type always means FALSE, for "notis"
4321 * it means TRUE. */
4322 n1 = (type == TYPE_NEQUAL);
4323 }
4324 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4325 {
4326 if (type_is)
4327 {
4328 n1 = (rettv->v_type == var2.v_type
4329 && rettv->vval.v_list == var2.vval.v_list);
4330 if (type == TYPE_NEQUAL)
4331 n1 = !n1;
4332 }
4333 else if (rettv->v_type != var2.v_type
4334 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4335 {
4336 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004337 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004338 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004339 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004340 clear_tv(rettv);
4341 clear_tv(&var2);
4342 return FAIL;
4343 }
4344 else
4345 {
4346 /* Compare two Lists for being equal or unequal. */
4347 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4348 if (type == TYPE_NEQUAL)
4349 n1 = !n1;
4350 }
4351 }
4352
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004353 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4354 {
4355 if (type_is)
4356 {
4357 n1 = (rettv->v_type == var2.v_type
4358 && rettv->vval.v_dict == var2.vval.v_dict);
4359 if (type == TYPE_NEQUAL)
4360 n1 = !n1;
4361 }
4362 else if (rettv->v_type != var2.v_type
4363 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4364 {
4365 if (rettv->v_type != var2.v_type)
4366 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4367 else
4368 EMSG(_("E736: Invalid operation for Dictionary"));
4369 clear_tv(rettv);
4370 clear_tv(&var2);
4371 return FAIL;
4372 }
4373 else
4374 {
4375 /* Compare two Dictionaries for being equal or unequal. */
4376 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4377 if (type == TYPE_NEQUAL)
4378 n1 = !n1;
4379 }
4380 }
4381
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004382 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4383 {
4384 if (rettv->v_type != var2.v_type
4385 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4386 {
4387 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004388 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004389 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004390 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004391 clear_tv(rettv);
4392 clear_tv(&var2);
4393 return FAIL;
4394 }
4395 else
4396 {
4397 /* Compare two Funcrefs for being equal or unequal. */
4398 if (rettv->vval.v_string == NULL
4399 || var2.vval.v_string == NULL)
4400 n1 = FALSE;
4401 else
4402 n1 = STRCMP(rettv->vval.v_string,
4403 var2.vval.v_string) == 0;
4404 if (type == TYPE_NEQUAL)
4405 n1 = !n1;
4406 }
4407 }
4408
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004409#ifdef FEAT_FLOAT
4410 /*
4411 * If one of the two variables is a float, compare as a float.
4412 * When using "=~" or "!~", always compare as string.
4413 */
4414 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4415 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4416 {
4417 float_T f1, f2;
4418
4419 if (rettv->v_type == VAR_FLOAT)
4420 f1 = rettv->vval.v_float;
4421 else
4422 f1 = get_tv_number(rettv);
4423 if (var2.v_type == VAR_FLOAT)
4424 f2 = var2.vval.v_float;
4425 else
4426 f2 = get_tv_number(&var2);
4427 n1 = FALSE;
4428 switch (type)
4429 {
4430 case TYPE_EQUAL: n1 = (f1 == f2); break;
4431 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4432 case TYPE_GREATER: n1 = (f1 > f2); break;
4433 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4434 case TYPE_SMALLER: n1 = (f1 < f2); break;
4435 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4436 case TYPE_UNKNOWN:
4437 case TYPE_MATCH:
4438 case TYPE_NOMATCH: break; /* avoid gcc warning */
4439 }
4440 }
4441#endif
4442
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 /*
4444 * If one of the two variables is a number, compare as a number.
4445 * When using "=~" or "!~", always compare as string.
4446 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004447 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450 n1 = get_tv_number(rettv);
4451 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 switch (type)
4453 {
4454 case TYPE_EQUAL: n1 = (n1 == n2); break;
4455 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4456 case TYPE_GREATER: n1 = (n1 > n2); break;
4457 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4458 case TYPE_SMALLER: n1 = (n1 < n2); break;
4459 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4460 case TYPE_UNKNOWN:
4461 case TYPE_MATCH:
4462 case TYPE_NOMATCH: break; /* avoid gcc warning */
4463 }
4464 }
4465 else
4466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004467 s1 = get_tv_string_buf(rettv, buf1);
4468 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4470 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4471 else
4472 i = 0;
4473 n1 = FALSE;
4474 switch (type)
4475 {
4476 case TYPE_EQUAL: n1 = (i == 0); break;
4477 case TYPE_NEQUAL: n1 = (i != 0); break;
4478 case TYPE_GREATER: n1 = (i > 0); break;
4479 case TYPE_GEQUAL: n1 = (i >= 0); break;
4480 case TYPE_SMALLER: n1 = (i < 0); break;
4481 case TYPE_SEQUAL: n1 = (i <= 0); break;
4482
4483 case TYPE_MATCH:
4484 case TYPE_NOMATCH:
4485 /* avoid 'l' flag in 'cpoptions' */
4486 save_cpo = p_cpo;
4487 p_cpo = (char_u *)"";
4488 regmatch.regprog = vim_regcomp(s2,
4489 RE_MAGIC + RE_STRING);
4490 regmatch.rm_ic = ic;
4491 if (regmatch.regprog != NULL)
4492 {
4493 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4494 vim_free(regmatch.regprog);
4495 if (type == TYPE_NOMATCH)
4496 n1 = !n1;
4497 }
4498 p_cpo = save_cpo;
4499 break;
4500
4501 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4502 }
4503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004504 clear_tv(rettv);
4505 clear_tv(&var2);
4506 rettv->v_type = VAR_NUMBER;
4507 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509 }
4510
4511 return OK;
4512}
4513
4514/*
4515 * Handle fourth level expression:
4516 * + number addition
4517 * - number subtraction
4518 * . string concatenation
4519 *
4520 * "arg" must point to the first non-white of the expression.
4521 * "arg" is advanced to the next non-white after the recognized expression.
4522 *
4523 * Return OK or FAIL.
4524 */
4525 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004526eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 int evaluate;
4530{
Bram Moolenaar33570922005-01-25 22:26:29 +00004531 typval_T var2;
4532 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 int op;
4534 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004535#ifdef FEAT_FLOAT
4536 float_T f1 = 0, f2 = 0;
4537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 char_u *s1, *s2;
4539 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4540 char_u *p;
4541
4542 /*
4543 * Get the first variable.
4544 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004545 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 return FAIL;
4547
4548 /*
4549 * Repeat computing, until no '+', '-' or '.' is following.
4550 */
4551 for (;;)
4552 {
4553 op = **arg;
4554 if (op != '+' && op != '-' && op != '.')
4555 break;
4556
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004557 if ((op != '+' || rettv->v_type != VAR_LIST)
4558#ifdef FEAT_FLOAT
4559 && (op == '.' || rettv->v_type != VAR_FLOAT)
4560#endif
4561 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004562 {
4563 /* For "list + ...", an illegal use of the first operand as
4564 * a number cannot be determined before evaluating the 2nd
4565 * operand: if this is also a list, all is ok.
4566 * For "something . ...", "something - ..." or "non-list + ...",
4567 * we know that the first operand needs to be a string or number
4568 * without evaluating the 2nd operand. So check before to avoid
4569 * side effects after an error. */
4570 if (evaluate && get_tv_string_chk(rettv) == NULL)
4571 {
4572 clear_tv(rettv);
4573 return FAIL;
4574 }
4575 }
4576
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 /*
4578 * Get the second variable.
4579 */
4580 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004581 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004583 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 return FAIL;
4585 }
4586
4587 if (evaluate)
4588 {
4589 /*
4590 * Compute the result.
4591 */
4592 if (op == '.')
4593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004594 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4595 s2 = get_tv_string_buf_chk(&var2, buf2);
4596 if (s2 == NULL) /* type error ? */
4597 {
4598 clear_tv(rettv);
4599 clear_tv(&var2);
4600 return FAIL;
4601 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004602 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603 clear_tv(rettv);
4604 rettv->v_type = VAR_STRING;
4605 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004607 else if (op == '+' && rettv->v_type == VAR_LIST
4608 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004609 {
4610 /* concatenate Lists */
4611 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4612 &var3) == FAIL)
4613 {
4614 clear_tv(rettv);
4615 clear_tv(&var2);
4616 return FAIL;
4617 }
4618 clear_tv(rettv);
4619 *rettv = var3;
4620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 else
4622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004623 int error = FALSE;
4624
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004625#ifdef FEAT_FLOAT
4626 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004627 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004628 f1 = rettv->vval.v_float;
4629 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004630 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004631 else
4632#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004633 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634 n1 = get_tv_number_chk(rettv, &error);
4635 if (error)
4636 {
4637 /* This can only happen for "list + non-list". For
4638 * "non-list + ..." or "something - ...", we returned
4639 * before evaluating the 2nd operand. */
4640 clear_tv(rettv);
4641 return FAIL;
4642 }
4643#ifdef FEAT_FLOAT
4644 if (var2.v_type == VAR_FLOAT)
4645 f1 = n1;
4646#endif
4647 }
4648#ifdef FEAT_FLOAT
4649 if (var2.v_type == VAR_FLOAT)
4650 {
4651 f2 = var2.vval.v_float;
4652 n2 = 0;
4653 }
4654 else
4655#endif
4656 {
4657 n2 = get_tv_number_chk(&var2, &error);
4658 if (error)
4659 {
4660 clear_tv(rettv);
4661 clear_tv(&var2);
4662 return FAIL;
4663 }
4664#ifdef FEAT_FLOAT
4665 if (rettv->v_type == VAR_FLOAT)
4666 f2 = n2;
4667#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004670
4671#ifdef FEAT_FLOAT
4672 /* If there is a float on either side the result is a float. */
4673 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4674 {
4675 if (op == '+')
4676 f1 = f1 + f2;
4677 else
4678 f1 = f1 - f2;
4679 rettv->v_type = VAR_FLOAT;
4680 rettv->vval.v_float = f1;
4681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004683#endif
4684 {
4685 if (op == '+')
4686 n1 = n1 + n2;
4687 else
4688 n1 = n1 - n2;
4689 rettv->v_type = VAR_NUMBER;
4690 rettv->vval.v_number = n1;
4691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695 }
4696 return OK;
4697}
4698
4699/*
4700 * Handle fifth level expression:
4701 * * number multiplication
4702 * / number division
4703 * % number modulo
4704 *
4705 * "arg" must point to the first non-white of the expression.
4706 * "arg" is advanced to the next non-white after the recognized expression.
4707 *
4708 * Return OK or FAIL.
4709 */
4710 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004711eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004713 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004715 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
Bram Moolenaar33570922005-01-25 22:26:29 +00004717 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 int op;
4719 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004720#ifdef FEAT_FLOAT
4721 int use_float = FALSE;
4722 float_T f1 = 0, f2;
4723#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
4726 /*
4727 * Get the first variable.
4728 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004729 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 return FAIL;
4731
4732 /*
4733 * Repeat computing, until no '*', '/' or '%' is following.
4734 */
4735 for (;;)
4736 {
4737 op = **arg;
4738 if (op != '*' && op != '/' && op != '%')
4739 break;
4740
4741 if (evaluate)
4742 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004743#ifdef FEAT_FLOAT
4744 if (rettv->v_type == VAR_FLOAT)
4745 {
4746 f1 = rettv->vval.v_float;
4747 use_float = TRUE;
4748 n1 = 0;
4749 }
4750 else
4751#endif
4752 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004753 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004754 if (error)
4755 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 else
4758 n1 = 0;
4759
4760 /*
4761 * Get the second variable.
4762 */
4763 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004764 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 return FAIL;
4766
4767 if (evaluate)
4768 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004769#ifdef FEAT_FLOAT
4770 if (var2.v_type == VAR_FLOAT)
4771 {
4772 if (!use_float)
4773 {
4774 f1 = n1;
4775 use_float = TRUE;
4776 }
4777 f2 = var2.vval.v_float;
4778 n2 = 0;
4779 }
4780 else
4781#endif
4782 {
4783 n2 = get_tv_number_chk(&var2, &error);
4784 clear_tv(&var2);
4785 if (error)
4786 return FAIL;
4787#ifdef FEAT_FLOAT
4788 if (use_float)
4789 f2 = n2;
4790#endif
4791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792
4793 /*
4794 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004797#ifdef FEAT_FLOAT
4798 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004800 if (op == '*')
4801 f1 = f1 * f2;
4802 else if (op == '/')
4803 {
4804 /* We rely on the floating point library to handle divide
4805 * by zero to result in "inf" and not a crash. */
4806 f1 = f1 / f2;
4807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004809 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004810 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 return FAIL;
4812 }
4813 rettv->v_type = VAR_FLOAT;
4814 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819 if (op == '*')
4820 n1 = n1 * n2;
4821 else if (op == '/')
4822 {
4823 if (n2 == 0) /* give an error message? */
4824 {
4825 if (n1 == 0)
4826 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4827 else if (n1 < 0)
4828 n1 = -0x7fffffffL;
4829 else
4830 n1 = 0x7fffffffL;
4831 }
4832 else
4833 n1 = n1 / n2;
4834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836 {
4837 if (n2 == 0) /* give an error message? */
4838 n1 = 0;
4839 else
4840 n1 = n1 % n2;
4841 }
4842 rettv->v_type = VAR_NUMBER;
4843 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
4846 }
4847
4848 return OK;
4849}
4850
4851/*
4852 * Handle sixth level expression:
4853 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004854 * "string" string constant
4855 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 * &option-name option value
4857 * @r register contents
4858 * identifier variable value
4859 * function() function call
4860 * $VAR environment variable
4861 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004862 * [expr, expr] List
4863 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 *
4865 * Also handle:
4866 * ! in front logical NOT
4867 * - in front unary minus
4868 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004869 * trailing [] subscript in String or List
4870 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 *
4872 * "arg" must point to the first non-white of the expression.
4873 * "arg" is advanced to the next non-white after the recognized expression.
4874 *
4875 * Return OK or FAIL.
4876 */
4877 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004878eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004882 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 long n;
4885 int len;
4886 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 char_u *start_leader, *end_leader;
4888 int ret = OK;
4889 char_u *alias;
4890
4891 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004893 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
4897 /*
4898 * Skip '!' and '-' characters. They are handled later.
4899 */
4900 start_leader = *arg;
4901 while (**arg == '!' || **arg == '-' || **arg == '+')
4902 *arg = skipwhite(*arg + 1);
4903 end_leader = *arg;
4904
4905 switch (**arg)
4906 {
4907 /*
4908 * Number constant.
4909 */
4910 case '0':
4911 case '1':
4912 case '2':
4913 case '3':
4914 case '4':
4915 case '5':
4916 case '6':
4917 case '7':
4918 case '8':
4919 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920 {
4921#ifdef FEAT_FLOAT
4922 char_u *p = skipdigits(*arg + 1);
4923 int get_float = FALSE;
4924
4925 /* We accept a float when the format matches
4926 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004927 * strict to avoid backwards compatibility problems.
4928 * Don't look for a float after the "." operator, so that
4929 * ":let vers = 1.2.3" doesn't fail. */
4930 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004932 get_float = TRUE;
4933 p = skipdigits(p + 2);
4934 if (*p == 'e' || *p == 'E')
4935 {
4936 ++p;
4937 if (*p == '-' || *p == '+')
4938 ++p;
4939 if (!vim_isdigit(*p))
4940 get_float = FALSE;
4941 else
4942 p = skipdigits(p + 1);
4943 }
4944 if (ASCII_ISALPHA(*p) || *p == '.')
4945 get_float = FALSE;
4946 }
4947 if (get_float)
4948 {
4949 float_T f;
4950
4951 *arg += string2float(*arg, &f);
4952 if (evaluate)
4953 {
4954 rettv->v_type = VAR_FLOAT;
4955 rettv->vval.v_float = f;
4956 }
4957 }
4958 else
4959#endif
4960 {
4961 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4962 *arg += len;
4963 if (evaluate)
4964 {
4965 rettv->v_type = VAR_NUMBER;
4966 rettv->vval.v_number = n;
4967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
4969 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971
4972 /*
4973 * String constant: "string".
4974 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004975 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 break;
4977
4978 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004981 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982 break;
4983
4984 /*
4985 * List: [expr, expr]
4986 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004987 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 break;
4989
4990 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004991 * Dictionary: {key: val, key: val}
4992 */
4993 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4994 break;
4995
4996 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004997 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004999 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 break;
5001
5002 /*
5003 * Environment variable: $VAR.
5004 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005005 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 break;
5007
5008 /*
5009 * Register contents: @r.
5010 */
5011 case '@': ++*arg;
5012 if (evaluate)
5013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005014 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005015 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 if (**arg != NUL)
5018 ++*arg;
5019 break;
5020
5021 /*
5022 * nested expression: (expression).
5023 */
5024 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005025 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 if (**arg == ')')
5027 ++*arg;
5028 else if (ret == OK)
5029 {
5030 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005031 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 ret = FAIL;
5033 }
5034 break;
5035
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 break;
5038 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039
5040 if (ret == NOTDONE)
5041 {
5042 /*
5043 * Must be a variable or function name.
5044 * Can also be a curly-braces kind of name: {expr}.
5045 */
5046 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005047 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 if (alias != NULL)
5049 s = alias;
5050
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005051 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005052 ret = FAIL;
5053 else
5054 {
5055 if (**arg == '(') /* recursive! */
5056 {
5057 /* If "s" is the name of a variable of type VAR_FUNC
5058 * use its contents. */
5059 s = deref_func_name(s, &len);
5060
5061 /* Invoke the function. */
5062 ret = get_func_tv(s, len, rettv, arg,
5063 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005064 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 /* Stop the expression evaluation when immediately
5066 * aborting on error, or when an interrupt occurred or
5067 * an exception was thrown but not caught. */
5068 if (aborting())
5069 {
5070 if (ret == OK)
5071 clear_tv(rettv);
5072 ret = FAIL;
5073 }
5074 }
5075 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005076 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005077 else
5078 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 }
5080
5081 if (alias != NULL)
5082 vim_free(alias);
5083 }
5084
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 *arg = skipwhite(*arg);
5086
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005087 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5088 * expr(expr). */
5089 if (ret == OK)
5090 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
5092 /*
5093 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5094 */
5095 if (ret == OK && evaluate && end_leader > start_leader)
5096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005097 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005098 int val = 0;
5099#ifdef FEAT_FLOAT
5100 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005101
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005102 if (rettv->v_type == VAR_FLOAT)
5103 f = rettv->vval.v_float;
5104 else
5105#endif
5106 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005107 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005109 clear_tv(rettv);
5110 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005112 else
5113 {
5114 while (end_leader > start_leader)
5115 {
5116 --end_leader;
5117 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005118 {
5119#ifdef FEAT_FLOAT
5120 if (rettv->v_type == VAR_FLOAT)
5121 f = !f;
5122 else
5123#endif
5124 val = !val;
5125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005127 {
5128#ifdef FEAT_FLOAT
5129 if (rettv->v_type == VAR_FLOAT)
5130 f = -f;
5131 else
5132#endif
5133 val = -val;
5134 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005135 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005136#ifdef FEAT_FLOAT
5137 if (rettv->v_type == VAR_FLOAT)
5138 {
5139 clear_tv(rettv);
5140 rettv->vval.v_float = f;
5141 }
5142 else
5143#endif
5144 {
5145 clear_tv(rettv);
5146 rettv->v_type = VAR_NUMBER;
5147 rettv->vval.v_number = val;
5148 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151
5152 return ret;
5153}
5154
5155/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005156 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5157 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5159 */
5160 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005161eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005163 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005165 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166{
5167 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005168 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005169 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005170 long len = -1;
5171 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005172 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005175 if (rettv->v_type == VAR_FUNC
5176#ifdef FEAT_FLOAT
5177 || rettv->v_type == VAR_FLOAT
5178#endif
5179 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005180 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005181 if (verbose)
5182 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183 return FAIL;
5184 }
5185
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 /*
5189 * dict.name
5190 */
5191 key = *arg + 1;
5192 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5193 ;
5194 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005197 }
5198 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 /*
5201 * something[idx]
5202 *
5203 * Get the (first) variable from inside the [].
5204 */
5205 *arg = skipwhite(*arg + 1);
5206 if (**arg == ':')
5207 empty1 = TRUE;
5208 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5209 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005210 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5211 {
5212 /* not a number or string */
5213 clear_tv(&var1);
5214 return FAIL;
5215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216
5217 /*
5218 * Get the second variable from inside the [:].
5219 */
5220 if (**arg == ':')
5221 {
5222 range = TRUE;
5223 *arg = skipwhite(*arg + 1);
5224 if (**arg == ']')
5225 empty2 = TRUE;
5226 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 if (!empty1)
5229 clear_tv(&var1);
5230 return FAIL;
5231 }
5232 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5233 {
5234 /* not a number or string */
5235 if (!empty1)
5236 clear_tv(&var1);
5237 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 return FAIL;
5239 }
5240 }
5241
5242 /* Check for the ']'. */
5243 if (**arg != ']')
5244 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005245 if (verbose)
5246 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 clear_tv(&var1);
5248 if (range)
5249 clear_tv(&var2);
5250 return FAIL;
5251 }
5252 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 }
5254
5255 if (evaluate)
5256 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257 n1 = 0;
5258 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005260 n1 = get_tv_number(&var1);
5261 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 }
5263 if (range)
5264 {
5265 if (empty2)
5266 n2 = -1;
5267 else
5268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005269 n2 = get_tv_number(&var2);
5270 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 }
5272 }
5273
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 {
5276 case VAR_NUMBER:
5277 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005278 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 len = (long)STRLEN(s);
5280 if (range)
5281 {
5282 /* The resulting variable is a substring. If the indexes
5283 * are out of range the result is empty. */
5284 if (n1 < 0)
5285 {
5286 n1 = len + n1;
5287 if (n1 < 0)
5288 n1 = 0;
5289 }
5290 if (n2 < 0)
5291 n2 = len + n2;
5292 else if (n2 >= len)
5293 n2 = len;
5294 if (n1 >= len || n2 < 0 || n1 > n2)
5295 s = NULL;
5296 else
5297 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5298 }
5299 else
5300 {
5301 /* The resulting variable is a string of a single
5302 * character. If the index is too big or negative the
5303 * result is empty. */
5304 if (n1 >= len || n1 < 0)
5305 s = NULL;
5306 else
5307 s = vim_strnsave(s + n1, 1);
5308 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005309 clear_tv(rettv);
5310 rettv->v_type = VAR_STRING;
5311 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 break;
5313
5314 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005315 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 if (n1 < 0)
5317 n1 = len + n1;
5318 if (!empty1 && (n1 < 0 || n1 >= len))
5319 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005320 /* For a range we allow invalid values and return an empty
5321 * list. A list index out of range is an error. */
5322 if (!range)
5323 {
5324 if (verbose)
5325 EMSGN(_(e_listidx), n1);
5326 return FAIL;
5327 }
5328 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 }
5330 if (range)
5331 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005332 list_T *l;
5333 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334
5335 if (n2 < 0)
5336 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005337 else if (n2 >= len)
5338 n2 = len - 1;
5339 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005340 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 l = list_alloc();
5342 if (l == NULL)
5343 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005344 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 n1 <= n2; ++n1)
5346 {
5347 if (list_append_tv(l, &item->li_tv) == FAIL)
5348 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005349 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 return FAIL;
5351 }
5352 item = item->li_next;
5353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 clear_tv(rettv);
5355 rettv->v_type = VAR_LIST;
5356 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005357 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 }
5359 else
5360 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005361 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005362 clear_tv(rettv);
5363 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 }
5365 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005366
5367 case VAR_DICT:
5368 if (range)
5369 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005370 if (verbose)
5371 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 if (len == -1)
5373 clear_tv(&var1);
5374 return FAIL;
5375 }
5376 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005377 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005378
5379 if (len == -1)
5380 {
5381 key = get_tv_string(&var1);
5382 if (*key == NUL)
5383 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005384 if (verbose)
5385 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386 clear_tv(&var1);
5387 return FAIL;
5388 }
5389 }
5390
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005391 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005393 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005394 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 if (len == -1)
5396 clear_tv(&var1);
5397 if (item == NULL)
5398 return FAIL;
5399
5400 copy_tv(&item->di_tv, &var1);
5401 clear_tv(rettv);
5402 *rettv = var1;
5403 }
5404 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 }
5406 }
5407
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 return OK;
5409}
5410
5411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 * Get an option value.
5413 * "arg" points to the '&' or '+' before the option name.
5414 * "arg" is advanced to character after the option name.
5415 * Return OK or FAIL.
5416 */
5417 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005420 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 int evaluate;
5422{
5423 char_u *option_end;
5424 long numval;
5425 char_u *stringval;
5426 int opt_type;
5427 int c;
5428 int working = (**arg == '+'); /* has("+option") */
5429 int ret = OK;
5430 int opt_flags;
5431
5432 /*
5433 * Isolate the option name and find its value.
5434 */
5435 option_end = find_option_end(arg, &opt_flags);
5436 if (option_end == NULL)
5437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 EMSG2(_("E112: Option name missing: %s"), *arg);
5440 return FAIL;
5441 }
5442
5443 if (!evaluate)
5444 {
5445 *arg = option_end;
5446 return OK;
5447 }
5448
5449 c = *option_end;
5450 *option_end = NUL;
5451 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005452 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
5454 if (opt_type == -3) /* invalid name */
5455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 EMSG2(_("E113: Unknown option: %s"), *arg);
5458 ret = FAIL;
5459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 {
5462 if (opt_type == -2) /* hidden string option */
5463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 rettv->v_type = VAR_STRING;
5465 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
5467 else if (opt_type == -1) /* hidden number option */
5468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 rettv->v_type = VAR_NUMBER;
5470 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472 else if (opt_type == 1) /* number option */
5473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 rettv->v_type = VAR_NUMBER;
5475 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 }
5477 else /* string option */
5478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 rettv->v_type = VAR_STRING;
5480 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 }
5482 }
5483 else if (working && (opt_type == -2 || opt_type == -1))
5484 ret = FAIL;
5485
5486 *option_end = c; /* put back for error messages */
5487 *arg = option_end;
5488
5489 return ret;
5490}
5491
5492/*
5493 * Allocate a variable for a string constant.
5494 * Return OK or FAIL.
5495 */
5496 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005497get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 int evaluate;
5501{
5502 char_u *p;
5503 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 int extra = 0;
5505
5506 /*
5507 * Find the end of the string, skipping backslashed characters.
5508 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005509 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
5511 if (*p == '\\' && p[1] != NUL)
5512 {
5513 ++p;
5514 /* A "\<x>" form occupies at least 4 characters, and produces up
5515 * to 6 characters: reserve space for 2 extra */
5516 if (*p == '<')
5517 extra += 2;
5518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 }
5520
5521 if (*p != '"')
5522 {
5523 EMSG2(_("E114: Missing quote: %s"), *arg);
5524 return FAIL;
5525 }
5526
5527 /* If only parsing, set *arg and return here */
5528 if (!evaluate)
5529 {
5530 *arg = p + 1;
5531 return OK;
5532 }
5533
5534 /*
5535 * Copy the string into allocated memory, handling backslashed
5536 * characters.
5537 */
5538 name = alloc((unsigned)(p - *arg + extra));
5539 if (name == NULL)
5540 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005541 rettv->v_type = VAR_STRING;
5542 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
Bram Moolenaar8c711452005-01-14 21:53:12 +00005544 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 {
5546 if (*p == '\\')
5547 {
5548 switch (*++p)
5549 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005550 case 'b': *name++ = BS; ++p; break;
5551 case 'e': *name++ = ESC; ++p; break;
5552 case 'f': *name++ = FF; ++p; break;
5553 case 'n': *name++ = NL; ++p; break;
5554 case 'r': *name++ = CAR; ++p; break;
5555 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
5557 case 'X': /* hex: "\x1", "\x12" */
5558 case 'x':
5559 case 'u': /* Unicode: "\u0023" */
5560 case 'U':
5561 if (vim_isxdigit(p[1]))
5562 {
5563 int n, nr;
5564 int c = toupper(*p);
5565
5566 if (c == 'X')
5567 n = 2;
5568 else
5569 n = 4;
5570 nr = 0;
5571 while (--n >= 0 && vim_isxdigit(p[1]))
5572 {
5573 ++p;
5574 nr = (nr << 4) + hex2nr(*p);
5575 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005576 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577#ifdef FEAT_MBYTE
5578 /* For "\u" store the number according to
5579 * 'encoding'. */
5580 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 else
5583#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 break;
5587
5588 /* octal: "\1", "\12", "\123" */
5589 case '0':
5590 case '1':
5591 case '2':
5592 case '3':
5593 case '4':
5594 case '5':
5595 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 case '7': *name = *p++ - '0';
5597 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 *name = (*name << 3) + *p++ - '0';
5600 if (*p >= '0' && *p <= '7')
5601 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 break;
5605
5606 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 if (extra != 0)
5609 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 break;
5612 }
5613 /* FALLTHROUGH */
5614
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 break;
5617 }
5618 }
5619 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005620 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 *arg = p + 1;
5625
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 return OK;
5627}
5628
5629/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 * Return OK or FAIL.
5632 */
5633 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 int evaluate;
5638{
5639 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005640 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 int reduce = 0;
5642
5643 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005645 */
5646 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5647 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005649 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005651 break;
5652 ++reduce;
5653 ++p;
5654 }
5655 }
5656
Bram Moolenaar8c711452005-01-14 21:53:12 +00005657 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005658 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005660 return FAIL;
5661 }
5662
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005664 if (!evaluate)
5665 {
5666 *arg = p + 1;
5667 return OK;
5668 }
5669
5670 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005672 */
5673 str = alloc((unsigned)((p - *arg) - reduce));
5674 if (str == NULL)
5675 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 rettv->v_type = VAR_STRING;
5677 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005678
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005684 break;
5685 ++p;
5686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005690 *arg = p + 1;
5691
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005692 return OK;
5693}
5694
5695/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 * Allocate a variable for a List and fill it from "*arg".
5697 * Return OK or FAIL.
5698 */
5699 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005700get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005702 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703 int evaluate;
5704{
Bram Moolenaar33570922005-01-25 22:26:29 +00005705 list_T *l = NULL;
5706 typval_T tv;
5707 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005708
5709 if (evaluate)
5710 {
5711 l = list_alloc();
5712 if (l == NULL)
5713 return FAIL;
5714 }
5715
5716 *arg = skipwhite(*arg + 1);
5717 while (**arg != ']' && **arg != NUL)
5718 {
5719 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5720 goto failret;
5721 if (evaluate)
5722 {
5723 item = listitem_alloc();
5724 if (item != NULL)
5725 {
5726 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005727 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728 list_append(l, item);
5729 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005730 else
5731 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732 }
5733
5734 if (**arg == ']')
5735 break;
5736 if (**arg != ',')
5737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005739 goto failret;
5740 }
5741 *arg = skipwhite(*arg + 1);
5742 }
5743
5744 if (**arg != ']')
5745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747failret:
5748 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005749 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005750 return FAIL;
5751 }
5752
5753 *arg = skipwhite(*arg + 1);
5754 if (evaluate)
5755 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005756 rettv->v_type = VAR_LIST;
5757 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005758 ++l->lv_refcount;
5759 }
5760
5761 return OK;
5762}
5763
5764/*
5765 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005766 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005767 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005768 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769list_alloc()
5770{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005771 list_T *l;
5772
5773 l = (list_T *)alloc_clear(sizeof(list_T));
5774 if (l != NULL)
5775 {
5776 /* Prepend the list to the list of lists for garbage collection. */
5777 if (first_list != NULL)
5778 first_list->lv_used_prev = l;
5779 l->lv_used_prev = NULL;
5780 l->lv_used_next = first_list;
5781 first_list = l;
5782 }
5783 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005784}
5785
5786/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005787 * Allocate an empty list for a return value.
5788 * Returns OK or FAIL.
5789 */
5790 static int
5791rettv_list_alloc(rettv)
5792 typval_T *rettv;
5793{
5794 list_T *l = list_alloc();
5795
5796 if (l == NULL)
5797 return FAIL;
5798
5799 rettv->vval.v_list = l;
5800 rettv->v_type = VAR_LIST;
5801 ++l->lv_refcount;
5802 return OK;
5803}
5804
5805/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 * Unreference a list: decrement the reference count and free it when it
5807 * becomes zero.
5808 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005809 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005810list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005811 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005812{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005813 if (l != NULL && --l->lv_refcount <= 0)
5814 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005815}
5816
5817/*
5818 * Free a list, including all items it points to.
5819 * Ignores the reference count.
5820 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005821 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005822list_free(l, recurse)
5823 list_T *l;
5824 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825{
Bram Moolenaar33570922005-01-25 22:26:29 +00005826 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005828 /* Remove the list from the list of lists for garbage collection. */
5829 if (l->lv_used_prev == NULL)
5830 first_list = l->lv_used_next;
5831 else
5832 l->lv_used_prev->lv_used_next = l->lv_used_next;
5833 if (l->lv_used_next != NULL)
5834 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5835
Bram Moolenaard9fba312005-06-26 22:34:35 +00005836 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005838 /* Remove the item before deleting it. */
5839 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005840 if (recurse || (item->li_tv.v_type != VAR_LIST
5841 && item->li_tv.v_type != VAR_DICT))
5842 clear_tv(&item->li_tv);
5843 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844 }
5845 vim_free(l);
5846}
5847
5848/*
5849 * Allocate a list item.
5850 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005851 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852listitem_alloc()
5853{
Bram Moolenaar33570922005-01-25 22:26:29 +00005854 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855}
5856
5857/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005858 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 */
5860 static void
5861listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005862 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005864 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 vim_free(item);
5866}
5867
5868/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005869 * Remove a list item from a List and free it. Also clears the value.
5870 */
5871 static void
5872listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005873 list_T *l;
5874 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005875{
5876 list_remove(l, item, item);
5877 listitem_free(item);
5878}
5879
5880/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 * Get the number of items in a list.
5882 */
5883 static long
5884list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887 if (l == NULL)
5888 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005889 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890}
5891
5892/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005893 * Return TRUE when two lists have exactly the same values.
5894 */
5895 static int
5896list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005897 list_T *l1;
5898 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005899 int ic; /* ignore case for strings */
5900{
Bram Moolenaar33570922005-01-25 22:26:29 +00005901 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005902
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005903 if (l1 == NULL || l2 == NULL)
5904 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005905 if (l1 == l2)
5906 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005907 if (list_len(l1) != list_len(l2))
5908 return FALSE;
5909
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005910 for (item1 = l1->lv_first, item2 = l2->lv_first;
5911 item1 != NULL && item2 != NULL;
5912 item1 = item1->li_next, item2 = item2->li_next)
5913 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5914 return FALSE;
5915 return item1 == NULL && item2 == NULL;
5916}
5917
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005918#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5919 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005920/*
5921 * Return the dictitem that an entry in a hashtable points to.
5922 */
5923 dictitem_T *
5924dict_lookup(hi)
5925 hashitem_T *hi;
5926{
5927 return HI2DI(hi);
5928}
5929#endif
5930
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005931/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005932 * Return TRUE when two dictionaries have exactly the same key/values.
5933 */
5934 static int
5935dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 dict_T *d1;
5937 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005938 int ic; /* ignore case for strings */
5939{
Bram Moolenaar33570922005-01-25 22:26:29 +00005940 hashitem_T *hi;
5941 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005942 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005943
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005944 if (d1 == NULL || d2 == NULL)
5945 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005946 if (d1 == d2)
5947 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005948 if (dict_len(d1) != dict_len(d2))
5949 return FALSE;
5950
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005951 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005952 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005954 if (!HASHITEM_EMPTY(hi))
5955 {
5956 item2 = dict_find(d2, hi->hi_key, -1);
5957 if (item2 == NULL)
5958 return FALSE;
5959 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5960 return FALSE;
5961 --todo;
5962 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005963 }
5964 return TRUE;
5965}
5966
5967/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005968 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005969 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005970 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005971 */
5972 static int
5973tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005974 typval_T *tv1;
5975 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005976 int ic; /* ignore case */
5977{
5978 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005979 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005980 static int recursive = 0; /* cach recursive loops */
5981 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005982
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005983 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005984 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005985 /* Catch lists and dicts that have an endless loop by limiting
5986 * recursiveness to 1000. We guess they are equal then. */
5987 if (recursive >= 1000)
5988 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005989
5990 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005991 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005992 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005993 ++recursive;
5994 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5995 --recursive;
5996 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005997
5998 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005999 ++recursive;
6000 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
6001 --recursive;
6002 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006003
6004 case VAR_FUNC:
6005 return (tv1->vval.v_string != NULL
6006 && tv2->vval.v_string != NULL
6007 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6008
6009 case VAR_NUMBER:
6010 return tv1->vval.v_number == tv2->vval.v_number;
6011
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006012#ifdef FEAT_FLOAT
6013 case VAR_FLOAT:
6014 return tv1->vval.v_float == tv2->vval.v_float;
6015#endif
6016
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006017 case VAR_STRING:
6018 s1 = get_tv_string_buf(tv1, buf1);
6019 s2 = get_tv_string_buf(tv2, buf2);
6020 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006021 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006022
6023 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024 return TRUE;
6025}
6026
6027/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 * Locate item with index "n" in list "l" and return it.
6029 * A negative index is counted from the end; -1 is the last item.
6030 * Returns NULL when "n" is out of range.
6031 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 long n;
6036{
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038 long idx;
6039
6040 if (l == NULL)
6041 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006042
6043 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006045 n = l->lv_len + n;
6046
6047 /* Check for index out of range. */
6048 if (n < 0 || n >= l->lv_len)
6049 return NULL;
6050
6051 /* When there is a cached index may start search from there. */
6052 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006054 if (n < l->lv_idx / 2)
6055 {
6056 /* closest to the start of the list */
6057 item = l->lv_first;
6058 idx = 0;
6059 }
6060 else if (n > (l->lv_idx + l->lv_len) / 2)
6061 {
6062 /* closest to the end of the list */
6063 item = l->lv_last;
6064 idx = l->lv_len - 1;
6065 }
6066 else
6067 {
6068 /* closest to the cached index */
6069 item = l->lv_idx_item;
6070 idx = l->lv_idx;
6071 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 }
6073 else
6074 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006075 if (n < l->lv_len / 2)
6076 {
6077 /* closest to the start of the list */
6078 item = l->lv_first;
6079 idx = 0;
6080 }
6081 else
6082 {
6083 /* closest to the end of the list */
6084 item = l->lv_last;
6085 idx = l->lv_len - 1;
6086 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006088
6089 while (n > idx)
6090 {
6091 /* search forward */
6092 item = item->li_next;
6093 ++idx;
6094 }
6095 while (n < idx)
6096 {
6097 /* search backward */
6098 item = item->li_prev;
6099 --idx;
6100 }
6101
6102 /* cache the used index */
6103 l->lv_idx = idx;
6104 l->lv_idx_item = item;
6105
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106 return item;
6107}
6108
6109/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006110 * Get list item "l[idx]" as a number.
6111 */
6112 static long
6113list_find_nr(l, idx, errorp)
6114 list_T *l;
6115 long idx;
6116 int *errorp; /* set to TRUE when something wrong */
6117{
6118 listitem_T *li;
6119
6120 li = list_find(l, idx);
6121 if (li == NULL)
6122 {
6123 if (errorp != NULL)
6124 *errorp = TRUE;
6125 return -1L;
6126 }
6127 return get_tv_number_chk(&li->li_tv, errorp);
6128}
6129
6130/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006131 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6132 */
6133 char_u *
6134list_find_str(l, idx)
6135 list_T *l;
6136 long idx;
6137{
6138 listitem_T *li;
6139
6140 li = list_find(l, idx - 1);
6141 if (li == NULL)
6142 {
6143 EMSGN(_(e_listidx), idx);
6144 return NULL;
6145 }
6146 return get_tv_string(&li->li_tv);
6147}
6148
6149/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006150 * Locate "item" list "l" and return its index.
6151 * Returns -1 when "item" is not in the list.
6152 */
6153 static long
6154list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 list_T *l;
6156 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006157{
6158 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006159 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006160
6161 if (l == NULL)
6162 return -1;
6163 idx = 0;
6164 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6165 ++idx;
6166 if (li == NULL)
6167 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006168 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006169}
6170
6171/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172 * Append item "item" to the end of list "l".
6173 */
6174 static void
6175list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006176 list_T *l;
6177 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178{
6179 if (l->lv_last == NULL)
6180 {
6181 /* empty list */
6182 l->lv_first = item;
6183 l->lv_last = item;
6184 item->li_prev = NULL;
6185 }
6186 else
6187 {
6188 l->lv_last->li_next = item;
6189 item->li_prev = l->lv_last;
6190 l->lv_last = item;
6191 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006192 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 item->li_next = NULL;
6194}
6195
6196/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006197 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006198 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006200 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006202 list_T *l;
6203 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006204{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006205 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006206
Bram Moolenaar05159a02005-02-26 23:04:13 +00006207 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006209 copy_tv(tv, &li->li_tv);
6210 list_append(l, li);
6211 return OK;
6212}
6213
6214/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006215 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006216 * Return FAIL when out of memory.
6217 */
6218 int
6219list_append_dict(list, dict)
6220 list_T *list;
6221 dict_T *dict;
6222{
6223 listitem_T *li = listitem_alloc();
6224
6225 if (li == NULL)
6226 return FAIL;
6227 li->li_tv.v_type = VAR_DICT;
6228 li->li_tv.v_lock = 0;
6229 li->li_tv.vval.v_dict = dict;
6230 list_append(list, li);
6231 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006232 return OK;
6233}
6234
6235/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006236 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006237 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006238 * Returns FAIL when out of memory.
6239 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006240 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006241list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006242 list_T *l;
6243 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006244 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006245{
6246 listitem_T *li = listitem_alloc();
6247
6248 if (li == NULL)
6249 return FAIL;
6250 list_append(l, li);
6251 li->li_tv.v_type = VAR_STRING;
6252 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006253 if (str == NULL)
6254 li->li_tv.vval.v_string = NULL;
6255 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006256 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006257 return FAIL;
6258 return OK;
6259}
6260
6261/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006262 * Append "n" to list "l".
6263 * Returns FAIL when out of memory.
6264 */
6265 static int
6266list_append_number(l, n)
6267 list_T *l;
6268 varnumber_T n;
6269{
6270 listitem_T *li;
6271
6272 li = listitem_alloc();
6273 if (li == NULL)
6274 return FAIL;
6275 li->li_tv.v_type = VAR_NUMBER;
6276 li->li_tv.v_lock = 0;
6277 li->li_tv.vval.v_number = n;
6278 list_append(l, li);
6279 return OK;
6280}
6281
6282/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006284 * If "item" is NULL append at the end.
6285 * Return FAIL when out of memory.
6286 */
6287 static int
6288list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 list_T *l;
6290 typval_T *tv;
6291 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006292{
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006294
6295 if (ni == NULL)
6296 return FAIL;
6297 copy_tv(tv, &ni->li_tv);
6298 if (item == NULL)
6299 /* Append new item at end of list. */
6300 list_append(l, ni);
6301 else
6302 {
6303 /* Insert new item before existing item. */
6304 ni->li_prev = item->li_prev;
6305 ni->li_next = item;
6306 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006307 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006308 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006309 ++l->lv_idx;
6310 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006311 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006312 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006313 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006314 l->lv_idx_item = NULL;
6315 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006316 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006317 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006318 }
6319 return OK;
6320}
6321
6322/*
6323 * Extend "l1" with "l2".
6324 * If "bef" is NULL append at the end, otherwise insert before this item.
6325 * Returns FAIL when out of memory.
6326 */
6327 static int
6328list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 list_T *l1;
6330 list_T *l2;
6331 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006332{
Bram Moolenaar33570922005-01-25 22:26:29 +00006333 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006334 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006335
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006336 /* We also quit the loop when we have inserted the original item count of
6337 * the list, avoid a hang when we extend a list with itself. */
6338 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006339 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6340 return FAIL;
6341 return OK;
6342}
6343
6344/*
6345 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6346 * Return FAIL when out of memory.
6347 */
6348 static int
6349list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 list_T *l1;
6351 list_T *l2;
6352 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006353{
Bram Moolenaar33570922005-01-25 22:26:29 +00006354 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006355
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006356 if (l1 == NULL || l2 == NULL)
6357 return FAIL;
6358
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006359 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006360 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006361 if (l == NULL)
6362 return FAIL;
6363 tv->v_type = VAR_LIST;
6364 tv->vval.v_list = l;
6365
6366 /* append all items from the second list */
6367 return list_extend(l, l2, NULL);
6368}
6369
6370/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006371 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006373 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006374 * Returns NULL when out of memory.
6375 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006376 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006377list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006380 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381{
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 list_T *copy;
6383 listitem_T *item;
6384 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006385
6386 if (orig == NULL)
6387 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388
6389 copy = list_alloc();
6390 if (copy != NULL)
6391 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006392 if (copyID != 0)
6393 {
6394 /* Do this before adding the items, because one of the items may
6395 * refer back to this list. */
6396 orig->lv_copyID = copyID;
6397 orig->lv_copylist = copy;
6398 }
6399 for (item = orig->lv_first; item != NULL && !got_int;
6400 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006401 {
6402 ni = listitem_alloc();
6403 if (ni == NULL)
6404 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006405 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006406 {
6407 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6408 {
6409 vim_free(ni);
6410 break;
6411 }
6412 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006414 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415 list_append(copy, ni);
6416 }
6417 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006418 if (item != NULL)
6419 {
6420 list_unref(copy);
6421 copy = NULL;
6422 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 }
6424
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425 return copy;
6426}
6427
6428/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006430 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006433list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 list_T *l;
6435 listitem_T *item;
6436 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437{
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440 /* notify watchers */
6441 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006443 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006444 list_fix_watch(l, ip);
6445 if (ip == item2)
6446 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448
6449 if (item2->li_next == NULL)
6450 l->lv_last = item->li_prev;
6451 else
6452 item2->li_next->li_prev = item->li_prev;
6453 if (item->li_prev == NULL)
6454 l->lv_first = item2->li_next;
6455 else
6456 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006458}
6459
6460/*
6461 * Return an allocated string with the string representation of a list.
6462 * May return NULL.
6463 */
6464 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006465list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006466 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006467 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006468{
6469 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006470
6471 if (tv->vval.v_list == NULL)
6472 return NULL;
6473 ga_init2(&ga, (int)sizeof(char), 80);
6474 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006475 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006476 {
6477 vim_free(ga.ga_data);
6478 return NULL;
6479 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006480 ga_append(&ga, ']');
6481 ga_append(&ga, NUL);
6482 return (char_u *)ga.ga_data;
6483}
6484
6485/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006486 * Join list "l" into a string in "*gap", using separator "sep".
6487 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006489 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006490 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006491list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006492 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006494 char_u *sep;
6495 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006496 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006497{
6498 int first = TRUE;
6499 char_u *tofree;
6500 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006502 char_u *s;
6503
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006504 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006505 {
6506 if (first)
6507 first = FALSE;
6508 else
6509 ga_concat(gap, sep);
6510
6511 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006512 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006513 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006514 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006515 if (s != NULL)
6516 ga_concat(gap, s);
6517 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006518 if (s == NULL)
6519 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006520 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006521 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006522 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006523}
6524
6525/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006526 * Garbage collection for lists and dictionaries.
6527 *
6528 * We use reference counts to be able to free most items right away when they
6529 * are no longer used. But for composite items it's possible that it becomes
6530 * unused while the reference count is > 0: When there is a recursive
6531 * reference. Example:
6532 * :let l = [1, 2, 3]
6533 * :let d = {9: l}
6534 * :let l[1] = d
6535 *
6536 * Since this is quite unusual we handle this with garbage collection: every
6537 * once in a while find out which lists and dicts are not referenced from any
6538 * variable.
6539 *
6540 * Here is a good reference text about garbage collection (refers to Python
6541 * but it applies to all reference-counting mechanisms):
6542 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006543 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006544
6545/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006546 * Do garbage collection for lists and dicts.
6547 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006548 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006549 int
6550garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006551{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006552 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006553 buf_T *buf;
6554 win_T *wp;
6555 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006556 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006557 int did_free;
6558 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006559#ifdef FEAT_WINDOWS
6560 tabpage_T *tp;
6561#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006562
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006563 /* Only do this once. */
6564 want_garbage_collect = FALSE;
6565 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006566 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006567
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006568 /* We advance by two because we add one for items referenced through
6569 * previous_funccal. */
6570 current_copyID += COPYID_INC;
6571 copyID = current_copyID;
6572
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006573 /*
6574 * 1. Go through all accessible variables and mark all lists and dicts
6575 * with copyID.
6576 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006577
6578 /* Don't free variables in the previous_funccal list unless they are only
6579 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006580 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006581 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6582 {
6583 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6584 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6585 }
6586
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006587 /* script-local variables */
6588 for (i = 1; i <= ga_scripts.ga_len; ++i)
6589 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6590
6591 /* buffer-local variables */
6592 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6593 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6594
6595 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006596 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006597 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6598
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006599#ifdef FEAT_WINDOWS
6600 /* tabpage-local variables */
6601 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6602 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6603#endif
6604
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006605 /* global variables */
6606 set_ref_in_ht(&globvarht, copyID);
6607
6608 /* function-local variables */
6609 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6610 {
6611 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6612 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6613 }
6614
Bram Moolenaard812df62008-11-09 12:46:09 +00006615 /* v: vars */
6616 set_ref_in_ht(&vimvarht, copyID);
6617
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006618 /*
6619 * 2. Free lists and dictionaries that are not referenced.
6620 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006621 did_free = free_unref_items(copyID);
6622
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006623 /*
6624 * 3. Check if any funccal can be freed now.
6625 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006626 for (pfc = &previous_funccal; *pfc != NULL; )
6627 {
6628 if (can_free_funccal(*pfc, copyID))
6629 {
6630 fc = *pfc;
6631 *pfc = fc->caller;
6632 free_funccal(fc, TRUE);
6633 did_free = TRUE;
6634 did_free_funccal = TRUE;
6635 }
6636 else
6637 pfc = &(*pfc)->caller;
6638 }
6639 if (did_free_funccal)
6640 /* When a funccal was freed some more items might be garbage
6641 * collected, so run again. */
6642 (void)garbage_collect();
6643
6644 return did_free;
6645}
6646
6647/*
6648 * Free lists and dictionaries that are no longer referenced.
6649 */
6650 static int
6651free_unref_items(copyID)
6652 int copyID;
6653{
6654 dict_T *dd;
6655 list_T *ll;
6656 int did_free = FALSE;
6657
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006658 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006659 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006660 */
6661 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006662 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006663 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006664 /* Free the Dictionary and ordinary items it contains, but don't
6665 * recurse into Lists and Dictionaries, they will be in the list
6666 * of dicts or list of lists. */
6667 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006668 did_free = TRUE;
6669
6670 /* restart, next dict may also have been freed */
6671 dd = first_dict;
6672 }
6673 else
6674 dd = dd->dv_used_next;
6675
6676 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006677 * Go through the list of lists and free items without the copyID.
6678 * But don't free a list that has a watcher (used in a for loop), these
6679 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006680 */
6681 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006682 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6683 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006684 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006685 /* Free the List and ordinary items it contains, but don't recurse
6686 * into Lists and Dictionaries, they will be in the list of dicts
6687 * or list of lists. */
6688 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006689 did_free = TRUE;
6690
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006691 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006692 ll = first_list;
6693 }
6694 else
6695 ll = ll->lv_used_next;
6696
6697 return did_free;
6698}
6699
6700/*
6701 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6702 */
6703 static void
6704set_ref_in_ht(ht, copyID)
6705 hashtab_T *ht;
6706 int copyID;
6707{
6708 int todo;
6709 hashitem_T *hi;
6710
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006711 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006712 for (hi = ht->ht_array; todo > 0; ++hi)
6713 if (!HASHITEM_EMPTY(hi))
6714 {
6715 --todo;
6716 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6717 }
6718}
6719
6720/*
6721 * Mark all lists and dicts referenced through list "l" with "copyID".
6722 */
6723 static void
6724set_ref_in_list(l, copyID)
6725 list_T *l;
6726 int copyID;
6727{
6728 listitem_T *li;
6729
6730 for (li = l->lv_first; li != NULL; li = li->li_next)
6731 set_ref_in_item(&li->li_tv, copyID);
6732}
6733
6734/*
6735 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6736 */
6737 static void
6738set_ref_in_item(tv, copyID)
6739 typval_T *tv;
6740 int copyID;
6741{
6742 dict_T *dd;
6743 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006744
6745 switch (tv->v_type)
6746 {
6747 case VAR_DICT:
6748 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006749 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006750 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006751 /* Didn't see this dict yet. */
6752 dd->dv_copyID = copyID;
6753 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006754 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006755 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006756
6757 case VAR_LIST:
6758 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006759 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006760 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006761 /* Didn't see this list yet. */
6762 ll->lv_copyID = copyID;
6763 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006765 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006766 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006768}
6769
6770/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006771 * Allocate an empty header for a dictionary.
6772 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006773 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006774dict_alloc()
6775{
Bram Moolenaar33570922005-01-25 22:26:29 +00006776 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006777
Bram Moolenaar33570922005-01-25 22:26:29 +00006778 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006779 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006780 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006781 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006782 if (first_dict != NULL)
6783 first_dict->dv_used_prev = d;
6784 d->dv_used_next = first_dict;
6785 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006786 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006787
Bram Moolenaar33570922005-01-25 22:26:29 +00006788 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006789 d->dv_lock = 0;
6790 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006791 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006792 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006793 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006794}
6795
6796/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006797 * Allocate an empty dict for a return value.
6798 * Returns OK or FAIL.
6799 */
6800 static int
6801rettv_dict_alloc(rettv)
6802 typval_T *rettv;
6803{
6804 dict_T *d = dict_alloc();
6805
6806 if (d == NULL)
6807 return FAIL;
6808
6809 rettv->vval.v_dict = d;
6810 rettv->v_type = VAR_DICT;
6811 ++d->dv_refcount;
6812 return OK;
6813}
6814
6815
6816/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006817 * Unreference a Dictionary: decrement the reference count and free it when it
6818 * becomes zero.
6819 */
6820 static void
6821dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006822 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006823{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006824 if (d != NULL && --d->dv_refcount <= 0)
6825 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006826}
6827
6828/*
6829 * Free a Dictionary, including all items it contains.
6830 * Ignores the reference count.
6831 */
6832 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006833dict_free(d, recurse)
6834 dict_T *d;
6835 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006836{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006837 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006838 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006840
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006841 /* Remove the dict from the list of dicts for garbage collection. */
6842 if (d->dv_used_prev == NULL)
6843 first_dict = d->dv_used_next;
6844 else
6845 d->dv_used_prev->dv_used_next = d->dv_used_next;
6846 if (d->dv_used_next != NULL)
6847 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6848
6849 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006850 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006851 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006852 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006853 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006854 if (!HASHITEM_EMPTY(hi))
6855 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006856 /* Remove the item before deleting it, just in case there is
6857 * something recursive causing trouble. */
6858 di = HI2DI(hi);
6859 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006860 if (recurse || (di->di_tv.v_type != VAR_LIST
6861 && di->di_tv.v_type != VAR_DICT))
6862 clear_tv(&di->di_tv);
6863 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006864 --todo;
6865 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006866 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006867 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006868 vim_free(d);
6869}
6870
6871/*
6872 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006873 * The "key" is copied to the new item.
6874 * Note that the value of the item "di_tv" still needs to be initialized!
6875 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006876 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006877 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006878dictitem_alloc(key)
6879 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006880{
Bram Moolenaar33570922005-01-25 22:26:29 +00006881 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006882
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006883 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006884 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006885 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006886 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006887 di->di_flags = 0;
6888 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006889 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006890}
6891
6892/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006893 * Make a copy of a Dictionary item.
6894 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006895 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006896dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006897 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006898{
Bram Moolenaar33570922005-01-25 22:26:29 +00006899 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006901 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6902 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006903 if (di != NULL)
6904 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006905 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006906 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006907 copy_tv(&org->di_tv, &di->di_tv);
6908 }
6909 return di;
6910}
6911
6912/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006913 * Remove item "item" from Dictionary "dict" and free it.
6914 */
6915 static void
6916dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006917 dict_T *dict;
6918 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006919{
Bram Moolenaar33570922005-01-25 22:26:29 +00006920 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006921
Bram Moolenaar33570922005-01-25 22:26:29 +00006922 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006923 if (HASHITEM_EMPTY(hi))
6924 EMSG2(_(e_intern2), "dictitem_remove()");
6925 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006926 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006927 dictitem_free(item);
6928}
6929
6930/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006931 * Free a dict item. Also clears the value.
6932 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006933 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006934dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006935 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006936{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006937 clear_tv(&item->di_tv);
6938 vim_free(item);
6939}
6940
6941/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006942 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6943 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006944 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006945 * Returns NULL when out of memory.
6946 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006947 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006948dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006949 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006950 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006951 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006952{
Bram Moolenaar33570922005-01-25 22:26:29 +00006953 dict_T *copy;
6954 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006955 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006956 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006957
6958 if (orig == NULL)
6959 return NULL;
6960
6961 copy = dict_alloc();
6962 if (copy != NULL)
6963 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006964 if (copyID != 0)
6965 {
6966 orig->dv_copyID = copyID;
6967 orig->dv_copydict = copy;
6968 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006969 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006970 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006971 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006972 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006973 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006974 --todo;
6975
6976 di = dictitem_alloc(hi->hi_key);
6977 if (di == NULL)
6978 break;
6979 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006980 {
6981 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6982 copyID) == FAIL)
6983 {
6984 vim_free(di);
6985 break;
6986 }
6987 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006988 else
6989 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6990 if (dict_add(copy, di) == FAIL)
6991 {
6992 dictitem_free(di);
6993 break;
6994 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006995 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006996 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006997
Bram Moolenaare9a41262005-01-15 22:18:47 +00006998 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006999 if (todo > 0)
7000 {
7001 dict_unref(copy);
7002 copy = NULL;
7003 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007004 }
7005
7006 return copy;
7007}
7008
7009/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007011 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007012 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007013 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007014dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 dict_T *d;
7016 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017{
Bram Moolenaar33570922005-01-25 22:26:29 +00007018 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019}
7020
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007022 * Add a number or string entry to dictionary "d".
7023 * When "str" is NULL use number "nr", otherwise use "str".
7024 * Returns FAIL when out of memory and when key already exists.
7025 */
7026 int
7027dict_add_nr_str(d, key, nr, str)
7028 dict_T *d;
7029 char *key;
7030 long nr;
7031 char_u *str;
7032{
7033 dictitem_T *item;
7034
7035 item = dictitem_alloc((char_u *)key);
7036 if (item == NULL)
7037 return FAIL;
7038 item->di_tv.v_lock = 0;
7039 if (str == NULL)
7040 {
7041 item->di_tv.v_type = VAR_NUMBER;
7042 item->di_tv.vval.v_number = nr;
7043 }
7044 else
7045 {
7046 item->di_tv.v_type = VAR_STRING;
7047 item->di_tv.vval.v_string = vim_strsave(str);
7048 }
7049 if (dict_add(d, item) == FAIL)
7050 {
7051 dictitem_free(item);
7052 return FAIL;
7053 }
7054 return OK;
7055}
7056
7057/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007058 * Add a list entry to dictionary "d".
7059 * Returns FAIL when out of memory and when key already exists.
7060 */
7061 int
7062dict_add_list(d, key, list)
7063 dict_T *d;
7064 char *key;
7065 list_T *list;
7066{
7067 dictitem_T *item;
7068
7069 item = dictitem_alloc((char_u *)key);
7070 if (item == NULL)
7071 return FAIL;
7072 item->di_tv.v_lock = 0;
7073 item->di_tv.v_type = VAR_LIST;
7074 item->di_tv.vval.v_list = list;
7075 if (dict_add(d, item) == FAIL)
7076 {
7077 dictitem_free(item);
7078 return FAIL;
7079 }
7080 return OK;
7081}
7082
7083/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007084 * Get the number of items in a Dictionary.
7085 */
7086 static long
7087dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007088 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007090 if (d == NULL)
7091 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007092 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007093}
7094
7095/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096 * Find item "key[len]" in Dictionary "d".
7097 * If "len" is negative use strlen(key).
7098 * Returns NULL when not found.
7099 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007100 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103 char_u *key;
7104 int len;
7105{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007106#define AKEYLEN 200
7107 char_u buf[AKEYLEN];
7108 char_u *akey;
7109 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007110 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007111
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 if (len < 0)
7113 akey = key;
7114 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007115 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116 tofree = akey = vim_strnsave(key, len);
7117 if (akey == NULL)
7118 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007119 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 else
7121 {
7122 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007123 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 akey = buf;
7125 }
7126
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007128 vim_free(tofree);
7129 if (HASHITEM_EMPTY(hi))
7130 return NULL;
7131 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132}
7133
7134/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007135 * Get a string item from a dictionary.
7136 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007137 * Returns NULL if the entry doesn't exist or out of memory.
7138 */
7139 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007140get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007141 dict_T *d;
7142 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007143 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007144{
7145 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007146 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007147
7148 di = dict_find(d, key, -1);
7149 if (di == NULL)
7150 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007151 s = get_tv_string(&di->di_tv);
7152 if (save && s != NULL)
7153 s = vim_strsave(s);
7154 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007155}
7156
7157/*
7158 * Get a number item from a dictionary.
7159 * Returns 0 if the entry doesn't exist or out of memory.
7160 */
7161 long
7162get_dict_number(d, key)
7163 dict_T *d;
7164 char_u *key;
7165{
7166 dictitem_T *di;
7167
7168 di = dict_find(d, key, -1);
7169 if (di == NULL)
7170 return 0;
7171 return get_tv_number(&di->di_tv);
7172}
7173
7174/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175 * Return an allocated string with the string representation of a Dictionary.
7176 * May return NULL.
7177 */
7178 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007179dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007180 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007181 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182{
7183 garray_T ga;
7184 int first = TRUE;
7185 char_u *tofree;
7186 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007191
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007192 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193 return NULL;
7194 ga_init2(&ga, (int)sizeof(char), 80);
7195 ga_append(&ga, '{');
7196
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007197 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007199 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 --todo;
7203
7204 if (first)
7205 first = FALSE;
7206 else
7207 ga_concat(&ga, (char_u *)", ");
7208
7209 tofree = string_quote(hi->hi_key, FALSE);
7210 if (tofree != NULL)
7211 {
7212 ga_concat(&ga, tofree);
7213 vim_free(tofree);
7214 }
7215 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007216 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007217 if (s != NULL)
7218 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007220 if (s == NULL)
7221 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007224 if (todo > 0)
7225 {
7226 vim_free(ga.ga_data);
7227 return NULL;
7228 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229
7230 ga_append(&ga, '}');
7231 ga_append(&ga, NUL);
7232 return (char_u *)ga.ga_data;
7233}
7234
7235/*
7236 * Allocate a variable for a Dictionary and fill it from "*arg".
7237 * Return OK or FAIL. Returns NOTDONE for {expr}.
7238 */
7239 static int
7240get_dict_tv(arg, rettv, evaluate)
7241 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007242 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243 int evaluate;
7244{
Bram Moolenaar33570922005-01-25 22:26:29 +00007245 dict_T *d = NULL;
7246 typval_T tvkey;
7247 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007248 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007251 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252
7253 /*
7254 * First check if it's not a curly-braces thing: {expr}.
7255 * Must do this without evaluating, otherwise a function may be called
7256 * twice. Unfortunately this means we need to call eval1() twice for the
7257 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007258 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007260 if (*start != '}')
7261 {
7262 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7263 return FAIL;
7264 if (*start == '}')
7265 return NOTDONE;
7266 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267
7268 if (evaluate)
7269 {
7270 d = dict_alloc();
7271 if (d == NULL)
7272 return FAIL;
7273 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 tvkey.v_type = VAR_UNKNOWN;
7275 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276
7277 *arg = skipwhite(*arg + 1);
7278 while (**arg != '}' && **arg != NUL)
7279 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 goto failret;
7282 if (**arg != ':')
7283 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007284 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286 goto failret;
7287 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007288 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007290 key = get_tv_string_buf_chk(&tvkey, buf);
7291 if (key == NULL || *key == NUL)
7292 {
7293 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7294 if (key != NULL)
7295 EMSG(_(e_emptykey));
7296 clear_tv(&tvkey);
7297 goto failret;
7298 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300
7301 *arg = skipwhite(*arg + 1);
7302 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7303 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007304 if (evaluate)
7305 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306 goto failret;
7307 }
7308 if (evaluate)
7309 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 if (item != NULL)
7312 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007313 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315 clear_tv(&tv);
7316 goto failret;
7317 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 item = dictitem_alloc(key);
7319 clear_tv(&tvkey);
7320 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007323 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 if (dict_add(d, item) == FAIL)
7325 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 }
7327 }
7328
7329 if (**arg == '}')
7330 break;
7331 if (**arg != ',')
7332 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007333 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007334 goto failret;
7335 }
7336 *arg = skipwhite(*arg + 1);
7337 }
7338
7339 if (**arg != '}')
7340 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007341 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342failret:
7343 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007344 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345 return FAIL;
7346 }
7347
7348 *arg = skipwhite(*arg + 1);
7349 if (evaluate)
7350 {
7351 rettv->v_type = VAR_DICT;
7352 rettv->vval.v_dict = d;
7353 ++d->dv_refcount;
7354 }
7355
7356 return OK;
7357}
7358
Bram Moolenaar8c711452005-01-14 21:53:12 +00007359/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007360 * Return a string with the string representation of a variable.
7361 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007362 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007363 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007364 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007365 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007366 */
7367 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007368echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007370 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007371 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007372 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007373{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 static int recurse = 0;
7375 char_u *r = NULL;
7376
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007379 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 *tofree = NULL;
7381 return NULL;
7382 }
7383 ++recurse;
7384
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007385 switch (tv->v_type)
7386 {
7387 case VAR_FUNC:
7388 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389 r = tv->vval.v_string;
7390 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007391
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007392 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007393 if (tv->vval.v_list == NULL)
7394 {
7395 *tofree = NULL;
7396 r = NULL;
7397 }
7398 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7399 {
7400 *tofree = NULL;
7401 r = (char_u *)"[...]";
7402 }
7403 else
7404 {
7405 tv->vval.v_list->lv_copyID = copyID;
7406 *tofree = list2string(tv, copyID);
7407 r = *tofree;
7408 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007410
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007412 if (tv->vval.v_dict == NULL)
7413 {
7414 *tofree = NULL;
7415 r = NULL;
7416 }
7417 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7418 {
7419 *tofree = NULL;
7420 r = (char_u *)"{...}";
7421 }
7422 else
7423 {
7424 tv->vval.v_dict->dv_copyID = copyID;
7425 *tofree = dict2string(tv, copyID);
7426 r = *tofree;
7427 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007429
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007430 case VAR_STRING:
7431 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432 *tofree = NULL;
7433 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007434 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007435
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007436#ifdef FEAT_FLOAT
7437 case VAR_FLOAT:
7438 *tofree = NULL;
7439 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7440 r = numbuf;
7441 break;
7442#endif
7443
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007444 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007445 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007446 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007448
7449 --recurse;
7450 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007451}
7452
7453/*
7454 * Return a string with the string representation of a variable.
7455 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7456 * "numbuf" is used for a number.
7457 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007458 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007459 */
7460 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007461tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007462 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007463 char_u **tofree;
7464 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007465 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007466{
7467 switch (tv->v_type)
7468 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007469 case VAR_FUNC:
7470 *tofree = string_quote(tv->vval.v_string, TRUE);
7471 return *tofree;
7472 case VAR_STRING:
7473 *tofree = string_quote(tv->vval.v_string, FALSE);
7474 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007475#ifdef FEAT_FLOAT
7476 case VAR_FLOAT:
7477 *tofree = NULL;
7478 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7479 return numbuf;
7480#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007481 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007482 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007483 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007484 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007485 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007486 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007487 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007488 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007489}
7490
7491/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 * Return string "str" in ' quotes, doubling ' characters.
7493 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007495 */
7496 static char_u *
7497string_quote(str, function)
7498 char_u *str;
7499 int function;
7500{
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007502 char_u *p, *r, *s;
7503
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 len = (function ? 13 : 3);
7505 if (str != NULL)
7506 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007507 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007508 for (p = str; *p != NUL; mb_ptr_adv(p))
7509 if (*p == '\'')
7510 ++len;
7511 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007512 s = r = alloc(len);
7513 if (r != NULL)
7514 {
7515 if (function)
7516 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007518 r += 10;
7519 }
7520 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 if (str != NULL)
7523 for (p = str; *p != NUL; )
7524 {
7525 if (*p == '\'')
7526 *r++ = '\'';
7527 MB_COPY_CHAR(p, r);
7528 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007530 if (function)
7531 *r++ = ')';
7532 *r++ = NUL;
7533 }
7534 return s;
7535}
7536
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007537#ifdef FEAT_FLOAT
7538/*
7539 * Convert the string "text" to a floating point number.
7540 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7541 * this always uses a decimal point.
7542 * Returns the length of the text that was consumed.
7543 */
7544 static int
7545string2float(text, value)
7546 char_u *text;
7547 float_T *value; /* result stored here */
7548{
7549 char *s = (char *)text;
7550 float_T f;
7551
7552 f = strtod(s, &s);
7553 *value = f;
7554 return (int)((char_u *)s - text);
7555}
7556#endif
7557
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 * Get the value of an environment variable.
7560 * "arg" is pointing to the '$'. It is advanced to after the name.
7561 * If the environment variable was not set, silently assume it is empty.
7562 * Always return OK.
7563 */
7564 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007565get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 int evaluate;
7569{
7570 char_u *string = NULL;
7571 int len;
7572 int cc;
7573 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007574 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575
7576 ++*arg;
7577 name = *arg;
7578 len = get_env_len(arg);
7579 if (evaluate)
7580 {
7581 if (len != 0)
7582 {
7583 cc = name[len];
7584 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007585 /* first try vim_getenv(), fast for normal environment vars */
7586 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007588 {
7589 if (!mustfree)
7590 string = vim_strsave(string);
7591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 else
7593 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007594 if (mustfree)
7595 vim_free(string);
7596
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 /* next try expanding things like $VIM and ${HOME} */
7598 string = expand_env_save(name - 1);
7599 if (string != NULL && *string == '$')
7600 {
7601 vim_free(string);
7602 string = NULL;
7603 }
7604 }
7605 name[len] = cc;
7606 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007607 rettv->v_type = VAR_STRING;
7608 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 }
7610
7611 return OK;
7612}
7613
7614/*
7615 * Array with names and number of arguments of all internal functions
7616 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7617 */
7618static struct fst
7619{
7620 char *f_name; /* function name */
7621 char f_min_argc; /* minimal number of arguments */
7622 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007623 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007624 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625} functions[] =
7626{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007627#ifdef FEAT_FLOAT
7628 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007629 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007630#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007631 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {"append", 2, 2, f_append},
7633 {"argc", 0, 0, f_argc},
7634 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007635 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007636#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007637 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007638 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007639 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007642 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {"bufexists", 1, 1, f_bufexists},
7644 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7645 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7646 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7647 {"buflisted", 1, 1, f_buflisted},
7648 {"bufloaded", 1, 1, f_bufloaded},
7649 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007650 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {"bufwinnr", 1, 1, f_bufwinnr},
7652 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007653 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007654 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007655#ifdef FEAT_FLOAT
7656 {"ceil", 1, 1, f_ceil},
7657#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007658 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 {"char2nr", 1, 1, f_char2nr},
7660 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007661 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007663#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007664 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007665 {"complete_add", 1, 1, f_complete_add},
7666 {"complete_check", 0, 0, f_complete_check},
7667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007669 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007670#ifdef FEAT_FLOAT
7671 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007672 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007673#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007676 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007677 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 {"delete", 1, 1, f_delete},
7679 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007680 {"diff_filler", 1, 1, f_diff_filler},
7681 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007682 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007684 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 {"eventhandler", 0, 0, f_eventhandler},
7686 {"executable", 1, 1, f_executable},
7687 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007688#ifdef FEAT_FLOAT
7689 {"exp", 1, 1, f_exp},
7690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007692 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007693 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7695 {"filereadable", 1, 1, f_filereadable},
7696 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007697 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007698 {"finddir", 1, 3, f_finddir},
7699 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007700#ifdef FEAT_FLOAT
7701 {"float2nr", 1, 1, f_float2nr},
7702 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007703 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007704#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007705 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 {"fnamemodify", 2, 2, f_fnamemodify},
7707 {"foldclosed", 1, 1, f_foldclosed},
7708 {"foldclosedend", 1, 1, f_foldclosedend},
7709 {"foldlevel", 1, 1, f_foldlevel},
7710 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007711 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007713 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007714 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007715 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007716 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 {"getbufvar", 2, 2, f_getbufvar},
7718 {"getchar", 0, 1, f_getchar},
7719 {"getcharmod", 0, 0, f_getcharmod},
7720 {"getcmdline", 0, 0, f_getcmdline},
7721 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007722 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007724 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007725 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {"getfsize", 1, 1, f_getfsize},
7727 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007728 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007729 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007730 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007731 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007732 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007733 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007734 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007735 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007737 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007738 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {"getwinposx", 0, 0, f_getwinposx},
7740 {"getwinposy", 0, 0, f_getwinposy},
7741 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007742 {"glob", 1, 2, f_glob},
7743 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007745 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007746 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007747 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7749 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7750 {"histadd", 2, 2, f_histadd},
7751 {"histdel", 1, 2, f_histdel},
7752 {"histget", 1, 2, f_histget},
7753 {"histnr", 1, 1, f_histnr},
7754 {"hlID", 1, 1, f_hlID},
7755 {"hlexists", 1, 1, f_hlexists},
7756 {"hostname", 0, 0, f_hostname},
7757 {"iconv", 3, 3, f_iconv},
7758 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007760 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007762 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 {"inputrestore", 0, 0, f_inputrestore},
7764 {"inputsave", 0, 0, f_inputsave},
7765 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007766 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007768 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007769 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007770 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007773 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 {"libcall", 3, 3, f_libcall},
7775 {"libcallnr", 3, 3, f_libcallnr},
7776 {"line", 1, 1, f_line},
7777 {"line2byte", 1, 1, f_line2byte},
7778 {"lispindent", 1, 1, f_lispindent},
7779 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007780#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007781 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007782 {"log10", 1, 1, f_log10},
7783#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007784 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007785 {"maparg", 1, 3, f_maparg},
7786 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007787 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007788 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007789 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007790 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007791 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007792 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007793 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007794 {"max", 1, 1, f_max},
7795 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007796#ifdef vim_mkdir
7797 {"mkdir", 1, 3, f_mkdir},
7798#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007799 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007800#ifdef FEAT_MZSCHEME
7801 {"mzeval", 1, 1, f_mzeval},
7802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {"nextnonblank", 1, 1, f_nextnonblank},
7804 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007805 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007806#ifdef FEAT_FLOAT
7807 {"pow", 2, 2, f_pow},
7808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007810 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007811 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007812 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007813 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007814 {"reltime", 0, 2, f_reltime},
7815 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {"remote_expr", 2, 3, f_remote_expr},
7817 {"remote_foreground", 1, 1, f_remote_foreground},
7818 {"remote_peek", 1, 2, f_remote_peek},
7819 {"remote_read", 1, 1, f_remote_read},
7820 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007821 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007823 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007825 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007826#ifdef FEAT_FLOAT
7827 {"round", 1, 1, f_round},
7828#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007829 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007830 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007831 {"searchpair", 3, 7, f_searchpair},
7832 {"searchpairpos", 3, 7, f_searchpairpos},
7833 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {"server2client", 2, 2, f_server2client},
7835 {"serverlist", 0, 0, f_serverlist},
7836 {"setbufvar", 3, 3, f_setbufvar},
7837 {"setcmdpos", 1, 1, f_setcmdpos},
7838 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007839 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007840 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007841 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007842 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007844 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007845 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007847 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007849#ifdef FEAT_FLOAT
7850 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007851 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007852#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007853 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007854 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007855 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007856 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007857 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007858#ifdef FEAT_FLOAT
7859 {"sqrt", 1, 1, f_sqrt},
7860 {"str2float", 1, 1, f_str2float},
7861#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007862 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007863 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007864 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865#ifdef HAVE_STRFTIME
7866 {"strftime", 1, 2, f_strftime},
7867#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007868 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007869 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {"strlen", 1, 1, f_strlen},
7871 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007872 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007874 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {"submatch", 1, 1, f_submatch},
7876 {"substitute", 4, 4, f_substitute},
7877 {"synID", 3, 3, f_synID},
7878 {"synIDattr", 2, 3, f_synIDattr},
7879 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007880 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007881 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007882 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007883 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007884 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007885 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007886 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007887 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007888#ifdef FEAT_FLOAT
7889 {"tan", 1, 1, f_tan},
7890 {"tanh", 1, 1, f_tanh},
7891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007893 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"tolower", 1, 1, f_tolower},
7895 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007896 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007897#ifdef FEAT_FLOAT
7898 {"trunc", 1, 1, f_trunc},
7899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007901 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007902 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007903 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"virtcol", 1, 1, f_virtcol},
7905 {"visualmode", 0, 1, f_visualmode},
7906 {"winbufnr", 1, 1, f_winbufnr},
7907 {"wincol", 0, 0, f_wincol},
7908 {"winheight", 1, 1, f_winheight},
7909 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007910 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007912 {"winrestview", 1, 1, f_winrestview},
7913 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007915 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916};
7917
7918#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7919
7920/*
7921 * Function given to ExpandGeneric() to obtain the list of internal
7922 * or user defined function names.
7923 */
7924 char_u *
7925get_function_name(xp, idx)
7926 expand_T *xp;
7927 int idx;
7928{
7929 static int intidx = -1;
7930 char_u *name;
7931
7932 if (idx == 0)
7933 intidx = -1;
7934 if (intidx < 0)
7935 {
7936 name = get_user_func_name(xp, idx);
7937 if (name != NULL)
7938 return name;
7939 }
7940 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7941 {
7942 STRCPY(IObuff, functions[intidx].f_name);
7943 STRCAT(IObuff, "(");
7944 if (functions[intidx].f_max_argc == 0)
7945 STRCAT(IObuff, ")");
7946 return IObuff;
7947 }
7948
7949 return NULL;
7950}
7951
7952/*
7953 * Function given to ExpandGeneric() to obtain the list of internal or
7954 * user defined variable or function names.
7955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 char_u *
7957get_expr_name(xp, idx)
7958 expand_T *xp;
7959 int idx;
7960{
7961 static int intidx = -1;
7962 char_u *name;
7963
7964 if (idx == 0)
7965 intidx = -1;
7966 if (intidx < 0)
7967 {
7968 name = get_function_name(xp, idx);
7969 if (name != NULL)
7970 return name;
7971 }
7972 return get_user_var_name(xp, ++intidx);
7973}
7974
7975#endif /* FEAT_CMDL_COMPL */
7976
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007977#if defined(EBCDIC) || defined(PROTO)
7978/*
7979 * Compare struct fst by function name.
7980 */
7981 static int
7982compare_func_name(s1, s2)
7983 const void *s1;
7984 const void *s2;
7985{
7986 struct fst *p1 = (struct fst *)s1;
7987 struct fst *p2 = (struct fst *)s2;
7988
7989 return STRCMP(p1->f_name, p2->f_name);
7990}
7991
7992/*
7993 * Sort the function table by function name.
7994 * The sorting of the table above is ASCII dependant.
7995 * On machines using EBCDIC we have to sort it.
7996 */
7997 static void
7998sortFunctions()
7999{
8000 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8001
8002 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8003}
8004#endif
8005
8006
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007/*
8008 * Find internal function in table above.
8009 * Return index, or -1 if not found
8010 */
8011 static int
8012find_internal_func(name)
8013 char_u *name; /* name of the function */
8014{
8015 int first = 0;
8016 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8017 int cmp;
8018 int x;
8019
8020 /*
8021 * Find the function name in the table. Binary search.
8022 */
8023 while (first <= last)
8024 {
8025 x = first + ((unsigned)(last - first) >> 1);
8026 cmp = STRCMP(name, functions[x].f_name);
8027 if (cmp < 0)
8028 last = x - 1;
8029 else if (cmp > 0)
8030 first = x + 1;
8031 else
8032 return x;
8033 }
8034 return -1;
8035}
8036
8037/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008038 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8039 * name it contains, otherwise return "name".
8040 */
8041 static char_u *
8042deref_func_name(name, lenp)
8043 char_u *name;
8044 int *lenp;
8045{
Bram Moolenaar33570922005-01-25 22:26:29 +00008046 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008047 int cc;
8048
8049 cc = name[*lenp];
8050 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008051 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008052 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008054 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008055 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008056 {
8057 *lenp = 0;
8058 return (char_u *)""; /* just in case */
8059 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008060 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008061 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008062 }
8063
8064 return name;
8065}
8066
8067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 * Allocate a variable for the result of a function.
8069 * Return OK or FAIL.
8070 */
8071 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008072get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8073 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 char_u *name; /* name of the function */
8075 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 char_u **arg; /* argument, pointing to the '(' */
8078 linenr_T firstline; /* first line of range */
8079 linenr_T lastline; /* last line of range */
8080 int *doesrange; /* return: function handled range */
8081 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008082 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
8084 char_u *argp;
8085 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008086 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 int argcount = 0; /* number of arguments found */
8088
8089 /*
8090 * Get the arguments.
8091 */
8092 argp = *arg;
8093 while (argcount < MAX_FUNC_ARGS)
8094 {
8095 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8096 if (*argp == ')' || *argp == ',' || *argp == NUL)
8097 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8099 {
8100 ret = FAIL;
8101 break;
8102 }
8103 ++argcount;
8104 if (*argp != ',')
8105 break;
8106 }
8107 if (*argp == ')')
8108 ++argp;
8109 else
8110 ret = FAIL;
8111
8112 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008113 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008114 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008116 {
8117 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008118 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008119 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008120 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122
8123 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008124 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125
8126 *arg = skipwhite(argp);
8127 return ret;
8128}
8129
8130
8131/*
8132 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008133 * Return OK when the function can't be called, FAIL otherwise.
8134 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 */
8136 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008137call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008138 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008139 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008141 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008143 typval_T *argvars; /* vars for arguments, must have "argcount"
8144 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 linenr_T firstline; /* first line of range */
8146 linenr_T lastline; /* last line of range */
8147 int *doesrange; /* return: function handled range */
8148 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008149 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150{
8151 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152#define ERROR_UNKNOWN 0
8153#define ERROR_TOOMANY 1
8154#define ERROR_TOOFEW 2
8155#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008156#define ERROR_DICT 4
8157#define ERROR_NONE 5
8158#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 int error = ERROR_NONE;
8160 int i;
8161 int llen;
8162 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163#define FLEN_FIXED 40
8164 char_u fname_buf[FLEN_FIXED + 1];
8165 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008166 char_u *name;
8167
8168 /* Make a copy of the name, if it comes from a funcref variable it could
8169 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008170 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008171 if (name == NULL)
8172 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173
8174 /*
8175 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8176 * Change <SNR>123_name() to K_SNR 123_name().
8177 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 llen = eval_fname_script(name);
8180 if (llen > 0)
8181 {
8182 fname_buf[0] = K_SPECIAL;
8183 fname_buf[1] = KS_EXTRA;
8184 fname_buf[2] = (int)KE_SNR;
8185 i = 3;
8186 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8187 {
8188 if (current_SID <= 0)
8189 error = ERROR_SCRIPT;
8190 else
8191 {
8192 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8193 i = (int)STRLEN(fname_buf);
8194 }
8195 }
8196 if (i + STRLEN(name + llen) < FLEN_FIXED)
8197 {
8198 STRCPY(fname_buf + i, name + llen);
8199 fname = fname_buf;
8200 }
8201 else
8202 {
8203 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8204 if (fname == NULL)
8205 error = ERROR_OTHER;
8206 else
8207 {
8208 mch_memmove(fname, fname_buf, (size_t)i);
8209 STRCPY(fname + i, name + llen);
8210 }
8211 }
8212 }
8213 else
8214 fname = name;
8215
8216 *doesrange = FALSE;
8217
8218
8219 /* execute the function if no errors detected and executing */
8220 if (evaluate && error == ERROR_NONE)
8221 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008222 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8223 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 error = ERROR_UNKNOWN;
8225
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008226 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {
8228 /*
8229 * User defined function.
8230 */
8231 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008232
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008234 /* Trigger FuncUndefined event, may load the function. */
8235 if (fp == NULL
8236 && apply_autocmds(EVENT_FUNCUNDEFINED,
8237 fname, fname, TRUE, NULL)
8238 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008240 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 fp = find_func(fname);
8242 }
8243#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008244 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008245 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008246 {
8247 /* loaded a package, search for the function again */
8248 fp = find_func(fname);
8249 }
8250
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 if (fp != NULL)
8252 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008253 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008255 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008257 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008259 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008260 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 else
8262 {
8263 /*
8264 * Call the user function.
8265 * Save and restore search patterns, script variables and
8266 * redo buffer.
8267 */
8268 save_search_patterns();
8269 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008270 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008271 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008272 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008273 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8274 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8275 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008276 /* Function was unreferenced while being used, free it
8277 * now. */
8278 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 restoreRedobuff();
8280 restore_search_patterns();
8281 error = ERROR_NONE;
8282 }
8283 }
8284 }
8285 else
8286 {
8287 /*
8288 * Find the function name in the table, call its implementation.
8289 */
8290 i = find_internal_func(fname);
8291 if (i >= 0)
8292 {
8293 if (argcount < functions[i].f_min_argc)
8294 error = ERROR_TOOFEW;
8295 else if (argcount > functions[i].f_max_argc)
8296 error = ERROR_TOOMANY;
8297 else
8298 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008299 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008300 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 error = ERROR_NONE;
8302 }
8303 }
8304 }
8305 /*
8306 * The function call (or "FuncUndefined" autocommand sequence) might
8307 * have been aborted by an error, an interrupt, or an explicitly thrown
8308 * exception that has not been caught so far. This situation can be
8309 * tested for by calling aborting(). For an error in an internal
8310 * function or for the "E132" error in call_user_func(), however, the
8311 * throw point at which the "force_abort" flag (temporarily reset by
8312 * emsg()) is normally updated has not been reached yet. We need to
8313 * update that flag first to make aborting() reliable.
8314 */
8315 update_force_abort();
8316 }
8317 if (error == ERROR_NONE)
8318 ret = OK;
8319
8320 /*
8321 * Report an error unless the argument evaluation or function call has been
8322 * cancelled due to an aborting error, an interrupt, or an exception.
8323 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008324 if (!aborting())
8325 {
8326 switch (error)
8327 {
8328 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008329 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008330 break;
8331 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008332 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008333 break;
8334 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008335 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008336 name);
8337 break;
8338 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008339 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008340 name);
8341 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008342 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008343 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008344 name);
8345 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008346 }
8347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 if (fname != name && fname != fname_buf)
8350 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008351 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352
8353 return ret;
8354}
8355
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008356/*
8357 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008358 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008359 */
8360 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008361emsg_funcname(ermsg, name)
8362 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008363 char_u *name;
8364{
8365 char_u *p;
8366
8367 if (*name == K_SPECIAL)
8368 p = concat_str((char_u *)"<SNR>", name + 3);
8369 else
8370 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008371 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008372 if (p != name)
8373 vim_free(p);
8374}
8375
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008376/*
8377 * Return TRUE for a non-zero Number and a non-empty String.
8378 */
8379 static int
8380non_zero_arg(argvars)
8381 typval_T *argvars;
8382{
8383 return ((argvars[0].v_type == VAR_NUMBER
8384 && argvars[0].vval.v_number != 0)
8385 || (argvars[0].v_type == VAR_STRING
8386 && argvars[0].vval.v_string != NULL
8387 && *argvars[0].vval.v_string != NUL));
8388}
8389
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390/*********************************************
8391 * Implementation of the built-in functions
8392 */
8393
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008394#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008395static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8396
8397/*
8398 * Get the float value of "argvars[0]" into "f".
8399 * Returns FAIL when the argument is not a Number or Float.
8400 */
8401 static int
8402get_float_arg(argvars, f)
8403 typval_T *argvars;
8404 float_T *f;
8405{
8406 if (argvars[0].v_type == VAR_FLOAT)
8407 {
8408 *f = argvars[0].vval.v_float;
8409 return OK;
8410 }
8411 if (argvars[0].v_type == VAR_NUMBER)
8412 {
8413 *f = (float_T)argvars[0].vval.v_number;
8414 return OK;
8415 }
8416 EMSG(_("E808: Number or Float required"));
8417 return FAIL;
8418}
8419
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008420/*
8421 * "abs(expr)" function
8422 */
8423 static void
8424f_abs(argvars, rettv)
8425 typval_T *argvars;
8426 typval_T *rettv;
8427{
8428 if (argvars[0].v_type == VAR_FLOAT)
8429 {
8430 rettv->v_type = VAR_FLOAT;
8431 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8432 }
8433 else
8434 {
8435 varnumber_T n;
8436 int error = FALSE;
8437
8438 n = get_tv_number_chk(&argvars[0], &error);
8439 if (error)
8440 rettv->vval.v_number = -1;
8441 else if (n > 0)
8442 rettv->vval.v_number = n;
8443 else
8444 rettv->vval.v_number = -n;
8445 }
8446}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008447
8448/*
8449 * "acos()" function
8450 */
8451 static void
8452f_acos(argvars, rettv)
8453 typval_T *argvars;
8454 typval_T *rettv;
8455{
8456 float_T f;
8457
8458 rettv->v_type = VAR_FLOAT;
8459 if (get_float_arg(argvars, &f) == OK)
8460 rettv->vval.v_float = acos(f);
8461 else
8462 rettv->vval.v_float = 0.0;
8463}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008464#endif
8465
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008467 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 */
8469 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008470f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008471 typval_T *argvars;
8472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473{
Bram Moolenaar33570922005-01-25 22:26:29 +00008474 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008477 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008479 if ((l = argvars[0].vval.v_list) != NULL
8480 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8481 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008482 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008483 }
8484 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008485 EMSG(_(e_listreq));
8486}
8487
8488/*
8489 * "append(lnum, string/list)" function
8490 */
8491 static void
8492f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008493 typval_T *argvars;
8494 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008495{
8496 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008497 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008498 list_T *l = NULL;
8499 listitem_T *li = NULL;
8500 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008501 long added = 0;
8502
Bram Moolenaar0d660222005-01-07 21:51:51 +00008503 lnum = get_tv_lnum(argvars);
8504 if (lnum >= 0
8505 && lnum <= curbuf->b_ml.ml_line_count
8506 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008507 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008508 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008509 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008510 l = argvars[1].vval.v_list;
8511 if (l == NULL)
8512 return;
8513 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008514 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008515 for (;;)
8516 {
8517 if (l == NULL)
8518 tv = &argvars[1]; /* append a string */
8519 else if (li == NULL)
8520 break; /* end of list */
8521 else
8522 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008523 line = get_tv_string_chk(tv);
8524 if (line == NULL) /* type error */
8525 {
8526 rettv->vval.v_number = 1; /* Failed */
8527 break;
8528 }
8529 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008530 ++added;
8531 if (l == NULL)
8532 break;
8533 li = li->li_next;
8534 }
8535
8536 appended_lines_mark(lnum, added);
8537 if (curwin->w_cursor.lnum > lnum)
8538 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008540 else
8541 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542}
8543
8544/*
8545 * "argc()" function
8546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008548f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008549 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008552 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553}
8554
8555/*
8556 * "argidx()" function
8557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008559f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008560 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008563 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564}
8565
8566/*
8567 * "argv(nr)" function
8568 */
8569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008570f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 typval_T *argvars;
8572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
8574 int idx;
8575
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008576 if (argvars[0].v_type != VAR_UNKNOWN)
8577 {
8578 idx = get_tv_number_chk(&argvars[0], NULL);
8579 if (idx >= 0 && idx < ARGCOUNT)
8580 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8581 else
8582 rettv->vval.v_string = NULL;
8583 rettv->v_type = VAR_STRING;
8584 }
8585 else if (rettv_list_alloc(rettv) == OK)
8586 for (idx = 0; idx < ARGCOUNT; ++idx)
8587 list_append_string(rettv->vval.v_list,
8588 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589}
8590
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008591#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008592/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008593 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008594 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008595 static void
8596f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008597 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008598 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008599{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008600 float_T f;
8601
8602 rettv->v_type = VAR_FLOAT;
8603 if (get_float_arg(argvars, &f) == OK)
8604 rettv->vval.v_float = asin(f);
8605 else
8606 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008607}
8608
8609/*
8610 * "atan()" function
8611 */
8612 static void
8613f_atan(argvars, rettv)
8614 typval_T *argvars;
8615 typval_T *rettv;
8616{
8617 float_T f;
8618
8619 rettv->v_type = VAR_FLOAT;
8620 if (get_float_arg(argvars, &f) == OK)
8621 rettv->vval.v_float = atan(f);
8622 else
8623 rettv->vval.v_float = 0.0;
8624}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008625
8626/*
8627 * "atan2()" function
8628 */
8629 static void
8630f_atan2(argvars, rettv)
8631 typval_T *argvars;
8632 typval_T *rettv;
8633{
8634 float_T fx, fy;
8635
8636 rettv->v_type = VAR_FLOAT;
8637 if (get_float_arg(argvars, &fx) == OK
8638 && get_float_arg(&argvars[1], &fy) == OK)
8639 rettv->vval.v_float = atan2(fx, fy);
8640 else
8641 rettv->vval.v_float = 0.0;
8642}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008643#endif
8644
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645/*
8646 * "browse(save, title, initdir, default)" function
8647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008650 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652{
8653#ifdef FEAT_BROWSE
8654 int save;
8655 char_u *title;
8656 char_u *initdir;
8657 char_u *defname;
8658 char_u buf[NUMBUFLEN];
8659 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008660 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008662 save = get_tv_number_chk(&argvars[0], &error);
8663 title = get_tv_string_chk(&argvars[1]);
8664 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8665 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667 if (error || title == NULL || initdir == NULL || defname == NULL)
8668 rettv->vval.v_string = NULL;
8669 else
8670 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008671 do_browse(save ? BROWSE_SAVE : 0,
8672 title, defname, NULL, initdir, NULL, curbuf);
8673#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008675#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008676 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008677}
8678
8679/*
8680 * "browsedir(title, initdir)" function
8681 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008683f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008684 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008686{
8687#ifdef FEAT_BROWSE
8688 char_u *title;
8689 char_u *initdir;
8690 char_u buf[NUMBUFLEN];
8691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008692 title = get_tv_string_chk(&argvars[0]);
8693 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008695 if (title == NULL || initdir == NULL)
8696 rettv->vval.v_string = NULL;
8697 else
8698 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008699 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008701 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704}
8705
Bram Moolenaar33570922005-01-25 22:26:29 +00008706static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008707
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708/*
8709 * Find a buffer by number or exact name.
8710 */
8711 static buf_T *
8712find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008713 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714{
8715 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008717 if (avar->v_type == VAR_NUMBER)
8718 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008719 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008721 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008722 if (buf == NULL)
8723 {
8724 /* No full path name match, try a match with a URL or a "nofile"
8725 * buffer, these don't use the full path. */
8726 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8727 if (buf->b_fname != NULL
8728 && (path_with_url(buf->b_fname)
8729#ifdef FEAT_QUICKFIX
8730 || bt_nofile(buf)
8731#endif
8732 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008733 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008734 break;
8735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 }
8737 return buf;
8738}
8739
8740/*
8741 * "bufexists(expr)" function
8742 */
8743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008745 typval_T *argvars;
8746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008748 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749}
8750
8751/*
8752 * "buflisted(expr)" function
8753 */
8754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008756 typval_T *argvars;
8757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758{
8759 buf_T *buf;
8760
8761 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763}
8764
8765/*
8766 * "bufloaded(expr)" function
8767 */
8768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008770 typval_T *argvars;
8771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772{
8773 buf_T *buf;
8774
8775 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008776 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777}
8778
Bram Moolenaar33570922005-01-25 22:26:29 +00008779static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008780
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781/*
8782 * Get buffer by number or pattern.
8783 */
8784 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008786 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 int save_magic;
8790 char_u *save_cpo;
8791 buf_T *buf;
8792
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008793 if (tv->v_type == VAR_NUMBER)
8794 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008795 if (tv->v_type != VAR_STRING)
8796 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 if (name == NULL || *name == NUL)
8798 return curbuf;
8799 if (name[0] == '$' && name[1] == NUL)
8800 return lastbuf;
8801
8802 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8803 save_magic = p_magic;
8804 p_magic = TRUE;
8805 save_cpo = p_cpo;
8806 p_cpo = (char_u *)"";
8807
8808 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8809 TRUE, FALSE));
8810
8811 p_magic = save_magic;
8812 p_cpo = save_cpo;
8813
8814 /* If not found, try expanding the name, like done for bufexists(). */
8815 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008816 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817
8818 return buf;
8819}
8820
8821/*
8822 * "bufname(expr)" function
8823 */
8824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008826 typval_T *argvars;
8827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828{
8829 buf_T *buf;
8830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008831 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833 buf = get_buf_tv(&argvars[0]);
8834 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 --emsg_off;
8840}
8841
8842/*
8843 * "bufnr(expr)" function
8844 */
8845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 typval_T *argvars;
8848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
8850 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008851 int error = FALSE;
8852 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008854 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008857 --emsg_off;
8858
8859 /* If the buffer isn't found and the second argument is not zero create a
8860 * new buffer. */
8861 if (buf == NULL
8862 && argvars[1].v_type != VAR_UNKNOWN
8863 && get_tv_number_chk(&argvars[1], &error) != 0
8864 && !error
8865 && (name = get_tv_string_chk(&argvars[0])) != NULL
8866 && !error)
8867 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873}
8874
8875/*
8876 * "bufwinnr(nr)" function
8877 */
8878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008879f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 typval_T *argvars;
8881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882{
8883#ifdef FEAT_WINDOWS
8884 win_T *wp;
8885 int winnr = 0;
8886#endif
8887 buf_T *buf;
8888
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008889 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892#ifdef FEAT_WINDOWS
8893 for (wp = firstwin; wp; wp = wp->w_next)
8894 {
8895 ++winnr;
8896 if (wp->w_buffer == buf)
8897 break;
8898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008901 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902#endif
8903 --emsg_off;
8904}
8905
8906/*
8907 * "byte2line(byte)" function
8908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008910f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008911 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008912 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913{
8914#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916#else
8917 long boff = 0;
8918
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008919 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 (linenr_T)0, &boff);
8925#endif
8926}
8927
8928/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008929 * "byteidx()" function
8930 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008933 typval_T *argvars;
8934 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008935{
8936#ifdef FEAT_MBYTE
8937 char_u *t;
8938#endif
8939 char_u *str;
8940 long idx;
8941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008942 str = get_tv_string_chk(&argvars[0]);
8943 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008945 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008946 return;
8947
8948#ifdef FEAT_MBYTE
8949 t = str;
8950 for ( ; idx > 0; idx--)
8951 {
8952 if (*t == NUL) /* EOL reached */
8953 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008954 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008955 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008956 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008957#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008958 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008960#endif
8961}
8962
8963/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008964 * "call(func, arglist)" function
8965 */
8966 static void
8967f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 typval_T *argvars;
8969 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008970{
8971 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008972 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008973 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008974 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008975 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008977
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008978 if (argvars[1].v_type != VAR_LIST)
8979 {
8980 EMSG(_(e_listreq));
8981 return;
8982 }
8983 if (argvars[1].vval.v_list == NULL)
8984 return;
8985
8986 if (argvars[0].v_type == VAR_FUNC)
8987 func = argvars[0].vval.v_string;
8988 else
8989 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 if (*func == NUL)
8991 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008992
Bram Moolenaare9a41262005-01-15 22:18:47 +00008993 if (argvars[2].v_type != VAR_UNKNOWN)
8994 {
8995 if (argvars[2].v_type != VAR_DICT)
8996 {
8997 EMSG(_(e_dictreq));
8998 return;
8999 }
9000 selfdict = argvars[2].vval.v_dict;
9001 }
9002
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009003 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9004 item = item->li_next)
9005 {
9006 if (argc == MAX_FUNC_ARGS)
9007 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009008 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009009 break;
9010 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009011 /* Make a copy of each argument. This is needed to be able to set
9012 * v_lock to VAR_FIXED in the copy without changing the original list.
9013 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009014 copy_tv(&item->li_tv, &argv[argc++]);
9015 }
9016
9017 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009018 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009019 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9020 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009021
9022 /* Free the arguments. */
9023 while (argc > 0)
9024 clear_tv(&argv[--argc]);
9025}
9026
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009027#ifdef FEAT_FLOAT
9028/*
9029 * "ceil({float})" function
9030 */
9031 static void
9032f_ceil(argvars, rettv)
9033 typval_T *argvars;
9034 typval_T *rettv;
9035{
9036 float_T f;
9037
9038 rettv->v_type = VAR_FLOAT;
9039 if (get_float_arg(argvars, &f) == OK)
9040 rettv->vval.v_float = ceil(f);
9041 else
9042 rettv->vval.v_float = 0.0;
9043}
9044#endif
9045
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009046/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009047 * "changenr()" function
9048 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009049 static void
9050f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009051 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009052 typval_T *rettv;
9053{
9054 rettv->vval.v_number = curbuf->b_u_seq_cur;
9055}
9056
9057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 * "char2nr(string)" function
9059 */
9060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *argvars;
9063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065#ifdef FEAT_MBYTE
9066 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 else
9069#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071}
9072
9073/*
9074 * "cindent(lnum)" function
9075 */
9076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 typval_T *argvars;
9079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081#ifdef FEAT_CINDENT
9082 pos_T pos;
9083 linenr_T lnum;
9084
9085 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9088 {
9089 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 curwin->w_cursor = pos;
9092 }
9093 else
9094#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009095 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096}
9097
9098/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009099 * "clearmatches()" function
9100 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009101 static void
9102f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009103 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009104 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009105{
9106#ifdef FEAT_SEARCH_EXTRA
9107 clear_matches(curwin);
9108#endif
9109}
9110
9111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 * "col(string)" function
9113 */
9114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009116 typval_T *argvars;
9117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
9119 colnr_T col = 0;
9120 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009121 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009123 fp = var2fpos(&argvars[0], FALSE, &fnum);
9124 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 {
9126 if (fp->col == MAXCOL)
9127 {
9128 /* '> can be MAXCOL, get the length of the line then */
9129 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009130 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 else
9132 col = MAXCOL;
9133 }
9134 else
9135 {
9136 col = fp->col + 1;
9137#ifdef FEAT_VIRTUALEDIT
9138 /* col(".") when the cursor is on the NUL at the end of the line
9139 * because of "coladd" can be seen as an extra column. */
9140 if (virtual_active() && fp == &curwin->w_cursor)
9141 {
9142 char_u *p = ml_get_cursor();
9143
9144 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9145 curwin->w_virtcol - curwin->w_cursor.coladd))
9146 {
9147# ifdef FEAT_MBYTE
9148 int l;
9149
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009150 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 col += l;
9152# else
9153 if (*p != NUL && p[1] == NUL)
9154 ++col;
9155# endif
9156 }
9157 }
9158#endif
9159 }
9160 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162}
9163
Bram Moolenaar572cb562005-08-05 21:35:02 +00009164#if defined(FEAT_INS_EXPAND)
9165/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009166 * "complete()" function
9167 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009168 static void
9169f_complete(argvars, rettv)
9170 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009171 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009172{
9173 int startcol;
9174
9175 if ((State & INSERT) == 0)
9176 {
9177 EMSG(_("E785: complete() can only be used in Insert mode"));
9178 return;
9179 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009180
9181 /* Check for undo allowed here, because if something was already inserted
9182 * the line was already saved for undo and this check isn't done. */
9183 if (!undo_allowed())
9184 return;
9185
Bram Moolenaarade00832006-03-10 21:46:58 +00009186 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9187 {
9188 EMSG(_(e_invarg));
9189 return;
9190 }
9191
9192 startcol = get_tv_number_chk(&argvars[0], NULL);
9193 if (startcol <= 0)
9194 return;
9195
9196 set_completion(startcol - 1, argvars[1].vval.v_list);
9197}
9198
9199/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009200 * "complete_add()" function
9201 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009202 static void
9203f_complete_add(argvars, rettv)
9204 typval_T *argvars;
9205 typval_T *rettv;
9206{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009207 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009208}
9209
9210/*
9211 * "complete_check()" function
9212 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009213 static void
9214f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009215 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009216 typval_T *rettv;
9217{
9218 int saved = RedrawingDisabled;
9219
9220 RedrawingDisabled = 0;
9221 ins_compl_check_keys(0);
9222 rettv->vval.v_number = compl_interrupted;
9223 RedrawingDisabled = saved;
9224}
9225#endif
9226
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227/*
9228 * "confirm(message, buttons[, default [, type]])" function
9229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009232 typval_T *argvars UNUSED;
9233 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
9235#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9236 char_u *message;
9237 char_u *buttons = NULL;
9238 char_u buf[NUMBUFLEN];
9239 char_u buf2[NUMBUFLEN];
9240 int def = 1;
9241 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242 char_u *typestr;
9243 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 message = get_tv_string_chk(&argvars[0]);
9246 if (message == NULL)
9247 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009248 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9251 if (buttons == NULL)
9252 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009253 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009256 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9259 if (typestr == NULL)
9260 error = TRUE;
9261 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009263 switch (TOUPPER_ASC(*typestr))
9264 {
9265 case 'E': type = VIM_ERROR; break;
9266 case 'Q': type = VIM_QUESTION; break;
9267 case 'I': type = VIM_INFO; break;
9268 case 'W': type = VIM_WARNING; break;
9269 case 'G': type = VIM_GENERIC; break;
9270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 }
9272 }
9273 }
9274 }
9275
9276 if (buttons == NULL || *buttons == NUL)
9277 buttons = (char_u *)_("&Ok");
9278
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009279 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009280 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282#endif
9283}
9284
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009285/*
9286 * "copy()" function
9287 */
9288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009289f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009290 typval_T *argvars;
9291 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009292{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009293 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009294}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009296#ifdef FEAT_FLOAT
9297/*
9298 * "cos()" function
9299 */
9300 static void
9301f_cos(argvars, rettv)
9302 typval_T *argvars;
9303 typval_T *rettv;
9304{
9305 float_T f;
9306
9307 rettv->v_type = VAR_FLOAT;
9308 if (get_float_arg(argvars, &f) == OK)
9309 rettv->vval.v_float = cos(f);
9310 else
9311 rettv->vval.v_float = 0.0;
9312}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009313
9314/*
9315 * "cosh()" function
9316 */
9317 static void
9318f_cosh(argvars, rettv)
9319 typval_T *argvars;
9320 typval_T *rettv;
9321{
9322 float_T f;
9323
9324 rettv->v_type = VAR_FLOAT;
9325 if (get_float_arg(argvars, &f) == OK)
9326 rettv->vval.v_float = cosh(f);
9327 else
9328 rettv->vval.v_float = 0.0;
9329}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009330#endif
9331
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009333 * "count()" function
9334 */
9335 static void
9336f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009337 typval_T *argvars;
9338 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009339{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009340 long n = 0;
9341 int ic = FALSE;
9342
Bram Moolenaare9a41262005-01-15 22:18:47 +00009343 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009344 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009345 listitem_T *li;
9346 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009347 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009348
Bram Moolenaare9a41262005-01-15 22:18:47 +00009349 if ((l = argvars[0].vval.v_list) != NULL)
9350 {
9351 li = l->lv_first;
9352 if (argvars[2].v_type != VAR_UNKNOWN)
9353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009354 int error = FALSE;
9355
9356 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009357 if (argvars[3].v_type != VAR_UNKNOWN)
9358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009359 idx = get_tv_number_chk(&argvars[3], &error);
9360 if (!error)
9361 {
9362 li = list_find(l, idx);
9363 if (li == NULL)
9364 EMSGN(_(e_listidx), idx);
9365 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009366 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009367 if (error)
9368 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009369 }
9370
9371 for ( ; li != NULL; li = li->li_next)
9372 if (tv_equal(&li->li_tv, &argvars[1], ic))
9373 ++n;
9374 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009375 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009376 else if (argvars[0].v_type == VAR_DICT)
9377 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009378 int todo;
9379 dict_T *d;
9380 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009381
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009382 if ((d = argvars[0].vval.v_dict) != NULL)
9383 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009384 int error = FALSE;
9385
Bram Moolenaare9a41262005-01-15 22:18:47 +00009386 if (argvars[2].v_type != VAR_UNKNOWN)
9387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009388 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009389 if (argvars[3].v_type != VAR_UNKNOWN)
9390 EMSG(_(e_invarg));
9391 }
9392
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009393 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009394 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009395 {
9396 if (!HASHITEM_EMPTY(hi))
9397 {
9398 --todo;
9399 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9400 ++n;
9401 }
9402 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009403 }
9404 }
9405 else
9406 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009407 rettv->vval.v_number = n;
9408}
9409
9410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9412 *
9413 * Checks the existence of a cscope connection.
9414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009417 typval_T *argvars UNUSED;
9418 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
9420#ifdef FEAT_CSCOPE
9421 int num = 0;
9422 char_u *dbpath = NULL;
9423 char_u *prepend = NULL;
9424 char_u buf[NUMBUFLEN];
9425
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009426 if (argvars[0].v_type != VAR_UNKNOWN
9427 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 num = (int)get_tv_number(&argvars[0]);
9430 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009431 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 }
9434
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436#endif
9437}
9438
9439/*
9440 * "cursor(lnum, col)" function
9441 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009442 * Moves the cursor to the specified line and column.
9443 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009446f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009447 typval_T *argvars;
9448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
9450 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009451#ifdef FEAT_VIRTUALEDIT
9452 long coladd = 0;
9453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009455 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009456 if (argvars[1].v_type == VAR_UNKNOWN)
9457 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009458 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009459
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009460 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009461 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009462 line = pos.lnum;
9463 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009464#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009465 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009466#endif
9467 }
9468 else
9469 {
9470 line = get_tv_lnum(argvars);
9471 col = get_tv_number_chk(&argvars[1], NULL);
9472#ifdef FEAT_VIRTUALEDIT
9473 if (argvars[2].v_type != VAR_UNKNOWN)
9474 coladd = get_tv_number_chk(&argvars[2], NULL);
9475#endif
9476 }
9477 if (line < 0 || col < 0
9478#ifdef FEAT_VIRTUALEDIT
9479 || coladd < 0
9480#endif
9481 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009482 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 if (line > 0)
9484 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 if (col > 0)
9486 curwin->w_cursor.col = col - 1;
9487#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009488 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489#endif
9490
9491 /* Make sure the cursor is in a valid position. */
9492 check_cursor();
9493#ifdef FEAT_MBYTE
9494 /* Correct cursor for multi-byte character. */
9495 if (has_mbyte)
9496 mb_adjust_cursor();
9497#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009498
9499 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009500 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501}
9502
9503/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009504 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 */
9506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009508 typval_T *argvars;
9509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009511 int noref = 0;
9512
9513 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009514 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009515 if (noref < 0 || noref > 1)
9516 EMSG(_(e_invarg));
9517 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009518 {
9519 current_copyID += COPYID_INC;
9520 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522}
9523
9524/*
9525 * "delete()" function
9526 */
9527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009528f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009529 typval_T *argvars;
9530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531{
9532 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536}
9537
9538/*
9539 * "did_filetype()" function
9540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009543 typval_T *argvars UNUSED;
9544 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548#endif
9549}
9550
9551/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009552 * "diff_filler()" function
9553 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009556 typval_T *argvars UNUSED;
9557 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009558{
9559#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009561#endif
9562}
9563
9564/*
9565 * "diff_hlID()" function
9566 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009569 typval_T *argvars UNUSED;
9570 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009571{
9572#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009574 static linenr_T prev_lnum = 0;
9575 static int changedtick = 0;
9576 static int fnum = 0;
9577 static int change_start = 0;
9578 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009579 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009580 int filler_lines;
9581 int col;
9582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 if (lnum < 0) /* ignore type error in {lnum} arg */
9584 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009585 if (lnum != prev_lnum
9586 || changedtick != curbuf->b_changedtick
9587 || fnum != curbuf->b_fnum)
9588 {
9589 /* New line, buffer, change: need to get the values. */
9590 filler_lines = diff_check(curwin, lnum);
9591 if (filler_lines < 0)
9592 {
9593 if (filler_lines == -1)
9594 {
9595 change_start = MAXCOL;
9596 change_end = -1;
9597 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9598 hlID = HLF_ADD; /* added line */
9599 else
9600 hlID = HLF_CHD; /* changed line */
9601 }
9602 else
9603 hlID = HLF_ADD; /* added line */
9604 }
9605 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009606 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009607 prev_lnum = lnum;
9608 changedtick = curbuf->b_changedtick;
9609 fnum = curbuf->b_fnum;
9610 }
9611
9612 if (hlID == HLF_CHD || hlID == HLF_TXD)
9613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009614 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009615 if (col >= change_start && col <= change_end)
9616 hlID = HLF_TXD; /* changed text */
9617 else
9618 hlID = HLF_CHD; /* changed line */
9619 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009620 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009621#endif
9622}
9623
9624/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009625 * "empty({expr})" function
9626 */
9627 static void
9628f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009629 typval_T *argvars;
9630 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009631{
9632 int n;
9633
9634 switch (argvars[0].v_type)
9635 {
9636 case VAR_STRING:
9637 case VAR_FUNC:
9638 n = argvars[0].vval.v_string == NULL
9639 || *argvars[0].vval.v_string == NUL;
9640 break;
9641 case VAR_NUMBER:
9642 n = argvars[0].vval.v_number == 0;
9643 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009644#ifdef FEAT_FLOAT
9645 case VAR_FLOAT:
9646 n = argvars[0].vval.v_float == 0.0;
9647 break;
9648#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009649 case VAR_LIST:
9650 n = argvars[0].vval.v_list == NULL
9651 || argvars[0].vval.v_list->lv_first == NULL;
9652 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009653 case VAR_DICT:
9654 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009655 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009656 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009657 default:
9658 EMSG2(_(e_intern2), "f_empty()");
9659 n = 0;
9660 }
9661
9662 rettv->vval.v_number = n;
9663}
9664
9665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 * "escape({string}, {chars})" function
9667 */
9668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009670 typval_T *argvars;
9671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672{
9673 char_u buf[NUMBUFLEN];
9674
Bram Moolenaar758711c2005-02-02 23:11:38 +00009675 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9676 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678}
9679
9680/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009681 * "eval()" function
9682 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009683 static void
9684f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009685 typval_T *argvars;
9686 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009687{
9688 char_u *s;
9689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009690 s = get_tv_string_chk(&argvars[0]);
9691 if (s != NULL)
9692 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009693
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009694 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9695 {
9696 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009697 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009698 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009699 else if (*s != NUL)
9700 EMSG(_(e_trailing));
9701}
9702
9703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 * "eventhandler()" function
9705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009708 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712}
9713
9714/*
9715 * "executable()" function
9716 */
9717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 typval_T *argvars;
9720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723}
9724
9725/*
9726 * "exists()" function
9727 */
9728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009730 typval_T *argvars;
9731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732{
9733 char_u *p;
9734 char_u *name;
9735 int n = FALSE;
9736 int len = 0;
9737
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009738 no_autoload = TRUE;
9739
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 if (*p == '$') /* environment variable */
9742 {
9743 /* first try "normal" environment variables (fast) */
9744 if (mch_getenv(p + 1) != NULL)
9745 n = TRUE;
9746 else
9747 {
9748 /* try expanding things like $VIM and ${HOME} */
9749 p = expand_env_save(p);
9750 if (p != NULL && *p != '$')
9751 n = TRUE;
9752 vim_free(p);
9753 }
9754 }
9755 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009757 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009758 if (*skipwhite(p) != NUL)
9759 n = FALSE; /* trailing garbage */
9760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 else if (*p == '*') /* internal or user defined function */
9762 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009763 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 }
9765 else if (*p == ':')
9766 {
9767 n = cmd_exists(p + 1);
9768 }
9769 else if (*p == '#')
9770 {
9771#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009772 if (p[1] == '#')
9773 n = autocmd_supported(p + 2);
9774 else
9775 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776#endif
9777 }
9778 else /* internal variable */
9779 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009780 char_u *tofree;
9781 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009783 /* get_name_len() takes care of expanding curly braces */
9784 name = p;
9785 len = get_name_len(&p, &tofree, TRUE, FALSE);
9786 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009788 if (tofree != NULL)
9789 name = tofree;
9790 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9791 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009793 /* handle d.key, l[idx], f(expr) */
9794 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9795 if (n)
9796 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 }
9798 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009799 if (*p != NUL)
9800 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009802 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 }
9804
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009806
9807 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808}
9809
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009810#ifdef FEAT_FLOAT
9811/*
9812 * "exp()" function
9813 */
9814 static void
9815f_exp(argvars, rettv)
9816 typval_T *argvars;
9817 typval_T *rettv;
9818{
9819 float_T f;
9820
9821 rettv->v_type = VAR_FLOAT;
9822 if (get_float_arg(argvars, &f) == OK)
9823 rettv->vval.v_float = exp(f);
9824 else
9825 rettv->vval.v_float = 0.0;
9826}
9827#endif
9828
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829/*
9830 * "expand()" function
9831 */
9832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009834 typval_T *argvars;
9835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837 char_u *s;
9838 int len;
9839 char_u *errormsg;
9840 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9841 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009842 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844 rettv->v_type = VAR_STRING;
9845 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 if (*s == '%' || *s == '#' || *s == '<')
9847 {
9848 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009849 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 --emsg_off;
9851 }
9852 else
9853 {
9854 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009855 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 if (argvars[1].v_type != VAR_UNKNOWN
9857 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009859 if (!error)
9860 {
9861 ExpandInit(&xpc);
9862 xpc.xp_context = EXPAND_FILES;
9863 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009864 }
9865 else
9866 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 }
9868}
9869
9870/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009871 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009872 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009873 */
9874 static void
9875f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009876 typval_T *argvars;
9877 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009878{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009879 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009880 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009881 list_T *l1, *l2;
9882 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009883 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009884 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009885
Bram Moolenaare9a41262005-01-15 22:18:47 +00009886 l1 = argvars[0].vval.v_list;
9887 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009888 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9889 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009890 {
9891 if (argvars[2].v_type != VAR_UNKNOWN)
9892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 before = get_tv_number_chk(&argvars[2], &error);
9894 if (error)
9895 return; /* type error; errmsg already given */
9896
Bram Moolenaar758711c2005-02-02 23:11:38 +00009897 if (before == l1->lv_len)
9898 item = NULL;
9899 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009900 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009901 item = list_find(l1, before);
9902 if (item == NULL)
9903 {
9904 EMSGN(_(e_listidx), before);
9905 return;
9906 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009907 }
9908 }
9909 else
9910 item = NULL;
9911 list_extend(l1, l2, item);
9912
Bram Moolenaare9a41262005-01-15 22:18:47 +00009913 copy_tv(&argvars[0], rettv);
9914 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009915 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009916 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9917 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009918 dict_T *d1, *d2;
9919 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 char_u *action;
9921 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009923 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009924
9925 d1 = argvars[0].vval.v_dict;
9926 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009927 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9928 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009929 {
9930 /* Check the third argument. */
9931 if (argvars[2].v_type != VAR_UNKNOWN)
9932 {
9933 static char *(av[]) = {"keep", "force", "error"};
9934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 action = get_tv_string_chk(&argvars[2]);
9936 if (action == NULL)
9937 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009938 for (i = 0; i < 3; ++i)
9939 if (STRCMP(action, av[i]) == 0)
9940 break;
9941 if (i == 3)
9942 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009943 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009944 return;
9945 }
9946 }
9947 else
9948 action = (char_u *)"force";
9949
9950 /* Go over all entries in the second dict and add them to the
9951 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009952 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009953 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009954 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009955 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009956 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009957 --todo;
9958 di1 = dict_find(d1, hi2->hi_key, -1);
9959 if (di1 == NULL)
9960 {
9961 di1 = dictitem_copy(HI2DI(hi2));
9962 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9963 dictitem_free(di1);
9964 }
9965 else if (*action == 'e')
9966 {
9967 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9968 break;
9969 }
9970 else if (*action == 'f')
9971 {
9972 clear_tv(&di1->di_tv);
9973 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9974 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009975 }
9976 }
9977
Bram Moolenaare9a41262005-01-15 22:18:47 +00009978 copy_tv(&argvars[0], rettv);
9979 }
9980 }
9981 else
9982 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009983}
9984
9985/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009986 * "feedkeys()" function
9987 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009988 static void
9989f_feedkeys(argvars, rettv)
9990 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009991 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009992{
9993 int remap = TRUE;
9994 char_u *keys, *flags;
9995 char_u nbuf[NUMBUFLEN];
9996 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009997 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009998
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009999 /* This is not allowed in the sandbox. If the commands would still be
10000 * executed in the sandbox it would be OK, but it probably happens later,
10001 * when "sandbox" is no longer set. */
10002 if (check_secure())
10003 return;
10004
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010005 keys = get_tv_string(&argvars[0]);
10006 if (*keys != NUL)
10007 {
10008 if (argvars[1].v_type != VAR_UNKNOWN)
10009 {
10010 flags = get_tv_string_buf(&argvars[1], nbuf);
10011 for ( ; *flags != NUL; ++flags)
10012 {
10013 switch (*flags)
10014 {
10015 case 'n': remap = FALSE; break;
10016 case 'm': remap = TRUE; break;
10017 case 't': typed = TRUE; break;
10018 }
10019 }
10020 }
10021
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010022 /* Need to escape K_SPECIAL and CSI before putting the string in the
10023 * typeahead buffer. */
10024 keys_esc = vim_strsave_escape_csi(keys);
10025 if (keys_esc != NULL)
10026 {
10027 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010028 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010029 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010030 if (vgetc_busy)
10031 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010032 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010033 }
10034}
10035
10036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 * "filereadable()" function
10038 */
10039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010041 typval_T *argvars;
10042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010044 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 char_u *p;
10046 int n;
10047
Bram Moolenaarc236c162008-07-13 17:41:49 +000010048#ifndef O_NONBLOCK
10049# define O_NONBLOCK 0
10050#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010051 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010052 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10053 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 {
10055 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010056 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 }
10058 else
10059 n = FALSE;
10060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062}
10063
10064/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010065 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 * rights to write into.
10067 */
10068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010069f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010070 typval_T *argvars;
10071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010073 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074}
10075
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010076static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010077
10078 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010079findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010080 typval_T *argvars;
10081 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010082 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010083{
10084#ifdef FEAT_SEARCHPATH
10085 char_u *fname;
10086 char_u *fresult = NULL;
10087 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10088 char_u *p;
10089 char_u pathbuf[NUMBUFLEN];
10090 int count = 1;
10091 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010092 int error = FALSE;
10093#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010094
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010095 rettv->vval.v_string = NULL;
10096 rettv->v_type = VAR_STRING;
10097
10098#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010100
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010101 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010103 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10104 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010105 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106 else
10107 {
10108 if (*p != NUL)
10109 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010112 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010113 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010114 }
10115
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010116 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10117 error = TRUE;
10118
10119 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010121 do
10122 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010123 if (rettv->v_type == VAR_STRING)
10124 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010125 fresult = find_file_in_path_option(first ? fname : NULL,
10126 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010127 0, first, path,
10128 find_what,
10129 curbuf->b_ffname,
10130 find_what == FINDFILE_DIR
10131 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010133
10134 if (fresult != NULL && rettv->v_type == VAR_LIST)
10135 list_append_string(rettv->vval.v_list, fresult, -1);
10136
10137 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010139
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010140 if (rettv->v_type == VAR_STRING)
10141 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010142#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010143}
10144
Bram Moolenaar33570922005-01-25 22:26:29 +000010145static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10146static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010147
10148/*
10149 * Implementation of map() and filter().
10150 */
10151 static void
10152filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010153 typval_T *argvars;
10154 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010155 int map;
10156{
10157 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010158 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010159 listitem_T *li, *nli;
10160 list_T *l = NULL;
10161 dictitem_T *di;
10162 hashtab_T *ht;
10163 hashitem_T *hi;
10164 dict_T *d = NULL;
10165 typval_T save_val;
10166 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010167 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010168 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010169 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010170 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010171 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010172
Bram Moolenaare9a41262005-01-15 22:18:47 +000010173 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010174 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010175 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010176 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010177 return;
10178 }
10179 else if (argvars[0].v_type == VAR_DICT)
10180 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010181 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010182 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010183 return;
10184 }
10185 else
10186 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010187 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010188 return;
10189 }
10190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010191 expr = get_tv_string_buf_chk(&argvars[1], buf);
10192 /* On type errors, the preceding call has already displayed an error
10193 * message. Avoid a misleading error message for an empty string that
10194 * was not passed as argument. */
10195 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010197 prepare_vimvar(VV_VAL, &save_val);
10198 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010199
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010200 /* We reset "did_emsg" to be able to detect whether an error
10201 * occurred during evaluation of the expression. */
10202 save_did_emsg = did_emsg;
10203 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010204
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010205 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010206 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 vimvars[VV_KEY].vv_type = VAR_STRING;
10209
10210 ht = &d->dv_hashtab;
10211 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010212 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 if (!HASHITEM_EMPTY(hi))
10216 {
10217 --todo;
10218 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010219 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010220 break;
10221 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010222 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010223 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010224 break;
10225 if (!map && rem)
10226 dictitem_remove(d, di);
10227 clear_tv(&vimvars[VV_KEY].vv_tv);
10228 }
10229 }
10230 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 }
10232 else
10233 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010234 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 for (li = l->lv_first; li != NULL; li = nli)
10237 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010238 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010239 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010241 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010242 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010243 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010244 break;
10245 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010246 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010247 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010248 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010249 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010250
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010251 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010252 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010253
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010254 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010255 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010256
10257 copy_tv(&argvars[0], rettv);
10258}
10259
10260 static int
10261filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010262 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010263 char_u *expr;
10264 int map;
10265 int *remp;
10266{
Bram Moolenaar33570922005-01-25 22:26:29 +000010267 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010268 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010269 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010270
Bram Moolenaar33570922005-01-25 22:26:29 +000010271 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010272 s = expr;
10273 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010274 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010275 if (*s != NUL) /* check for trailing chars after expr */
10276 {
10277 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010278 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010279 }
10280 if (map)
10281 {
10282 /* map(): replace the list item value */
10283 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010284 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010285 *tv = rettv;
10286 }
10287 else
10288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 int error = FALSE;
10290
Bram Moolenaare9a41262005-01-15 22:18:47 +000010291 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010292 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010293 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010294 /* On type error, nothing has been removed; return FAIL to stop the
10295 * loop. The error message was given by get_tv_number_chk(). */
10296 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010297 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010298 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010299 retval = OK;
10300theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010301 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010302 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010303}
10304
10305/*
10306 * "filter()" function
10307 */
10308 static void
10309f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010310 typval_T *argvars;
10311 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010312{
10313 filter_map(argvars, rettv, FALSE);
10314}
10315
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010316/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010317 * "finddir({fname}[, {path}[, {count}]])" function
10318 */
10319 static void
10320f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010321 typval_T *argvars;
10322 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010323{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010324 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010325}
10326
10327/*
10328 * "findfile({fname}[, {path}[, {count}]])" function
10329 */
10330 static void
10331f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010332 typval_T *argvars;
10333 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010334{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010335 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010336}
10337
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010338#ifdef FEAT_FLOAT
10339/*
10340 * "float2nr({float})" function
10341 */
10342 static void
10343f_float2nr(argvars, rettv)
10344 typval_T *argvars;
10345 typval_T *rettv;
10346{
10347 float_T f;
10348
10349 if (get_float_arg(argvars, &f) == OK)
10350 {
10351 if (f < -0x7fffffff)
10352 rettv->vval.v_number = -0x7fffffff;
10353 else if (f > 0x7fffffff)
10354 rettv->vval.v_number = 0x7fffffff;
10355 else
10356 rettv->vval.v_number = (varnumber_T)f;
10357 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010358}
10359
10360/*
10361 * "floor({float})" function
10362 */
10363 static void
10364f_floor(argvars, rettv)
10365 typval_T *argvars;
10366 typval_T *rettv;
10367{
10368 float_T f;
10369
10370 rettv->v_type = VAR_FLOAT;
10371 if (get_float_arg(argvars, &f) == OK)
10372 rettv->vval.v_float = floor(f);
10373 else
10374 rettv->vval.v_float = 0.0;
10375}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010376
10377/*
10378 * "fmod()" function
10379 */
10380 static void
10381f_fmod(argvars, rettv)
10382 typval_T *argvars;
10383 typval_T *rettv;
10384{
10385 float_T fx, fy;
10386
10387 rettv->v_type = VAR_FLOAT;
10388 if (get_float_arg(argvars, &fx) == OK
10389 && get_float_arg(&argvars[1], &fy) == OK)
10390 rettv->vval.v_float = fmod(fx, fy);
10391 else
10392 rettv->vval.v_float = 0.0;
10393}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010394#endif
10395
Bram Moolenaar0d660222005-01-07 21:51:51 +000010396/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010397 * "fnameescape({string})" function
10398 */
10399 static void
10400f_fnameescape(argvars, rettv)
10401 typval_T *argvars;
10402 typval_T *rettv;
10403{
10404 rettv->vval.v_string = vim_strsave_fnameescape(
10405 get_tv_string(&argvars[0]), FALSE);
10406 rettv->v_type = VAR_STRING;
10407}
10408
10409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 * "fnamemodify({fname}, {mods})" function
10411 */
10412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010414 typval_T *argvars;
10415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416{
10417 char_u *fname;
10418 char_u *mods;
10419 int usedlen = 0;
10420 int len;
10421 char_u *fbuf = NULL;
10422 char_u buf[NUMBUFLEN];
10423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010424 fname = get_tv_string_chk(&argvars[0]);
10425 mods = get_tv_string_buf_chk(&argvars[1], buf);
10426 if (fname == NULL || mods == NULL)
10427 fname = NULL;
10428 else
10429 {
10430 len = (int)STRLEN(fname);
10431 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010434 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010438 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 vim_free(fbuf);
10440}
10441
Bram Moolenaar33570922005-01-25 22:26:29 +000010442static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443
10444/*
10445 * "foldclosed()" function
10446 */
10447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010448foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010449 typval_T *argvars;
10450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 int end;
10452{
10453#ifdef FEAT_FOLDING
10454 linenr_T lnum;
10455 linenr_T first, last;
10456
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10459 {
10460 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10461 {
10462 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010463 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010465 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 return;
10467 }
10468 }
10469#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471}
10472
10473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010474 * "foldclosed()" function
10475 */
10476 static void
10477f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010478 typval_T *argvars;
10479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010480{
10481 foldclosed_both(argvars, rettv, FALSE);
10482}
10483
10484/*
10485 * "foldclosedend()" function
10486 */
10487 static void
10488f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010489 typval_T *argvars;
10490 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010491{
10492 foldclosed_both(argvars, rettv, TRUE);
10493}
10494
10495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 * "foldlevel()" function
10497 */
10498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010499f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010500 typval_T *argvars;
10501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502{
10503#ifdef FEAT_FOLDING
10504 linenr_T lnum;
10505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510}
10511
10512/*
10513 * "foldtext()" function
10514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010516f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010517 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519{
10520#ifdef FEAT_FOLDING
10521 linenr_T lnum;
10522 char_u *s;
10523 char_u *r;
10524 int len;
10525 char *txt;
10526#endif
10527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 rettv->v_type = VAR_STRING;
10529 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010531 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10532 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10533 <= curbuf->b_ml.ml_line_count
10534 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 {
10536 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010537 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10538 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 {
10540 if (!linewhite(lnum))
10541 break;
10542 ++lnum;
10543 }
10544
10545 /* Find interesting text in this line. */
10546 s = skipwhite(ml_get(lnum));
10547 /* skip C comment-start */
10548 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010551 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010552 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010553 {
10554 s = skipwhite(ml_get(lnum + 1));
10555 if (*s == '*')
10556 s = skipwhite(s + 1);
10557 }
10558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 txt = _("+-%s%3ld lines: ");
10560 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010561 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 + 20 /* for %3ld */
10563 + STRLEN(s))); /* concatenated */
10564 if (r != NULL)
10565 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010566 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10567 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10568 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 len = (int)STRLEN(r);
10570 STRCAT(r, s);
10571 /* remove 'foldmarker' and 'commentstring' */
10572 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010573 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 }
10575 }
10576#endif
10577}
10578
10579/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010580 * "foldtextresult(lnum)" function
10581 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010583f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010584 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010585 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010586{
10587#ifdef FEAT_FOLDING
10588 linenr_T lnum;
10589 char_u *text;
10590 char_u buf[51];
10591 foldinfo_T foldinfo;
10592 int fold_count;
10593#endif
10594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010595 rettv->v_type = VAR_STRING;
10596 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010597#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010599 /* treat illegal types and illegal string values for {lnum} the same */
10600 if (lnum < 0)
10601 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010602 fold_count = foldedCount(curwin, lnum, &foldinfo);
10603 if (fold_count > 0)
10604 {
10605 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10606 &foldinfo, buf);
10607 if (text == buf)
10608 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010609 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010610 }
10611#endif
10612}
10613
10614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 * "foreground()" function
10616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010618f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010619 typval_T *argvars UNUSED;
10620 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622#ifdef FEAT_GUI
10623 if (gui.in_use)
10624 gui_mch_set_foreground();
10625#else
10626# ifdef WIN32
10627 win32_set_foreground();
10628# endif
10629#endif
10630}
10631
10632/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010633 * "function()" function
10634 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010636f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010637 typval_T *argvars;
10638 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010639{
10640 char_u *s;
10641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010642 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010643 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010644 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010645 /* Don't check an autoload name for existence here. */
10646 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010647 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010648 else
10649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010650 rettv->vval.v_string = vim_strsave(s);
10651 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010652 }
10653}
10654
10655/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010656 * "garbagecollect()" function
10657 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010658 static void
10659f_garbagecollect(argvars, rettv)
10660 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010661 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010662{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010663 /* This is postponed until we are back at the toplevel, because we may be
10664 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10665 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010666
10667 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10668 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010669}
10670
10671/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010672 * "get()" function
10673 */
10674 static void
10675f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010676 typval_T *argvars;
10677 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010678{
Bram Moolenaar33570922005-01-25 22:26:29 +000010679 listitem_T *li;
10680 list_T *l;
10681 dictitem_T *di;
10682 dict_T *d;
10683 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010684
Bram Moolenaare9a41262005-01-15 22:18:47 +000010685 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010686 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010687 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 int error = FALSE;
10690
10691 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10692 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010693 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010694 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010695 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010696 else if (argvars[0].v_type == VAR_DICT)
10697 {
10698 if ((d = argvars[0].vval.v_dict) != NULL)
10699 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010700 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010701 if (di != NULL)
10702 tv = &di->di_tv;
10703 }
10704 }
10705 else
10706 EMSG2(_(e_listdictarg), "get()");
10707
10708 if (tv == NULL)
10709 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010710 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010711 copy_tv(&argvars[2], rettv);
10712 }
10713 else
10714 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010715}
10716
Bram Moolenaar342337a2005-07-21 21:11:17 +000010717static 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 +000010718
10719/*
10720 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010721 * Return a range (from start to end) of lines in rettv from the specified
10722 * buffer.
10723 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010724 */
10725 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010726get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010727 buf_T *buf;
10728 linenr_T start;
10729 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010730 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010731 typval_T *rettv;
10732{
10733 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010734
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010735 if (retlist && rettv_list_alloc(rettv) == FAIL)
10736 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010737
10738 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10739 return;
10740
10741 if (!retlist)
10742 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010743 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10744 p = ml_get_buf(buf, start, FALSE);
10745 else
10746 p = (char_u *)"";
10747
10748 rettv->v_type = VAR_STRING;
10749 rettv->vval.v_string = vim_strsave(p);
10750 }
10751 else
10752 {
10753 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010754 return;
10755
10756 if (start < 1)
10757 start = 1;
10758 if (end > buf->b_ml.ml_line_count)
10759 end = buf->b_ml.ml_line_count;
10760 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010761 if (list_append_string(rettv->vval.v_list,
10762 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010763 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010764 }
10765}
10766
10767/*
10768 * "getbufline()" function
10769 */
10770 static void
10771f_getbufline(argvars, rettv)
10772 typval_T *argvars;
10773 typval_T *rettv;
10774{
10775 linenr_T lnum;
10776 linenr_T end;
10777 buf_T *buf;
10778
10779 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10780 ++emsg_off;
10781 buf = get_buf_tv(&argvars[0]);
10782 --emsg_off;
10783
Bram Moolenaar661b1822005-07-28 22:36:45 +000010784 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010785 if (argvars[2].v_type == VAR_UNKNOWN)
10786 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010787 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010788 end = get_tv_lnum_buf(&argvars[2], buf);
10789
Bram Moolenaar342337a2005-07-21 21:11:17 +000010790 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010791}
10792
Bram Moolenaar0d660222005-01-07 21:51:51 +000010793/*
10794 * "getbufvar()" function
10795 */
10796 static void
10797f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010798 typval_T *argvars;
10799 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010800{
10801 buf_T *buf;
10802 buf_T *save_curbuf;
10803 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010804 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10807 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010808 ++emsg_off;
10809 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010810
10811 rettv->v_type = VAR_STRING;
10812 rettv->vval.v_string = NULL;
10813
10814 if (buf != NULL && varname != NULL)
10815 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010816 /* set curbuf to be our buf, temporarily */
10817 save_curbuf = curbuf;
10818 curbuf = buf;
10819
Bram Moolenaar0d660222005-01-07 21:51:51 +000010820 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010821 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010822 else
10823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010824 if (*varname == NUL)
10825 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10826 * scope prefix before the NUL byte is required by
10827 * find_var_in_ht(). */
10828 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010829 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010830 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010831 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010832 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010833 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010834
10835 /* restore previous notion of curbuf */
10836 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010837 }
10838
10839 --emsg_off;
10840}
10841
10842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 * "getchar()" function
10844 */
10845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010847 typval_T *argvars;
10848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849{
10850 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010853 /* Position the cursor. Needed after a message that ends in a space. */
10854 windgoto(msg_row, msg_col);
10855
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 ++no_mapping;
10857 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010858 for (;;)
10859 {
10860 if (argvars[0].v_type == VAR_UNKNOWN)
10861 /* getchar(): blocking wait. */
10862 n = safe_vgetc();
10863 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10864 /* getchar(1): only check if char avail */
10865 n = vpeekc();
10866 else if (error || vpeekc() == NUL)
10867 /* illegal argument or getchar(0) and no char avail: return zero */
10868 n = 0;
10869 else
10870 /* getchar(0) and char avail: return char */
10871 n = safe_vgetc();
10872 if (n == K_IGNORE)
10873 continue;
10874 break;
10875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 --no_mapping;
10877 --allow_keys;
10878
Bram Moolenaar219b8702006-11-01 14:32:36 +000010879 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10880 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10881 vimvars[VV_MOUSE_COL].vv_nr = 0;
10882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 if (IS_SPECIAL(n) || mod_mask != 0)
10885 {
10886 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10887 int i = 0;
10888
10889 /* Turn a special key into three bytes, plus modifier. */
10890 if (mod_mask != 0)
10891 {
10892 temp[i++] = K_SPECIAL;
10893 temp[i++] = KS_MODIFIER;
10894 temp[i++] = mod_mask;
10895 }
10896 if (IS_SPECIAL(n))
10897 {
10898 temp[i++] = K_SPECIAL;
10899 temp[i++] = K_SECOND(n);
10900 temp[i++] = K_THIRD(n);
10901 }
10902#ifdef FEAT_MBYTE
10903 else if (has_mbyte)
10904 i += (*mb_char2bytes)(n, temp + i);
10905#endif
10906 else
10907 temp[i++] = n;
10908 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010909 rettv->v_type = VAR_STRING;
10910 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010911
10912#ifdef FEAT_MOUSE
10913 if (n == K_LEFTMOUSE
10914 || n == K_LEFTMOUSE_NM
10915 || n == K_LEFTDRAG
10916 || n == K_LEFTRELEASE
10917 || n == K_LEFTRELEASE_NM
10918 || n == K_MIDDLEMOUSE
10919 || n == K_MIDDLEDRAG
10920 || n == K_MIDDLERELEASE
10921 || n == K_RIGHTMOUSE
10922 || n == K_RIGHTDRAG
10923 || n == K_RIGHTRELEASE
10924 || n == K_X1MOUSE
10925 || n == K_X1DRAG
10926 || n == K_X1RELEASE
10927 || n == K_X2MOUSE
10928 || n == K_X2DRAG
10929 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020010930 || n == K_MOUSELEFT
10931 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000010932 || n == K_MOUSEDOWN
10933 || n == K_MOUSEUP)
10934 {
10935 int row = mouse_row;
10936 int col = mouse_col;
10937 win_T *win;
10938 linenr_T lnum;
10939# ifdef FEAT_WINDOWS
10940 win_T *wp;
10941# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010942 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010943
10944 if (row >= 0 && col >= 0)
10945 {
10946 /* Find the window at the mouse coordinates and compute the
10947 * text position. */
10948 win = mouse_find_win(&row, &col);
10949 (void)mouse_comp_pos(win, &row, &col, &lnum);
10950# ifdef FEAT_WINDOWS
10951 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010952 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010953# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010954 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010955 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10956 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10957 }
10958 }
10959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 }
10961}
10962
10963/*
10964 * "getcharmod()" function
10965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010968 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010971 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972}
10973
10974/*
10975 * "getcmdline()" function
10976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010979 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010982 rettv->v_type = VAR_STRING;
10983 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984}
10985
10986/*
10987 * "getcmdpos()" function
10988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010991 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010994 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995}
10996
10997/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010998 * "getcmdtype()" function
10999 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011000 static void
11001f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011002 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011003 typval_T *rettv;
11004{
11005 rettv->v_type = VAR_STRING;
11006 rettv->vval.v_string = alloc(2);
11007 if (rettv->vval.v_string != NULL)
11008 {
11009 rettv->vval.v_string[0] = get_cmdline_type();
11010 rettv->vval.v_string[1] = NUL;
11011 }
11012}
11013
11014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 * "getcwd()" function
11016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011018f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011019 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021{
11022 char_u cwd[MAXPATHL];
11023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011026 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 else
11028 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011029 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011031 if (rettv->vval.v_string != NULL)
11032 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033#endif
11034 }
11035}
11036
11037/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011038 * "getfontname()" function
11039 */
11040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011042 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011043 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011044{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 rettv->v_type = VAR_STRING;
11046 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011047#ifdef FEAT_GUI
11048 if (gui.in_use)
11049 {
11050 GuiFont font;
11051 char_u *name = NULL;
11052
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011053 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011054 {
11055 /* Get the "Normal" font. Either the name saved by
11056 * hl_set_font_name() or from the font ID. */
11057 font = gui.norm_font;
11058 name = hl_get_font_name();
11059 }
11060 else
11061 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011063 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11064 return;
11065 font = gui_mch_get_font(name, FALSE);
11066 if (font == NOFONT)
11067 return; /* Invalid font name, return empty string. */
11068 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011070 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011071 gui_mch_free_font(font);
11072 }
11073#endif
11074}
11075
11076/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011077 * "getfperm({fname})" function
11078 */
11079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011080f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011081 typval_T *argvars;
11082 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011083{
11084 char_u *fname;
11085 struct stat st;
11086 char_u *perm = NULL;
11087 char_u flags[] = "rwx";
11088 int i;
11089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011090 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011092 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011093 if (mch_stat((char *)fname, &st) >= 0)
11094 {
11095 perm = vim_strsave((char_u *)"---------");
11096 if (perm != NULL)
11097 {
11098 for (i = 0; i < 9; i++)
11099 {
11100 if (st.st_mode & (1 << (8 - i)))
11101 perm[i] = flags[i % 3];
11102 }
11103 }
11104 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011105 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011106}
11107
11108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 * "getfsize({fname})" function
11110 */
11111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011113 typval_T *argvars;
11114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115{
11116 char_u *fname;
11117 struct stat st;
11118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122
11123 if (mch_stat((char *)fname, &st) >= 0)
11124 {
11125 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011130
11131 /* non-perfect check for overflow */
11132 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11133 rettv->vval.v_number = -2;
11134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 }
11136 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138}
11139
11140/*
11141 * "getftime({fname})" function
11142 */
11143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011145 typval_T *argvars;
11146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147{
11148 char_u *fname;
11149 struct stat st;
11150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011151 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152
11153 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011156 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157}
11158
11159/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011160 * "getftype({fname})" function
11161 */
11162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011164 typval_T *argvars;
11165 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011166{
11167 char_u *fname;
11168 struct stat st;
11169 char_u *type = NULL;
11170 char *t;
11171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011173
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011175 if (mch_lstat((char *)fname, &st) >= 0)
11176 {
11177#ifdef S_ISREG
11178 if (S_ISREG(st.st_mode))
11179 t = "file";
11180 else if (S_ISDIR(st.st_mode))
11181 t = "dir";
11182# ifdef S_ISLNK
11183 else if (S_ISLNK(st.st_mode))
11184 t = "link";
11185# endif
11186# ifdef S_ISBLK
11187 else if (S_ISBLK(st.st_mode))
11188 t = "bdev";
11189# endif
11190# ifdef S_ISCHR
11191 else if (S_ISCHR(st.st_mode))
11192 t = "cdev";
11193# endif
11194# ifdef S_ISFIFO
11195 else if (S_ISFIFO(st.st_mode))
11196 t = "fifo";
11197# endif
11198# ifdef S_ISSOCK
11199 else if (S_ISSOCK(st.st_mode))
11200 t = "fifo";
11201# endif
11202 else
11203 t = "other";
11204#else
11205# ifdef S_IFMT
11206 switch (st.st_mode & S_IFMT)
11207 {
11208 case S_IFREG: t = "file"; break;
11209 case S_IFDIR: t = "dir"; break;
11210# ifdef S_IFLNK
11211 case S_IFLNK: t = "link"; break;
11212# endif
11213# ifdef S_IFBLK
11214 case S_IFBLK: t = "bdev"; break;
11215# endif
11216# ifdef S_IFCHR
11217 case S_IFCHR: t = "cdev"; break;
11218# endif
11219# ifdef S_IFIFO
11220 case S_IFIFO: t = "fifo"; break;
11221# endif
11222# ifdef S_IFSOCK
11223 case S_IFSOCK: t = "socket"; break;
11224# endif
11225 default: t = "other";
11226 }
11227# else
11228 if (mch_isdir(fname))
11229 t = "dir";
11230 else
11231 t = "file";
11232# endif
11233#endif
11234 type = vim_strsave((char_u *)t);
11235 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011237}
11238
11239/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011240 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011241 */
11242 static void
11243f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011244 typval_T *argvars;
11245 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011246{
11247 linenr_T lnum;
11248 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011249 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011250
11251 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011252 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011253 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011254 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011255 retlist = FALSE;
11256 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011257 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011258 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011259 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011260 retlist = TRUE;
11261 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011262
Bram Moolenaar342337a2005-07-21 21:11:17 +000011263 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011264}
11265
11266/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011267 * "getmatches()" function
11268 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011269 static void
11270f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011271 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011272 typval_T *rettv;
11273{
11274#ifdef FEAT_SEARCH_EXTRA
11275 dict_T *dict;
11276 matchitem_T *cur = curwin->w_match_head;
11277
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011278 if (rettv_list_alloc(rettv) == OK)
11279 {
11280 while (cur != NULL)
11281 {
11282 dict = dict_alloc();
11283 if (dict == NULL)
11284 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011285 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11286 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11287 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11288 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11289 list_append_dict(rettv->vval.v_list, dict);
11290 cur = cur->next;
11291 }
11292 }
11293#endif
11294}
11295
11296/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011297 * "getpid()" function
11298 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011299 static void
11300f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011301 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011302 typval_T *rettv;
11303{
11304 rettv->vval.v_number = mch_get_pid();
11305}
11306
11307/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011308 * "getpos(string)" function
11309 */
11310 static void
11311f_getpos(argvars, rettv)
11312 typval_T *argvars;
11313 typval_T *rettv;
11314{
11315 pos_T *fp;
11316 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011317 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011318
11319 if (rettv_list_alloc(rettv) == OK)
11320 {
11321 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011322 fp = var2fpos(&argvars[0], TRUE, &fnum);
11323 if (fnum != -1)
11324 list_append_number(l, (varnumber_T)fnum);
11325 else
11326 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011327 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11328 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011329 list_append_number(l, (fp != NULL)
11330 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011331 : (varnumber_T)0);
11332 list_append_number(l,
11333#ifdef FEAT_VIRTUALEDIT
11334 (fp != NULL) ? (varnumber_T)fp->coladd :
11335#endif
11336 (varnumber_T)0);
11337 }
11338 else
11339 rettv->vval.v_number = FALSE;
11340}
11341
11342/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011343 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011344 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011345 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011346f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011347 typval_T *argvars UNUSED;
11348 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011349{
11350#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011351 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011352#endif
11353
Bram Moolenaar2641f772005-03-25 21:58:17 +000011354#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011355 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011356 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011357 wp = NULL;
11358 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11359 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011360 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011361 if (wp == NULL)
11362 return;
11363 }
11364
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011365 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011366 }
11367#endif
11368}
11369
11370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 * "getreg()" function
11372 */
11373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011374f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011375 typval_T *argvars;
11376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377{
11378 char_u *strregname;
11379 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011380 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011383 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011385 strregname = get_tv_string_chk(&argvars[0]);
11386 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011387 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011391 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 regname = (strregname == NULL ? '"' : *strregname);
11393 if (regname == 0)
11394 regname = '"';
11395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397 rettv->vval.v_string = error ? NULL :
11398 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399}
11400
11401/*
11402 * "getregtype()" function
11403 */
11404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011405f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011406 typval_T *argvars;
11407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408{
11409 char_u *strregname;
11410 int regname;
11411 char_u buf[NUMBUFLEN + 2];
11412 long reglen = 0;
11413
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011414 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011415 {
11416 strregname = get_tv_string_chk(&argvars[0]);
11417 if (strregname == NULL) /* type error; errmsg already given */
11418 {
11419 rettv->v_type = VAR_STRING;
11420 rettv->vval.v_string = NULL;
11421 return;
11422 }
11423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 else
11425 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011426 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427
11428 regname = (strregname == NULL ? '"' : *strregname);
11429 if (regname == 0)
11430 regname = '"';
11431
11432 buf[0] = NUL;
11433 buf[1] = NUL;
11434 switch (get_reg_type(regname, &reglen))
11435 {
11436 case MLINE: buf[0] = 'V'; break;
11437 case MCHAR: buf[0] = 'v'; break;
11438#ifdef FEAT_VISUAL
11439 case MBLOCK:
11440 buf[0] = Ctrl_V;
11441 sprintf((char *)buf + 1, "%ld", reglen + 1);
11442 break;
11443#endif
11444 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 rettv->v_type = VAR_STRING;
11446 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447}
11448
11449/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011450 * "gettabvar()" function
11451 */
11452 static void
11453f_gettabvar(argvars, rettv)
11454 typval_T *argvars;
11455 typval_T *rettv;
11456{
11457 tabpage_T *tp;
11458 dictitem_T *v;
11459 char_u *varname;
11460
11461 rettv->v_type = VAR_STRING;
11462 rettv->vval.v_string = NULL;
11463
11464 varname = get_tv_string_chk(&argvars[1]);
11465 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11466 if (tp != NULL && varname != NULL)
11467 {
11468 /* look up the variable */
11469 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11470 if (v != NULL)
11471 copy_tv(&v->di_tv, rettv);
11472 }
11473}
11474
11475/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011476 * "gettabwinvar()" function
11477 */
11478 static void
11479f_gettabwinvar(argvars, rettv)
11480 typval_T *argvars;
11481 typval_T *rettv;
11482{
11483 getwinvar(argvars, rettv, 1);
11484}
11485
11486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 * "getwinposx()" function
11488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011491 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495#ifdef FEAT_GUI
11496 if (gui.in_use)
11497 {
11498 int x, y;
11499
11500 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 }
11503#endif
11504}
11505
11506/*
11507 * "getwinposy()" function
11508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011511 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515#ifdef FEAT_GUI
11516 if (gui.in_use)
11517 {
11518 int x, y;
11519
11520 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 }
11523#endif
11524}
11525
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011526/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011527 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011528 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011529 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011530find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011531 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011532 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011533{
11534#ifdef FEAT_WINDOWS
11535 win_T *wp;
11536#endif
11537 int nr;
11538
11539 nr = get_tv_number_chk(vp, NULL);
11540
11541#ifdef FEAT_WINDOWS
11542 if (nr < 0)
11543 return NULL;
11544 if (nr == 0)
11545 return curwin;
11546
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011547 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11548 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011549 if (--nr <= 0)
11550 break;
11551 return wp;
11552#else
11553 if (nr == 0 || nr == 1)
11554 return curwin;
11555 return NULL;
11556#endif
11557}
11558
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559/*
11560 * "getwinvar()" function
11561 */
11562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011564 typval_T *argvars;
11565 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011567 getwinvar(argvars, rettv, 0);
11568}
11569
11570/*
11571 * getwinvar() and gettabwinvar()
11572 */
11573 static void
11574getwinvar(argvars, rettv, off)
11575 typval_T *argvars;
11576 typval_T *rettv;
11577 int off; /* 1 for gettabwinvar() */
11578{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579 win_T *win, *oldcurwin;
11580 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011581 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011582 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011584#ifdef FEAT_WINDOWS
11585 if (off == 1)
11586 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11587 else
11588 tp = curtab;
11589#endif
11590 win = find_win_by_nr(&argvars[off], tp);
11591 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011592 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011594 rettv->v_type = VAR_STRING;
11595 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596
11597 if (win != NULL && varname != NULL)
11598 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011599 /* Set curwin to be our win, temporarily. Also set curbuf, so
11600 * that we can get buffer-local options. */
11601 oldcurwin = curwin;
11602 curwin = win;
11603 curbuf = win->w_buffer;
11604
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 else
11608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011609 if (*varname == NUL)
11610 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11611 * scope prefix before the NUL byte is required by
11612 * find_var_in_ht(). */
11613 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011615 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011617 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011619
11620 /* restore previous notion of curwin */
11621 curwin = oldcurwin;
11622 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 }
11624
11625 --emsg_off;
11626}
11627
11628/*
11629 * "glob()" function
11630 */
11631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011633 typval_T *argvars;
11634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011636 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011638 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011640 /* When the optional second argument is non-zero, don't remove matches
11641 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11642 if (argvars[1].v_type != VAR_UNKNOWN
11643 && get_tv_number_chk(&argvars[1], &error))
11644 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011645 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011646 if (!error)
11647 {
11648 ExpandInit(&xpc);
11649 xpc.xp_context = EXPAND_FILES;
11650 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11651 NULL, flags, WILD_ALL);
11652 }
11653 else
11654 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655}
11656
11657/*
11658 * "globpath()" function
11659 */
11660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011662 typval_T *argvars;
11663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011665 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011668 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011670 /* When the optional second argument is non-zero, don't remove matches
11671 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11672 if (argvars[2].v_type != VAR_UNKNOWN
11673 && get_tv_number_chk(&argvars[2], &error))
11674 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011675 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011676 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011677 rettv->vval.v_string = NULL;
11678 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011679 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11680 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681}
11682
11683/*
11684 * "has()" function
11685 */
11686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011687f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011688 typval_T *argvars;
11689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690{
11691 int i;
11692 char_u *name;
11693 int n = FALSE;
11694 static char *(has_list[]) =
11695 {
11696#ifdef AMIGA
11697 "amiga",
11698# ifdef FEAT_ARP
11699 "arp",
11700# endif
11701#endif
11702#ifdef __BEOS__
11703 "beos",
11704#endif
11705#ifdef MSDOS
11706# ifdef DJGPP
11707 "dos32",
11708# else
11709 "dos16",
11710# endif
11711#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011712#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 "mac",
11714#endif
11715#if defined(MACOS_X_UNIX)
11716 "macunix",
11717#endif
11718#ifdef OS2
11719 "os2",
11720#endif
11721#ifdef __QNX__
11722 "qnx",
11723#endif
11724#ifdef RISCOS
11725 "riscos",
11726#endif
11727#ifdef UNIX
11728 "unix",
11729#endif
11730#ifdef VMS
11731 "vms",
11732#endif
11733#ifdef WIN16
11734 "win16",
11735#endif
11736#ifdef WIN32
11737 "win32",
11738#endif
11739#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11740 "win32unix",
11741#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011742#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743 "win64",
11744#endif
11745#ifdef EBCDIC
11746 "ebcdic",
11747#endif
11748#ifndef CASE_INSENSITIVE_FILENAME
11749 "fname_case",
11750#endif
11751#ifdef FEAT_ARABIC
11752 "arabic",
11753#endif
11754#ifdef FEAT_AUTOCMD
11755 "autocmd",
11756#endif
11757#ifdef FEAT_BEVAL
11758 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011759# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11760 "balloon_multiline",
11761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762#endif
11763#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11764 "builtin_terms",
11765# ifdef ALL_BUILTIN_TCAPS
11766 "all_builtin_terms",
11767# endif
11768#endif
11769#ifdef FEAT_BYTEOFF
11770 "byte_offset",
11771#endif
11772#ifdef FEAT_CINDENT
11773 "cindent",
11774#endif
11775#ifdef FEAT_CLIENTSERVER
11776 "clientserver",
11777#endif
11778#ifdef FEAT_CLIPBOARD
11779 "clipboard",
11780#endif
11781#ifdef FEAT_CMDL_COMPL
11782 "cmdline_compl",
11783#endif
11784#ifdef FEAT_CMDHIST
11785 "cmdline_hist",
11786#endif
11787#ifdef FEAT_COMMENTS
11788 "comments",
11789#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011790#ifdef FEAT_CONCEAL
11791 "conceal",
11792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793#ifdef FEAT_CRYPT
11794 "cryptv",
11795#endif
11796#ifdef FEAT_CSCOPE
11797 "cscope",
11798#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011799#ifdef FEAT_CURSORBIND
11800 "cursorbind",
11801#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011802#ifdef CURSOR_SHAPE
11803 "cursorshape",
11804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805#ifdef DEBUG
11806 "debug",
11807#endif
11808#ifdef FEAT_CON_DIALOG
11809 "dialog_con",
11810#endif
11811#ifdef FEAT_GUI_DIALOG
11812 "dialog_gui",
11813#endif
11814#ifdef FEAT_DIFF
11815 "diff",
11816#endif
11817#ifdef FEAT_DIGRAPHS
11818 "digraphs",
11819#endif
11820#ifdef FEAT_DND
11821 "dnd",
11822#endif
11823#ifdef FEAT_EMACS_TAGS
11824 "emacs_tags",
11825#endif
11826 "eval", /* always present, of course! */
11827#ifdef FEAT_EX_EXTRA
11828 "ex_extra",
11829#endif
11830#ifdef FEAT_SEARCH_EXTRA
11831 "extra_search",
11832#endif
11833#ifdef FEAT_FKMAP
11834 "farsi",
11835#endif
11836#ifdef FEAT_SEARCHPATH
11837 "file_in_path",
11838#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011839#if defined(UNIX) && !defined(USE_SYSTEM)
11840 "filterpipe",
11841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842#ifdef FEAT_FIND_ID
11843 "find_in_path",
11844#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011845#ifdef FEAT_FLOAT
11846 "float",
11847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848#ifdef FEAT_FOLDING
11849 "folding",
11850#endif
11851#ifdef FEAT_FOOTER
11852 "footer",
11853#endif
11854#if !defined(USE_SYSTEM) && defined(UNIX)
11855 "fork",
11856#endif
11857#ifdef FEAT_GETTEXT
11858 "gettext",
11859#endif
11860#ifdef FEAT_GUI
11861 "gui",
11862#endif
11863#ifdef FEAT_GUI_ATHENA
11864# ifdef FEAT_GUI_NEXTAW
11865 "gui_neXtaw",
11866# else
11867 "gui_athena",
11868# endif
11869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870#ifdef FEAT_GUI_GTK
11871 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011874#ifdef FEAT_GUI_GNOME
11875 "gui_gnome",
11876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877#ifdef FEAT_GUI_MAC
11878 "gui_mac",
11879#endif
11880#ifdef FEAT_GUI_MOTIF
11881 "gui_motif",
11882#endif
11883#ifdef FEAT_GUI_PHOTON
11884 "gui_photon",
11885#endif
11886#ifdef FEAT_GUI_W16
11887 "gui_win16",
11888#endif
11889#ifdef FEAT_GUI_W32
11890 "gui_win32",
11891#endif
11892#ifdef FEAT_HANGULIN
11893 "hangul_input",
11894#endif
11895#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11896 "iconv",
11897#endif
11898#ifdef FEAT_INS_EXPAND
11899 "insert_expand",
11900#endif
11901#ifdef FEAT_JUMPLIST
11902 "jumplist",
11903#endif
11904#ifdef FEAT_KEYMAP
11905 "keymap",
11906#endif
11907#ifdef FEAT_LANGMAP
11908 "langmap",
11909#endif
11910#ifdef FEAT_LIBCALL
11911 "libcall",
11912#endif
11913#ifdef FEAT_LINEBREAK
11914 "linebreak",
11915#endif
11916#ifdef FEAT_LISP
11917 "lispindent",
11918#endif
11919#ifdef FEAT_LISTCMDS
11920 "listcmds",
11921#endif
11922#ifdef FEAT_LOCALMAP
11923 "localmap",
11924#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011925#ifdef FEAT_LUA
11926# ifndef DYNAMIC_LUA
11927 "lua",
11928# endif
11929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930#ifdef FEAT_MENU
11931 "menu",
11932#endif
11933#ifdef FEAT_SESSION
11934 "mksession",
11935#endif
11936#ifdef FEAT_MODIFY_FNAME
11937 "modify_fname",
11938#endif
11939#ifdef FEAT_MOUSE
11940 "mouse",
11941#endif
11942#ifdef FEAT_MOUSESHAPE
11943 "mouseshape",
11944#endif
11945#if defined(UNIX) || defined(VMS)
11946# ifdef FEAT_MOUSE_DEC
11947 "mouse_dec",
11948# endif
11949# ifdef FEAT_MOUSE_GPM
11950 "mouse_gpm",
11951# endif
11952# ifdef FEAT_MOUSE_JSB
11953 "mouse_jsbterm",
11954# endif
11955# ifdef FEAT_MOUSE_NET
11956 "mouse_netterm",
11957# endif
11958# ifdef FEAT_MOUSE_PTERM
11959 "mouse_pterm",
11960# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011961# ifdef FEAT_SYSMOUSE
11962 "mouse_sysmouse",
11963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964# ifdef FEAT_MOUSE_XTERM
11965 "mouse_xterm",
11966# endif
11967#endif
11968#ifdef FEAT_MBYTE
11969 "multi_byte",
11970#endif
11971#ifdef FEAT_MBYTE_IME
11972 "multi_byte_ime",
11973#endif
11974#ifdef FEAT_MULTI_LANG
11975 "multi_lang",
11976#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011977#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011978#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011979 "mzscheme",
11980#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011982#ifdef FEAT_OLE
11983 "ole",
11984#endif
11985#ifdef FEAT_OSFILETYPE
11986 "osfiletype",
11987#endif
11988#ifdef FEAT_PATH_EXTRA
11989 "path_extra",
11990#endif
11991#ifdef FEAT_PERL
11992#ifndef DYNAMIC_PERL
11993 "perl",
11994#endif
11995#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011996#ifdef FEAT_PERSISTENT_UNDO
11997 "persistent_undo",
11998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999#ifdef FEAT_PYTHON
12000#ifndef DYNAMIC_PYTHON
12001 "python",
12002#endif
12003#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012004#ifdef FEAT_PYTHON3
12005#ifndef DYNAMIC_PYTHON3
12006 "python3",
12007#endif
12008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009#ifdef FEAT_POSTSCRIPT
12010 "postscript",
12011#endif
12012#ifdef FEAT_PRINTER
12013 "printer",
12014#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012015#ifdef FEAT_PROFILE
12016 "profile",
12017#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012018#ifdef FEAT_RELTIME
12019 "reltime",
12020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021#ifdef FEAT_QUICKFIX
12022 "quickfix",
12023#endif
12024#ifdef FEAT_RIGHTLEFT
12025 "rightleft",
12026#endif
12027#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12028 "ruby",
12029#endif
12030#ifdef FEAT_SCROLLBIND
12031 "scrollbind",
12032#endif
12033#ifdef FEAT_CMDL_INFO
12034 "showcmd",
12035 "cmdline_info",
12036#endif
12037#ifdef FEAT_SIGNS
12038 "signs",
12039#endif
12040#ifdef FEAT_SMARTINDENT
12041 "smartindent",
12042#endif
12043#ifdef FEAT_SNIFF
12044 "sniff",
12045#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012046#ifdef STARTUPTIME
12047 "startuptime",
12048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049#ifdef FEAT_STL_OPT
12050 "statusline",
12051#endif
12052#ifdef FEAT_SUN_WORKSHOP
12053 "sun_workshop",
12054#endif
12055#ifdef FEAT_NETBEANS_INTG
12056 "netbeans_intg",
12057#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012058#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012059 "spell",
12060#endif
12061#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062 "syntax",
12063#endif
12064#if defined(USE_SYSTEM) || !defined(UNIX)
12065 "system",
12066#endif
12067#ifdef FEAT_TAG_BINS
12068 "tag_binary",
12069#endif
12070#ifdef FEAT_TAG_OLDSTATIC
12071 "tag_old_static",
12072#endif
12073#ifdef FEAT_TAG_ANYWHITE
12074 "tag_any_white",
12075#endif
12076#ifdef FEAT_TCL
12077# ifndef DYNAMIC_TCL
12078 "tcl",
12079# endif
12080#endif
12081#ifdef TERMINFO
12082 "terminfo",
12083#endif
12084#ifdef FEAT_TERMRESPONSE
12085 "termresponse",
12086#endif
12087#ifdef FEAT_TEXTOBJ
12088 "textobjects",
12089#endif
12090#ifdef HAVE_TGETENT
12091 "tgetent",
12092#endif
12093#ifdef FEAT_TITLE
12094 "title",
12095#endif
12096#ifdef FEAT_TOOLBAR
12097 "toolbar",
12098#endif
12099#ifdef FEAT_USR_CMDS
12100 "user-commands", /* was accidentally included in 5.4 */
12101 "user_commands",
12102#endif
12103#ifdef FEAT_VIMINFO
12104 "viminfo",
12105#endif
12106#ifdef FEAT_VERTSPLIT
12107 "vertsplit",
12108#endif
12109#ifdef FEAT_VIRTUALEDIT
12110 "virtualedit",
12111#endif
12112#ifdef FEAT_VISUAL
12113 "visual",
12114#endif
12115#ifdef FEAT_VISUALEXTRA
12116 "visualextra",
12117#endif
12118#ifdef FEAT_VREPLACE
12119 "vreplace",
12120#endif
12121#ifdef FEAT_WILDIGN
12122 "wildignore",
12123#endif
12124#ifdef FEAT_WILDMENU
12125 "wildmenu",
12126#endif
12127#ifdef FEAT_WINDOWS
12128 "windows",
12129#endif
12130#ifdef FEAT_WAK
12131 "winaltkeys",
12132#endif
12133#ifdef FEAT_WRITEBACKUP
12134 "writebackup",
12135#endif
12136#ifdef FEAT_XIM
12137 "xim",
12138#endif
12139#ifdef FEAT_XFONTSET
12140 "xfontset",
12141#endif
12142#ifdef USE_XSMP
12143 "xsmp",
12144#endif
12145#ifdef USE_XSMP_INTERACT
12146 "xsmp_interact",
12147#endif
12148#ifdef FEAT_XCLIPBOARD
12149 "xterm_clipboard",
12150#endif
12151#ifdef FEAT_XTERM_SAVE
12152 "xterm_save",
12153#endif
12154#if defined(UNIX) && defined(FEAT_X11)
12155 "X11",
12156#endif
12157 NULL
12158 };
12159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 for (i = 0; has_list[i] != NULL; ++i)
12162 if (STRICMP(name, has_list[i]) == 0)
12163 {
12164 n = TRUE;
12165 break;
12166 }
12167
12168 if (n == FALSE)
12169 {
12170 if (STRNICMP(name, "patch", 5) == 0)
12171 n = has_patch(atoi((char *)name + 5));
12172 else if (STRICMP(name, "vim_starting") == 0)
12173 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012174#ifdef FEAT_MBYTE
12175 else if (STRICMP(name, "multi_byte_encoding") == 0)
12176 n = has_mbyte;
12177#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012178#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12179 else if (STRICMP(name, "balloon_multiline") == 0)
12180 n = multiline_balloon_available();
12181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182#ifdef DYNAMIC_TCL
12183 else if (STRICMP(name, "tcl") == 0)
12184 n = tcl_enabled(FALSE);
12185#endif
12186#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12187 else if (STRICMP(name, "iconv") == 0)
12188 n = iconv_enabled(FALSE);
12189#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012190#ifdef DYNAMIC_LUA
12191 else if (STRICMP(name, "lua") == 0)
12192 n = lua_enabled(FALSE);
12193#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012194#ifdef DYNAMIC_MZSCHEME
12195 else if (STRICMP(name, "mzscheme") == 0)
12196 n = mzscheme_enabled(FALSE);
12197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198#ifdef DYNAMIC_RUBY
12199 else if (STRICMP(name, "ruby") == 0)
12200 n = ruby_enabled(FALSE);
12201#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012202#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203#ifdef DYNAMIC_PYTHON
12204 else if (STRICMP(name, "python") == 0)
12205 n = python_enabled(FALSE);
12206#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012207#endif
12208#ifdef FEAT_PYTHON3
12209#ifdef DYNAMIC_PYTHON3
12210 else if (STRICMP(name, "python3") == 0)
12211 n = python3_enabled(FALSE);
12212#endif
12213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214#ifdef DYNAMIC_PERL
12215 else if (STRICMP(name, "perl") == 0)
12216 n = perl_enabled(FALSE);
12217#endif
12218#ifdef FEAT_GUI
12219 else if (STRICMP(name, "gui_running") == 0)
12220 n = (gui.in_use || gui.starting);
12221# ifdef FEAT_GUI_W32
12222 else if (STRICMP(name, "gui_win32s") == 0)
12223 n = gui_is_win32s();
12224# endif
12225# ifdef FEAT_BROWSE
12226 else if (STRICMP(name, "browse") == 0)
12227 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12228# endif
12229#endif
12230#ifdef FEAT_SYN_HL
12231 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012232 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233#endif
12234#if defined(WIN3264)
12235 else if (STRICMP(name, "win95") == 0)
12236 n = mch_windows95();
12237#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012238#ifdef FEAT_NETBEANS_INTG
12239 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012240 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 }
12243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012244 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245}
12246
12247/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012248 * "has_key()" function
12249 */
12250 static void
12251f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012252 typval_T *argvars;
12253 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012254{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012255 if (argvars[0].v_type != VAR_DICT)
12256 {
12257 EMSG(_(e_dictreq));
12258 return;
12259 }
12260 if (argvars[0].vval.v_dict == NULL)
12261 return;
12262
12263 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012264 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012265}
12266
12267/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012268 * "haslocaldir()" function
12269 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012270 static void
12271f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012272 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012273 typval_T *rettv;
12274{
12275 rettv->vval.v_number = (curwin->w_localdir != NULL);
12276}
12277
12278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279 * "hasmapto()" function
12280 */
12281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012282f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012283 typval_T *argvars;
12284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285{
12286 char_u *name;
12287 char_u *mode;
12288 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012289 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012291 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012292 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 mode = (char_u *)"nvo";
12294 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012296 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012297 if (argvars[2].v_type != VAR_UNKNOWN)
12298 abbr = get_tv_number(&argvars[2]);
12299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300
Bram Moolenaar2c932302006-03-18 21:42:09 +000012301 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012302 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012304 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305}
12306
12307/*
12308 * "histadd()" function
12309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012311f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012312 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314{
12315#ifdef FEAT_CMDHIST
12316 int histype;
12317 char_u *str;
12318 char_u buf[NUMBUFLEN];
12319#endif
12320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012321 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 if (check_restricted() || check_secure())
12323 return;
12324#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012325 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12326 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327 if (histype >= 0)
12328 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 if (*str != NUL)
12331 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012332 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012334 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335 return;
12336 }
12337 }
12338#endif
12339}
12340
12341/*
12342 * "histdel()" function
12343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012345f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012346 typval_T *argvars UNUSED;
12347 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348{
12349#ifdef FEAT_CMDHIST
12350 int n;
12351 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012352 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012354 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12355 if (str == NULL)
12356 n = 0;
12357 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012359 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012360 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012362 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012363 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 else
12365 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012366 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012367 get_tv_string_buf(&argvars[1], buf));
12368 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369#endif
12370}
12371
12372/*
12373 * "histget()" function
12374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376f_histget(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#ifdef FEAT_CMDHIST
12381 int type;
12382 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012383 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012385 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12386 if (str == NULL)
12387 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012389 {
12390 type = get_histtype(str);
12391 if (argvars[1].v_type == VAR_UNKNOWN)
12392 idx = get_history_idx(type);
12393 else
12394 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12395 /* -1 on type error */
12396 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012399 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012401 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402}
12403
12404/*
12405 * "histnr()" function
12406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012408f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012409 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411{
12412 int i;
12413
12414#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012415 char_u *history = get_tv_string_chk(&argvars[0]);
12416
12417 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418 if (i >= HIST_CMD && i < HIST_COUNT)
12419 i = get_history_idx(i);
12420 else
12421#endif
12422 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012423 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424}
12425
12426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 * "highlightID(name)" function
12428 */
12429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012431 typval_T *argvars;
12432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012434 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435}
12436
12437/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012438 * "highlight_exists()" function
12439 */
12440 static void
12441f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012442 typval_T *argvars;
12443 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012444{
12445 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12446}
12447
12448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449 * "hostname()" function
12450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012452f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012453 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455{
12456 char_u hostname[256];
12457
12458 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012459 rettv->v_type = VAR_STRING;
12460 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461}
12462
12463/*
12464 * iconv() function
12465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012467f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012468 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470{
12471#ifdef FEAT_MBYTE
12472 char_u buf1[NUMBUFLEN];
12473 char_u buf2[NUMBUFLEN];
12474 char_u *from, *to, *str;
12475 vimconv_T vimconv;
12476#endif
12477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012478 rettv->v_type = VAR_STRING;
12479 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480
12481#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012482 str = get_tv_string(&argvars[0]);
12483 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12484 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485 vimconv.vc_type = CONV_NONE;
12486 convert_setup(&vimconv, from, to);
12487
12488 /* If the encodings are equal, no conversion needed. */
12489 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012492 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493
12494 convert_setup(&vimconv, NULL, NULL);
12495 vim_free(from);
12496 vim_free(to);
12497#endif
12498}
12499
12500/*
12501 * "indent()" function
12502 */
12503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012504f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012505 typval_T *argvars;
12506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507{
12508 linenr_T lnum;
12509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012510 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012514 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515}
12516
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012517/*
12518 * "index()" function
12519 */
12520 static void
12521f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012522 typval_T *argvars;
12523 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012524{
Bram Moolenaar33570922005-01-25 22:26:29 +000012525 list_T *l;
12526 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012527 long idx = 0;
12528 int ic = FALSE;
12529
12530 rettv->vval.v_number = -1;
12531 if (argvars[0].v_type != VAR_LIST)
12532 {
12533 EMSG(_(e_listreq));
12534 return;
12535 }
12536 l = argvars[0].vval.v_list;
12537 if (l != NULL)
12538 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012539 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012540 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012542 int error = FALSE;
12543
Bram Moolenaar758711c2005-02-02 23:11:38 +000012544 /* Start at specified item. Use the cached index that list_find()
12545 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012546 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012547 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012548 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012549 ic = get_tv_number_chk(&argvars[3], &error);
12550 if (error)
12551 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012552 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012553
Bram Moolenaar758711c2005-02-02 23:11:38 +000012554 for ( ; item != NULL; item = item->li_next, ++idx)
12555 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012556 {
12557 rettv->vval.v_number = idx;
12558 break;
12559 }
12560 }
12561}
12562
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563static int inputsecret_flag = 0;
12564
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012565static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12566
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012568 * This function is used by f_input() and f_inputdialog() functions. The third
12569 * argument to f_input() specifies the type of completion to use at the
12570 * prompt. The third argument to f_inputdialog() specifies the value to return
12571 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572 */
12573 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012574get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012575 typval_T *argvars;
12576 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012577 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012579 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580 char_u *p = NULL;
12581 int c;
12582 char_u buf[NUMBUFLEN];
12583 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012584 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012585 int xp_type = EXPAND_NOTHING;
12586 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012588 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012589 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590
12591#ifdef NO_CONSOLE_INPUT
12592 /* While starting up, there is no place to enter text. */
12593 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595#endif
12596
12597 cmd_silent = FALSE; /* Want to see the prompt. */
12598 if (prompt != NULL)
12599 {
12600 /* Only the part of the message after the last NL is considered as
12601 * prompt for the command line */
12602 p = vim_strrchr(prompt, '\n');
12603 if (p == NULL)
12604 p = prompt;
12605 else
12606 {
12607 ++p;
12608 c = *p;
12609 *p = NUL;
12610 msg_start();
12611 msg_clr_eos();
12612 msg_puts_attr(prompt, echo_attr);
12613 msg_didout = FALSE;
12614 msg_starthere();
12615 *p = c;
12616 }
12617 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012619 if (argvars[1].v_type != VAR_UNKNOWN)
12620 {
12621 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12622 if (defstr != NULL)
12623 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012625 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012626 {
12627 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012628 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012629 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012630
Bram Moolenaar4463f292005-09-25 22:20:24 +000012631 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012632
Bram Moolenaar4463f292005-09-25 22:20:24 +000012633 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12634 if (xp_name == NULL)
12635 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012636
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012637 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012638
Bram Moolenaar4463f292005-09-25 22:20:24 +000012639 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12640 &xp_arg) == FAIL)
12641 return;
12642 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012643 }
12644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012645 if (defstr != NULL)
12646 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012647 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12648 xp_type, xp_arg);
12649
12650 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012652 /* since the user typed this, no need to wait for return */
12653 need_wait_return = FALSE;
12654 msg_didout = FALSE;
12655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656 cmd_silent = cmd_silent_save;
12657}
12658
12659/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012660 * "input()" function
12661 * Also handles inputsecret() when inputsecret is set.
12662 */
12663 static void
12664f_input(argvars, rettv)
12665 typval_T *argvars;
12666 typval_T *rettv;
12667{
12668 get_user_input(argvars, rettv, FALSE);
12669}
12670
12671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 * "inputdialog()" function
12673 */
12674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012676 typval_T *argvars;
12677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678{
12679#if defined(FEAT_GUI_TEXTDIALOG)
12680 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12681 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12682 {
12683 char_u *message;
12684 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012685 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012687 message = get_tv_string_chk(&argvars[0]);
12688 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012689 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012690 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691 else
12692 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012693 if (message != NULL && defstr != NULL
12694 && do_dialog(VIM_QUESTION, NULL, message,
12695 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012696 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012697 else
12698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012699 if (message != NULL && defstr != NULL
12700 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012701 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012702 rettv->vval.v_string = vim_strsave(
12703 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012705 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012707 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 }
12709 else
12710#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012711 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712}
12713
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012714/*
12715 * "inputlist()" function
12716 */
12717 static void
12718f_inputlist(argvars, rettv)
12719 typval_T *argvars;
12720 typval_T *rettv;
12721{
12722 listitem_T *li;
12723 int selected;
12724 int mouse_used;
12725
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012726#ifdef NO_CONSOLE_INPUT
12727 /* While starting up, there is no place to enter text. */
12728 if (no_console_input())
12729 return;
12730#endif
12731 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12732 {
12733 EMSG2(_(e_listarg), "inputlist()");
12734 return;
12735 }
12736
12737 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012738 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012739 lines_left = Rows; /* avoid more prompt */
12740 msg_scroll = TRUE;
12741 msg_clr_eos();
12742
12743 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12744 {
12745 msg_puts(get_tv_string(&li->li_tv));
12746 msg_putchar('\n');
12747 }
12748
12749 /* Ask for choice. */
12750 selected = prompt_for_number(&mouse_used);
12751 if (mouse_used)
12752 selected -= lines_left;
12753
12754 rettv->vval.v_number = selected;
12755}
12756
12757
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12759
12760/*
12761 * "inputrestore()" function
12762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012765 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767{
12768 if (ga_userinput.ga_len > 0)
12769 {
12770 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12772 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012773 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 }
12775 else if (p_verbose > 1)
12776 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012777 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012778 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779 }
12780}
12781
12782/*
12783 * "inputsave()" function
12784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012787 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012790 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791 if (ga_grow(&ga_userinput, 1) == OK)
12792 {
12793 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12794 + ga_userinput.ga_len);
12795 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012796 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797 }
12798 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800}
12801
12802/*
12803 * "inputsecret()" function
12804 */
12805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012807 typval_T *argvars;
12808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809{
12810 ++cmdline_star;
12811 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012812 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 --cmdline_star;
12814 --inputsecret_flag;
12815}
12816
12817/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012818 * "insert()" function
12819 */
12820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012821f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012822 typval_T *argvars;
12823 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012824{
12825 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012826 listitem_T *item;
12827 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012828 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012829
12830 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012831 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012832 else if ((l = argvars[0].vval.v_list) != NULL
12833 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012834 {
12835 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012836 before = get_tv_number_chk(&argvars[2], &error);
12837 if (error)
12838 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012839
Bram Moolenaar758711c2005-02-02 23:11:38 +000012840 if (before == l->lv_len)
12841 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012842 else
12843 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012844 item = list_find(l, before);
12845 if (item == NULL)
12846 {
12847 EMSGN(_(e_listidx), before);
12848 l = NULL;
12849 }
12850 }
12851 if (l != NULL)
12852 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012853 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012854 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012855 }
12856 }
12857}
12858
12859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 * "isdirectory()" function
12861 */
12862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012864 typval_T *argvars;
12865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868}
12869
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012870/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012871 * "islocked()" function
12872 */
12873 static void
12874f_islocked(argvars, rettv)
12875 typval_T *argvars;
12876 typval_T *rettv;
12877{
12878 lval_T lv;
12879 char_u *end;
12880 dictitem_T *di;
12881
12882 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012883 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12884 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012885 if (end != NULL && lv.ll_name != NULL)
12886 {
12887 if (*end != NUL)
12888 EMSG(_(e_trailing));
12889 else
12890 {
12891 if (lv.ll_tv == NULL)
12892 {
12893 if (check_changedtick(lv.ll_name))
12894 rettv->vval.v_number = 1; /* always locked */
12895 else
12896 {
12897 di = find_var(lv.ll_name, NULL);
12898 if (di != NULL)
12899 {
12900 /* Consider a variable locked when:
12901 * 1. the variable itself is locked
12902 * 2. the value of the variable is locked.
12903 * 3. the List or Dict value is locked.
12904 */
12905 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12906 || tv_islocked(&di->di_tv));
12907 }
12908 }
12909 }
12910 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012911 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012912 else if (lv.ll_newkey != NULL)
12913 EMSG2(_(e_dictkey), lv.ll_newkey);
12914 else if (lv.ll_list != NULL)
12915 /* List item. */
12916 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12917 else
12918 /* Dictionary item. */
12919 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12920 }
12921 }
12922
12923 clear_lval(&lv);
12924}
12925
Bram Moolenaar33570922005-01-25 22:26:29 +000012926static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012927
12928/*
12929 * Turn a dict into a list:
12930 * "what" == 0: list of keys
12931 * "what" == 1: list of values
12932 * "what" == 2: list of items
12933 */
12934 static void
12935dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012936 typval_T *argvars;
12937 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012938 int what;
12939{
Bram Moolenaar33570922005-01-25 22:26:29 +000012940 list_T *l2;
12941 dictitem_T *di;
12942 hashitem_T *hi;
12943 listitem_T *li;
12944 listitem_T *li2;
12945 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012946 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012947
Bram Moolenaar8c711452005-01-14 21:53:12 +000012948 if (argvars[0].v_type != VAR_DICT)
12949 {
12950 EMSG(_(e_dictreq));
12951 return;
12952 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012953 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012954 return;
12955
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012956 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012957 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012958
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012959 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012960 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012961 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012962 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012963 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012964 --todo;
12965 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012966
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012967 li = listitem_alloc();
12968 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012969 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012970 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012971
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012972 if (what == 0)
12973 {
12974 /* keys() */
12975 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012976 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012977 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12978 }
12979 else if (what == 1)
12980 {
12981 /* values() */
12982 copy_tv(&di->di_tv, &li->li_tv);
12983 }
12984 else
12985 {
12986 /* items() */
12987 l2 = list_alloc();
12988 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012989 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012990 li->li_tv.vval.v_list = l2;
12991 if (l2 == NULL)
12992 break;
12993 ++l2->lv_refcount;
12994
12995 li2 = listitem_alloc();
12996 if (li2 == NULL)
12997 break;
12998 list_append(l2, li2);
12999 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013000 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013001 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13002
13003 li2 = listitem_alloc();
13004 if (li2 == NULL)
13005 break;
13006 list_append(l2, li2);
13007 copy_tv(&di->di_tv, &li2->li_tv);
13008 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013009 }
13010 }
13011}
13012
13013/*
13014 * "items(dict)" function
13015 */
13016 static void
13017f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013018 typval_T *argvars;
13019 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013020{
13021 dict_list(argvars, rettv, 2);
13022}
13023
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013025 * "join()" function
13026 */
13027 static void
13028f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013029 typval_T *argvars;
13030 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013031{
13032 garray_T ga;
13033 char_u *sep;
13034
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013035 if (argvars[0].v_type != VAR_LIST)
13036 {
13037 EMSG(_(e_listreq));
13038 return;
13039 }
13040 if (argvars[0].vval.v_list == NULL)
13041 return;
13042 if (argvars[1].v_type == VAR_UNKNOWN)
13043 sep = (char_u *)" ";
13044 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013045 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013046
13047 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013048
13049 if (sep != NULL)
13050 {
13051 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013052 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013053 ga_append(&ga, NUL);
13054 rettv->vval.v_string = (char_u *)ga.ga_data;
13055 }
13056 else
13057 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013058}
13059
13060/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013061 * "keys()" function
13062 */
13063 static void
13064f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013065 typval_T *argvars;
13066 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013067{
13068 dict_list(argvars, rettv, 0);
13069}
13070
13071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072 * "last_buffer_nr()" function.
13073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013075f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013076 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078{
13079 int n = 0;
13080 buf_T *buf;
13081
13082 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13083 if (n < buf->b_fnum)
13084 n = buf->b_fnum;
13085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013086 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013087}
13088
13089/*
13090 * "len()" function
13091 */
13092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013093f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013094 typval_T *argvars;
13095 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013096{
13097 switch (argvars[0].v_type)
13098 {
13099 case VAR_STRING:
13100 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013101 rettv->vval.v_number = (varnumber_T)STRLEN(
13102 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013103 break;
13104 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013105 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013106 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013107 case VAR_DICT:
13108 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13109 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013110 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013111 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013112 break;
13113 }
13114}
13115
Bram Moolenaar33570922005-01-25 22:26:29 +000013116static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013117
13118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013119libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013120 typval_T *argvars;
13121 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013122 int type;
13123{
13124#ifdef FEAT_LIBCALL
13125 char_u *string_in;
13126 char_u **string_result;
13127 int nr_result;
13128#endif
13129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013131 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013132 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013133
13134 if (check_restricted() || check_secure())
13135 return;
13136
13137#ifdef FEAT_LIBCALL
13138 /* The first two args must be strings, otherwise its meaningless */
13139 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13140 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013141 string_in = NULL;
13142 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013143 string_in = argvars[2].vval.v_string;
13144 if (type == VAR_NUMBER)
13145 string_result = NULL;
13146 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013148 if (mch_libcall(argvars[0].vval.v_string,
13149 argvars[1].vval.v_string,
13150 string_in,
13151 argvars[2].vval.v_number,
13152 string_result,
13153 &nr_result) == OK
13154 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013155 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013156 }
13157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158}
13159
13160/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013161 * "libcall()" function
13162 */
13163 static void
13164f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013165 typval_T *argvars;
13166 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013167{
13168 libcall_common(argvars, rettv, VAR_STRING);
13169}
13170
13171/*
13172 * "libcallnr()" function
13173 */
13174 static void
13175f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013176 typval_T *argvars;
13177 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013178{
13179 libcall_common(argvars, rettv, VAR_NUMBER);
13180}
13181
13182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183 * "line(string)" function
13184 */
13185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013186f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013187 typval_T *argvars;
13188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189{
13190 linenr_T lnum = 0;
13191 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013192 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013194 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195 if (fp != NULL)
13196 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013197 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198}
13199
13200/*
13201 * "line2byte(lnum)" function
13202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013204f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013205 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013206 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207{
13208#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210#else
13211 linenr_T lnum;
13212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013215 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013217 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13218 if (rettv->vval.v_number >= 0)
13219 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220#endif
13221}
13222
13223/*
13224 * "lispindent(lnum)" function
13225 */
13226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013227f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013228 typval_T *argvars;
13229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230{
13231#ifdef FEAT_LISP
13232 pos_T pos;
13233 linenr_T lnum;
13234
13235 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13238 {
13239 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013240 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241 curwin->w_cursor = pos;
13242 }
13243 else
13244#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013245 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246}
13247
13248/*
13249 * "localtime()" function
13250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013253 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013256 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257}
13258
Bram Moolenaar33570922005-01-25 22:26:29 +000013259static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260
13261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013262get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013263 typval_T *argvars;
13264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 int exact;
13266{
13267 char_u *keys;
13268 char_u *which;
13269 char_u buf[NUMBUFLEN];
13270 char_u *keys_buf = NULL;
13271 char_u *rhs;
13272 int mode;
13273 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013274 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275
13276 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277 rettv->v_type = VAR_STRING;
13278 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013280 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281 if (*keys == NUL)
13282 return;
13283
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013284 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013286 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013287 if (argvars[2].v_type != VAR_UNKNOWN)
13288 abbr = get_tv_number(&argvars[2]);
13289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 else
13291 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013292 if (which == NULL)
13293 return;
13294
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 mode = get_map_mode(&which, 0);
13296
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013297 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013298 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299 vim_free(keys_buf);
13300 if (rhs != NULL)
13301 {
13302 ga_init(&ga);
13303 ga.ga_itemsize = 1;
13304 ga.ga_growsize = 40;
13305
13306 while (*rhs != NUL)
13307 ga_concat(&ga, str2special(&rhs, FALSE));
13308
13309 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013310 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311 }
13312}
13313
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013314#ifdef FEAT_FLOAT
13315/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013316 * "log()" function
13317 */
13318 static void
13319f_log(argvars, rettv)
13320 typval_T *argvars;
13321 typval_T *rettv;
13322{
13323 float_T f;
13324
13325 rettv->v_type = VAR_FLOAT;
13326 if (get_float_arg(argvars, &f) == OK)
13327 rettv->vval.v_float = log(f);
13328 else
13329 rettv->vval.v_float = 0.0;
13330}
13331
13332/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013333 * "log10()" function
13334 */
13335 static void
13336f_log10(argvars, rettv)
13337 typval_T *argvars;
13338 typval_T *rettv;
13339{
13340 float_T f;
13341
13342 rettv->v_type = VAR_FLOAT;
13343 if (get_float_arg(argvars, &f) == OK)
13344 rettv->vval.v_float = log10(f);
13345 else
13346 rettv->vval.v_float = 0.0;
13347}
13348#endif
13349
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013351 * "map()" function
13352 */
13353 static void
13354f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013355 typval_T *argvars;
13356 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013357{
13358 filter_map(argvars, rettv, TRUE);
13359}
13360
13361/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013362 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013363 */
13364 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013365f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013366 typval_T *argvars;
13367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013369 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370}
13371
13372/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013373 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374 */
13375 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013376f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013377 typval_T *argvars;
13378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013380 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381}
13382
Bram Moolenaar33570922005-01-25 22:26:29 +000013383static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384
13385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013386find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013387 typval_T *argvars;
13388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389 int type;
13390{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013391 char_u *str = NULL;
13392 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393 char_u *pat;
13394 regmatch_T regmatch;
13395 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013396 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397 char_u *save_cpo;
13398 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013399 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013400 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013401 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013402 list_T *l = NULL;
13403 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013404 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013405 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406
13407 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13408 save_cpo = p_cpo;
13409 p_cpo = (char_u *)"";
13410
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013411 rettv->vval.v_number = -1;
13412 if (type == 3)
13413 {
13414 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013415 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013416 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013417 }
13418 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013420 rettv->v_type = VAR_STRING;
13421 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013424 if (argvars[0].v_type == VAR_LIST)
13425 {
13426 if ((l = argvars[0].vval.v_list) == NULL)
13427 goto theend;
13428 li = l->lv_first;
13429 }
13430 else
13431 expr = str = get_tv_string(&argvars[0]);
13432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013433 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13434 if (pat == NULL)
13435 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013436
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013437 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013439 int error = FALSE;
13440
13441 start = get_tv_number_chk(&argvars[2], &error);
13442 if (error)
13443 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013444 if (l != NULL)
13445 {
13446 li = list_find(l, start);
13447 if (li == NULL)
13448 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013449 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013450 }
13451 else
13452 {
13453 if (start < 0)
13454 start = 0;
13455 if (start > (long)STRLEN(str))
13456 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013457 /* When "count" argument is there ignore matches before "start",
13458 * otherwise skip part of the string. Differs when pattern is "^"
13459 * or "\<". */
13460 if (argvars[3].v_type != VAR_UNKNOWN)
13461 startcol = start;
13462 else
13463 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013464 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013465
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013466 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013467 nth = get_tv_number_chk(&argvars[3], &error);
13468 if (error)
13469 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 }
13471
13472 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13473 if (regmatch.regprog != NULL)
13474 {
13475 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013476
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013477 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013478 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013479 if (l != NULL)
13480 {
13481 if (li == NULL)
13482 {
13483 match = FALSE;
13484 break;
13485 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013486 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013487 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013488 if (str == NULL)
13489 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013490 }
13491
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013492 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013493
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013494 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013495 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013496 if (l == NULL && !match)
13497 break;
13498
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013499 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013500 if (l != NULL)
13501 {
13502 li = li->li_next;
13503 ++idx;
13504 }
13505 else
13506 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013507#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013508 startcol = (colnr_T)(regmatch.startp[0]
13509 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013510#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013511 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013512#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013513 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013514 }
13515
13516 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013518 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013519 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013520 int i;
13521
13522 /* return list with matched string and submatches */
13523 for (i = 0; i < NSUBEXP; ++i)
13524 {
13525 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013526 {
13527 if (list_append_string(rettv->vval.v_list,
13528 (char_u *)"", 0) == FAIL)
13529 break;
13530 }
13531 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013532 regmatch.startp[i],
13533 (int)(regmatch.endp[i] - regmatch.startp[i]))
13534 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013535 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013536 }
13537 }
13538 else if (type == 2)
13539 {
13540 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013541 if (l != NULL)
13542 copy_tv(&li->li_tv, rettv);
13543 else
13544 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013546 }
13547 else if (l != NULL)
13548 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 else
13550 {
13551 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013552 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 (varnumber_T)(regmatch.startp[0] - str);
13554 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013557 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558 }
13559 }
13560 vim_free(regmatch.regprog);
13561 }
13562
13563theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013564 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 p_cpo = save_cpo;
13566}
13567
13568/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013569 * "match()" function
13570 */
13571 static void
13572f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013573 typval_T *argvars;
13574 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013575{
13576 find_some_match(argvars, rettv, 1);
13577}
13578
13579/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013580 * "matchadd()" function
13581 */
13582 static void
13583f_matchadd(argvars, rettv)
13584 typval_T *argvars;
13585 typval_T *rettv;
13586{
13587#ifdef FEAT_SEARCH_EXTRA
13588 char_u buf[NUMBUFLEN];
13589 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13590 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13591 int prio = 10; /* default priority */
13592 int id = -1;
13593 int error = FALSE;
13594
13595 rettv->vval.v_number = -1;
13596
13597 if (grp == NULL || pat == NULL)
13598 return;
13599 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013600 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013601 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013602 if (argvars[3].v_type != VAR_UNKNOWN)
13603 id = get_tv_number_chk(&argvars[3], &error);
13604 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013605 if (error == TRUE)
13606 return;
13607 if (id >= 1 && id <= 3)
13608 {
13609 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13610 return;
13611 }
13612
13613 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13614#endif
13615}
13616
13617/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013618 * "matcharg()" function
13619 */
13620 static void
13621f_matcharg(argvars, rettv)
13622 typval_T *argvars;
13623 typval_T *rettv;
13624{
13625 if (rettv_list_alloc(rettv) == OK)
13626 {
13627#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013628 int id = get_tv_number(&argvars[0]);
13629 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013630
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013631 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013632 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013633 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13634 {
13635 list_append_string(rettv->vval.v_list,
13636 syn_id2name(m->hlg_id), -1);
13637 list_append_string(rettv->vval.v_list, m->pattern, -1);
13638 }
13639 else
13640 {
13641 list_append_string(rettv->vval.v_list, NUL, -1);
13642 list_append_string(rettv->vval.v_list, NUL, -1);
13643 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013644 }
13645#endif
13646 }
13647}
13648
13649/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013650 * "matchdelete()" function
13651 */
13652 static void
13653f_matchdelete(argvars, rettv)
13654 typval_T *argvars;
13655 typval_T *rettv;
13656{
13657#ifdef FEAT_SEARCH_EXTRA
13658 rettv->vval.v_number = match_delete(curwin,
13659 (int)get_tv_number(&argvars[0]), TRUE);
13660#endif
13661}
13662
13663/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013664 * "matchend()" function
13665 */
13666 static void
13667f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013668 typval_T *argvars;
13669 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013670{
13671 find_some_match(argvars, rettv, 0);
13672}
13673
13674/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013675 * "matchlist()" function
13676 */
13677 static void
13678f_matchlist(argvars, rettv)
13679 typval_T *argvars;
13680 typval_T *rettv;
13681{
13682 find_some_match(argvars, rettv, 3);
13683}
13684
13685/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013686 * "matchstr()" function
13687 */
13688 static void
13689f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013690 typval_T *argvars;
13691 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013692{
13693 find_some_match(argvars, rettv, 2);
13694}
13695
Bram Moolenaar33570922005-01-25 22:26:29 +000013696static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013697
13698 static void
13699max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013700 typval_T *argvars;
13701 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013702 int domax;
13703{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013704 long n = 0;
13705 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013707
13708 if (argvars[0].v_type == VAR_LIST)
13709 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013710 list_T *l;
13711 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013712
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013713 l = argvars[0].vval.v_list;
13714 if (l != NULL)
13715 {
13716 li = l->lv_first;
13717 if (li != NULL)
13718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013720 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013721 {
13722 li = li->li_next;
13723 if (li == NULL)
13724 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013725 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013726 if (domax ? i > n : i < n)
13727 n = i;
13728 }
13729 }
13730 }
13731 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013732 else if (argvars[0].v_type == VAR_DICT)
13733 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013734 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013735 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013736 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013737 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013738
13739 d = argvars[0].vval.v_dict;
13740 if (d != NULL)
13741 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013742 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013743 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013744 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013745 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013746 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013747 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013748 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013749 if (first)
13750 {
13751 n = i;
13752 first = FALSE;
13753 }
13754 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013755 n = i;
13756 }
13757 }
13758 }
13759 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013760 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013761 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013762 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013763}
13764
13765/*
13766 * "max()" function
13767 */
13768 static void
13769f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013770 typval_T *argvars;
13771 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013772{
13773 max_min(argvars, rettv, TRUE);
13774}
13775
13776/*
13777 * "min()" function
13778 */
13779 static void
13780f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013781 typval_T *argvars;
13782 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013783{
13784 max_min(argvars, rettv, FALSE);
13785}
13786
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013787static int mkdir_recurse __ARGS((char_u *dir, int prot));
13788
13789/*
13790 * Create the directory in which "dir" is located, and higher levels when
13791 * needed.
13792 */
13793 static int
13794mkdir_recurse(dir, prot)
13795 char_u *dir;
13796 int prot;
13797{
13798 char_u *p;
13799 char_u *updir;
13800 int r = FAIL;
13801
13802 /* Get end of directory name in "dir".
13803 * We're done when it's "/" or "c:/". */
13804 p = gettail_sep(dir);
13805 if (p <= get_past_head(dir))
13806 return OK;
13807
13808 /* If the directory exists we're done. Otherwise: create it.*/
13809 updir = vim_strnsave(dir, (int)(p - dir));
13810 if (updir == NULL)
13811 return FAIL;
13812 if (mch_isdir(updir))
13813 r = OK;
13814 else if (mkdir_recurse(updir, prot) == OK)
13815 r = vim_mkdir_emsg(updir, prot);
13816 vim_free(updir);
13817 return r;
13818}
13819
13820#ifdef vim_mkdir
13821/*
13822 * "mkdir()" function
13823 */
13824 static void
13825f_mkdir(argvars, rettv)
13826 typval_T *argvars;
13827 typval_T *rettv;
13828{
13829 char_u *dir;
13830 char_u buf[NUMBUFLEN];
13831 int prot = 0755;
13832
13833 rettv->vval.v_number = FAIL;
13834 if (check_restricted() || check_secure())
13835 return;
13836
13837 dir = get_tv_string_buf(&argvars[0], buf);
13838 if (argvars[1].v_type != VAR_UNKNOWN)
13839 {
13840 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013841 prot = get_tv_number_chk(&argvars[2], NULL);
13842 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013843 mkdir_recurse(dir, prot);
13844 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013845 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013846}
13847#endif
13848
Bram Moolenaar0d660222005-01-07 21:51:51 +000013849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 * "mode()" function
13851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013853f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013854 typval_T *argvars;
13855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013857 char_u buf[3];
13858
13859 buf[1] = NUL;
13860 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861
13862#ifdef FEAT_VISUAL
13863 if (VIsual_active)
13864 {
13865 if (VIsual_select)
13866 buf[0] = VIsual_mode + 's' - 'v';
13867 else
13868 buf[0] = VIsual_mode;
13869 }
13870 else
13871#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013872 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13873 || State == CONFIRM)
13874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013876 if (State == ASKMORE)
13877 buf[1] = 'm';
13878 else if (State == CONFIRM)
13879 buf[1] = '?';
13880 }
13881 else if (State == EXTERNCMD)
13882 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 else if (State & INSERT)
13884 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013885#ifdef FEAT_VREPLACE
13886 if (State & VREPLACE_FLAG)
13887 {
13888 buf[0] = 'R';
13889 buf[1] = 'v';
13890 }
13891 else
13892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893 if (State & REPLACE_FLAG)
13894 buf[0] = 'R';
13895 else
13896 buf[0] = 'i';
13897 }
13898 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013901 if (exmode_active)
13902 buf[1] = 'v';
13903 }
13904 else if (exmode_active)
13905 {
13906 buf[0] = 'c';
13907 buf[1] = 'e';
13908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013912 if (finish_op)
13913 buf[1] = 'o';
13914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013916 /* Clear out the minor mode when the argument is not a non-zero number or
13917 * non-empty string. */
13918 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013919 buf[1] = NUL;
13920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013921 rettv->vval.v_string = vim_strsave(buf);
13922 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923}
13924
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013925#ifdef FEAT_MZSCHEME
13926/*
13927 * "mzeval()" function
13928 */
13929 static void
13930f_mzeval(argvars, rettv)
13931 typval_T *argvars;
13932 typval_T *rettv;
13933{
13934 char_u *str;
13935 char_u buf[NUMBUFLEN];
13936
13937 str = get_tv_string_buf(&argvars[0], buf);
13938 do_mzeval(str, rettv);
13939}
13940#endif
13941
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013943 * "nextnonblank()" function
13944 */
13945 static void
13946f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013947 typval_T *argvars;
13948 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013949{
13950 linenr_T lnum;
13951
13952 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013954 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013955 {
13956 lnum = 0;
13957 break;
13958 }
13959 if (*skipwhite(ml_get(lnum)) != NUL)
13960 break;
13961 }
13962 rettv->vval.v_number = lnum;
13963}
13964
13965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966 * "nr2char()" function
13967 */
13968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013969f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013970 typval_T *argvars;
13971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972{
13973 char_u buf[NUMBUFLEN];
13974
13975#ifdef FEAT_MBYTE
13976 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013977 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 else
13979#endif
13980 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013981 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 buf[1] = NUL;
13983 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013984 rettv->v_type = VAR_STRING;
13985 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013986}
13987
13988/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013989 * "pathshorten()" function
13990 */
13991 static void
13992f_pathshorten(argvars, rettv)
13993 typval_T *argvars;
13994 typval_T *rettv;
13995{
13996 char_u *p;
13997
13998 rettv->v_type = VAR_STRING;
13999 p = get_tv_string_chk(&argvars[0]);
14000 if (p == NULL)
14001 rettv->vval.v_string = NULL;
14002 else
14003 {
14004 p = vim_strsave(p);
14005 rettv->vval.v_string = p;
14006 if (p != NULL)
14007 shorten_dir(p);
14008 }
14009}
14010
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014011#ifdef FEAT_FLOAT
14012/*
14013 * "pow()" function
14014 */
14015 static void
14016f_pow(argvars, rettv)
14017 typval_T *argvars;
14018 typval_T *rettv;
14019{
14020 float_T fx, fy;
14021
14022 rettv->v_type = VAR_FLOAT;
14023 if (get_float_arg(argvars, &fx) == OK
14024 && get_float_arg(&argvars[1], &fy) == OK)
14025 rettv->vval.v_float = pow(fx, fy);
14026 else
14027 rettv->vval.v_float = 0.0;
14028}
14029#endif
14030
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014031/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014032 * "prevnonblank()" function
14033 */
14034 static void
14035f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014036 typval_T *argvars;
14037 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014038{
14039 linenr_T lnum;
14040
14041 lnum = get_tv_lnum(argvars);
14042 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14043 lnum = 0;
14044 else
14045 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14046 --lnum;
14047 rettv->vval.v_number = lnum;
14048}
14049
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014050#ifdef HAVE_STDARG_H
14051/* This dummy va_list is here because:
14052 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14053 * - locally in the function results in a "used before set" warning
14054 * - using va_start() to initialize it gives "function with fixed args" error */
14055static va_list ap;
14056#endif
14057
Bram Moolenaar8c711452005-01-14 21:53:12 +000014058/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014059 * "printf()" function
14060 */
14061 static void
14062f_printf(argvars, rettv)
14063 typval_T *argvars;
14064 typval_T *rettv;
14065{
14066 rettv->v_type = VAR_STRING;
14067 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014068#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014069 {
14070 char_u buf[NUMBUFLEN];
14071 int len;
14072 char_u *s;
14073 int saved_did_emsg = did_emsg;
14074 char *fmt;
14075
14076 /* Get the required length, allocate the buffer and do it for real. */
14077 did_emsg = FALSE;
14078 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014079 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014080 if (!did_emsg)
14081 {
14082 s = alloc(len + 1);
14083 if (s != NULL)
14084 {
14085 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014086 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014087 }
14088 }
14089 did_emsg |= saved_did_emsg;
14090 }
14091#endif
14092}
14093
14094/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014095 * "pumvisible()" function
14096 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014097 static void
14098f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014099 typval_T *argvars UNUSED;
14100 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014101{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014102#ifdef FEAT_INS_EXPAND
14103 if (pum_visible())
14104 rettv->vval.v_number = 1;
14105#endif
14106}
14107
14108/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014109 * "range()" function
14110 */
14111 static void
14112f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014113 typval_T *argvars;
14114 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014115{
14116 long start;
14117 long end;
14118 long stride = 1;
14119 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014120 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014122 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014123 if (argvars[1].v_type == VAR_UNKNOWN)
14124 {
14125 end = start - 1;
14126 start = 0;
14127 }
14128 else
14129 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014130 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014131 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014132 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014133 }
14134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014135 if (error)
14136 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014137 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014138 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014139 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014140 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014141 else
14142 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014143 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014144 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014145 if (list_append_number(rettv->vval.v_list,
14146 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014147 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014148 }
14149}
14150
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014151/*
14152 * "readfile()" function
14153 */
14154 static void
14155f_readfile(argvars, rettv)
14156 typval_T *argvars;
14157 typval_T *rettv;
14158{
14159 int binary = FALSE;
14160 char_u *fname;
14161 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014162 listitem_T *li;
14163#define FREAD_SIZE 200 /* optimized for text lines */
14164 char_u buf[FREAD_SIZE];
14165 int readlen; /* size of last fread() */
14166 int buflen; /* nr of valid chars in buf[] */
14167 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14168 int tolist; /* first byte in buf[] still to be put in list */
14169 int chop; /* how many CR to chop off */
14170 char_u *prev = NULL; /* previously read bytes, if any */
14171 int prevlen = 0; /* length of "prev" if not NULL */
14172 char_u *s;
14173 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014174 long maxline = MAXLNUM;
14175 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014176
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014177 if (argvars[1].v_type != VAR_UNKNOWN)
14178 {
14179 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14180 binary = TRUE;
14181 if (argvars[2].v_type != VAR_UNKNOWN)
14182 maxline = get_tv_number(&argvars[2]);
14183 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014184
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014185 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014186 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014187
14188 /* Always open the file in binary mode, library functions have a mind of
14189 * their own about CR-LF conversion. */
14190 fname = get_tv_string(&argvars[0]);
14191 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14192 {
14193 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14194 return;
14195 }
14196
14197 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014198 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014199 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014200 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014201 buflen = filtd + readlen;
14202 tolist = 0;
14203 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14204 {
14205 if (buf[filtd] == '\n' || readlen <= 0)
14206 {
14207 /* Only when in binary mode add an empty list item when the
14208 * last line ends in a '\n'. */
14209 if (!binary && readlen == 0 && filtd == 0)
14210 break;
14211
14212 /* Found end-of-line or end-of-file: add a text line to the
14213 * list. */
14214 chop = 0;
14215 if (!binary)
14216 while (filtd - chop - 1 >= tolist
14217 && buf[filtd - chop - 1] == '\r')
14218 ++chop;
14219 len = filtd - tolist - chop;
14220 if (prev == NULL)
14221 s = vim_strnsave(buf + tolist, len);
14222 else
14223 {
14224 s = alloc((unsigned)(prevlen + len + 1));
14225 if (s != NULL)
14226 {
14227 mch_memmove(s, prev, prevlen);
14228 vim_free(prev);
14229 prev = NULL;
14230 mch_memmove(s + prevlen, buf + tolist, len);
14231 s[prevlen + len] = NUL;
14232 }
14233 }
14234 tolist = filtd + 1;
14235
14236 li = listitem_alloc();
14237 if (li == NULL)
14238 {
14239 vim_free(s);
14240 break;
14241 }
14242 li->li_tv.v_type = VAR_STRING;
14243 li->li_tv.v_lock = 0;
14244 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014245 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014246
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014247 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014248 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014249 if (readlen <= 0)
14250 break;
14251 }
14252 else if (buf[filtd] == NUL)
14253 buf[filtd] = '\n';
14254 }
14255 if (readlen <= 0)
14256 break;
14257
14258 if (tolist == 0)
14259 {
14260 /* "buf" is full, need to move text to an allocated buffer */
14261 if (prev == NULL)
14262 {
14263 prev = vim_strnsave(buf, buflen);
14264 prevlen = buflen;
14265 }
14266 else
14267 {
14268 s = alloc((unsigned)(prevlen + buflen));
14269 if (s != NULL)
14270 {
14271 mch_memmove(s, prev, prevlen);
14272 mch_memmove(s + prevlen, buf, buflen);
14273 vim_free(prev);
14274 prev = s;
14275 prevlen += buflen;
14276 }
14277 }
14278 filtd = 0;
14279 }
14280 else
14281 {
14282 mch_memmove(buf, buf + tolist, buflen - tolist);
14283 filtd -= tolist;
14284 }
14285 }
14286
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014287 /*
14288 * For a negative line count use only the lines at the end of the file,
14289 * free the rest.
14290 */
14291 if (maxline < 0)
14292 while (cnt > -maxline)
14293 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014294 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014295 --cnt;
14296 }
14297
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014298 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014299 fclose(fd);
14300}
14301
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014302#if defined(FEAT_RELTIME)
14303static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14304
14305/*
14306 * Convert a List to proftime_T.
14307 * Return FAIL when there is something wrong.
14308 */
14309 static int
14310list2proftime(arg, tm)
14311 typval_T *arg;
14312 proftime_T *tm;
14313{
14314 long n1, n2;
14315 int error = FALSE;
14316
14317 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14318 || arg->vval.v_list->lv_len != 2)
14319 return FAIL;
14320 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14321 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14322# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014323 tm->HighPart = n1;
14324 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014325# else
14326 tm->tv_sec = n1;
14327 tm->tv_usec = n2;
14328# endif
14329 return error ? FAIL : OK;
14330}
14331#endif /* FEAT_RELTIME */
14332
14333/*
14334 * "reltime()" function
14335 */
14336 static void
14337f_reltime(argvars, rettv)
14338 typval_T *argvars;
14339 typval_T *rettv;
14340{
14341#ifdef FEAT_RELTIME
14342 proftime_T res;
14343 proftime_T start;
14344
14345 if (argvars[0].v_type == VAR_UNKNOWN)
14346 {
14347 /* No arguments: get current time. */
14348 profile_start(&res);
14349 }
14350 else if (argvars[1].v_type == VAR_UNKNOWN)
14351 {
14352 if (list2proftime(&argvars[0], &res) == FAIL)
14353 return;
14354 profile_end(&res);
14355 }
14356 else
14357 {
14358 /* Two arguments: compute the difference. */
14359 if (list2proftime(&argvars[0], &start) == FAIL
14360 || list2proftime(&argvars[1], &res) == FAIL)
14361 return;
14362 profile_sub(&res, &start);
14363 }
14364
14365 if (rettv_list_alloc(rettv) == OK)
14366 {
14367 long n1, n2;
14368
14369# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014370 n1 = res.HighPart;
14371 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014372# else
14373 n1 = res.tv_sec;
14374 n2 = res.tv_usec;
14375# endif
14376 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14377 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14378 }
14379#endif
14380}
14381
14382/*
14383 * "reltimestr()" function
14384 */
14385 static void
14386f_reltimestr(argvars, rettv)
14387 typval_T *argvars;
14388 typval_T *rettv;
14389{
14390#ifdef FEAT_RELTIME
14391 proftime_T tm;
14392#endif
14393
14394 rettv->v_type = VAR_STRING;
14395 rettv->vval.v_string = NULL;
14396#ifdef FEAT_RELTIME
14397 if (list2proftime(&argvars[0], &tm) == OK)
14398 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14399#endif
14400}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014401
Bram Moolenaar0d660222005-01-07 21:51:51 +000014402#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14403static void make_connection __ARGS((void));
14404static int check_connection __ARGS((void));
14405
14406 static void
14407make_connection()
14408{
14409 if (X_DISPLAY == NULL
14410# ifdef FEAT_GUI
14411 && !gui.in_use
14412# endif
14413 )
14414 {
14415 x_force_connect = TRUE;
14416 setup_term_clip();
14417 x_force_connect = FALSE;
14418 }
14419}
14420
14421 static int
14422check_connection()
14423{
14424 make_connection();
14425 if (X_DISPLAY == NULL)
14426 {
14427 EMSG(_("E240: No connection to Vim server"));
14428 return FAIL;
14429 }
14430 return OK;
14431}
14432#endif
14433
14434#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014435static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014436
14437 static void
14438remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014439 typval_T *argvars;
14440 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014441 int expr;
14442{
14443 char_u *server_name;
14444 char_u *keys;
14445 char_u *r = NULL;
14446 char_u buf[NUMBUFLEN];
14447# ifdef WIN32
14448 HWND w;
14449# else
14450 Window w;
14451# endif
14452
14453 if (check_restricted() || check_secure())
14454 return;
14455
14456# ifdef FEAT_X11
14457 if (check_connection() == FAIL)
14458 return;
14459# endif
14460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014461 server_name = get_tv_string_chk(&argvars[0]);
14462 if (server_name == NULL)
14463 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014464 keys = get_tv_string_buf(&argvars[1], buf);
14465# ifdef WIN32
14466 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14467# else
14468 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14469 < 0)
14470# endif
14471 {
14472 if (r != NULL)
14473 EMSG(r); /* sending worked but evaluation failed */
14474 else
14475 EMSG2(_("E241: Unable to send to %s"), server_name);
14476 return;
14477 }
14478
14479 rettv->vval.v_string = r;
14480
14481 if (argvars[2].v_type != VAR_UNKNOWN)
14482 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014483 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014484 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014485 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014486
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014487 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014488 v.di_tv.v_type = VAR_STRING;
14489 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014490 idvar = get_tv_string_chk(&argvars[2]);
14491 if (idvar != NULL)
14492 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014493 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014494 }
14495}
14496#endif
14497
14498/*
14499 * "remote_expr()" function
14500 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014501 static void
14502f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014503 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014504 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014505{
14506 rettv->v_type = VAR_STRING;
14507 rettv->vval.v_string = NULL;
14508#ifdef FEAT_CLIENTSERVER
14509 remote_common(argvars, rettv, TRUE);
14510#endif
14511}
14512
14513/*
14514 * "remote_foreground()" function
14515 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014516 static void
14517f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014518 typval_T *argvars UNUSED;
14519 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014520{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014521#ifdef FEAT_CLIENTSERVER
14522# ifdef WIN32
14523 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014524 {
14525 char_u *server_name = get_tv_string_chk(&argvars[0]);
14526
14527 if (server_name != NULL)
14528 serverForeground(server_name);
14529 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014530# else
14531 /* Send a foreground() expression to the server. */
14532 argvars[1].v_type = VAR_STRING;
14533 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14534 argvars[2].v_type = VAR_UNKNOWN;
14535 remote_common(argvars, rettv, TRUE);
14536 vim_free(argvars[1].vval.v_string);
14537# endif
14538#endif
14539}
14540
Bram Moolenaar0d660222005-01-07 21:51:51 +000014541 static void
14542f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014543 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014544 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014545{
14546#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014547 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014548 char_u *s = NULL;
14549# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014550 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014551# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014552 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014553
14554 if (check_restricted() || check_secure())
14555 {
14556 rettv->vval.v_number = -1;
14557 return;
14558 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014559 serverid = get_tv_string_chk(&argvars[0]);
14560 if (serverid == NULL)
14561 {
14562 rettv->vval.v_number = -1;
14563 return; /* type error; errmsg already given */
14564 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014565# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014566 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014567 if (n == 0)
14568 rettv->vval.v_number = -1;
14569 else
14570 {
14571 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14572 rettv->vval.v_number = (s != NULL);
14573 }
14574# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014575 if (check_connection() == FAIL)
14576 return;
14577
14578 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014579 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014580# endif
14581
14582 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014584 char_u *retvar;
14585
Bram Moolenaar33570922005-01-25 22:26:29 +000014586 v.di_tv.v_type = VAR_STRING;
14587 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014588 retvar = get_tv_string_chk(&argvars[1]);
14589 if (retvar != NULL)
14590 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014591 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014592 }
14593#else
14594 rettv->vval.v_number = -1;
14595#endif
14596}
14597
Bram Moolenaar0d660222005-01-07 21:51:51 +000014598 static void
14599f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014600 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014601 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014602{
14603 char_u *r = NULL;
14604
14605#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014606 char_u *serverid = get_tv_string_chk(&argvars[0]);
14607
14608 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014609 {
14610# ifdef WIN32
14611 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014612 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014613
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014614 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014615 if (n != 0)
14616 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14617 if (r == NULL)
14618# else
14619 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014620 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014621# endif
14622 EMSG(_("E277: Unable to read a server reply"));
14623 }
14624#endif
14625 rettv->v_type = VAR_STRING;
14626 rettv->vval.v_string = r;
14627}
14628
14629/*
14630 * "remote_send()" function
14631 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014632 static void
14633f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014634 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014635 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014636{
14637 rettv->v_type = VAR_STRING;
14638 rettv->vval.v_string = NULL;
14639#ifdef FEAT_CLIENTSERVER
14640 remote_common(argvars, rettv, FALSE);
14641#endif
14642}
14643
14644/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014645 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014646 */
14647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014648f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014649 typval_T *argvars;
14650 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014651{
Bram Moolenaar33570922005-01-25 22:26:29 +000014652 list_T *l;
14653 listitem_T *item, *item2;
14654 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014655 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014656 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014657 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014658 dict_T *d;
14659 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014660
Bram Moolenaar8c711452005-01-14 21:53:12 +000014661 if (argvars[0].v_type == VAR_DICT)
14662 {
14663 if (argvars[2].v_type != VAR_UNKNOWN)
14664 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014665 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014666 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014668 key = get_tv_string_chk(&argvars[1]);
14669 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014671 di = dict_find(d, key, -1);
14672 if (di == NULL)
14673 EMSG2(_(e_dictkey), key);
14674 else
14675 {
14676 *rettv = di->di_tv;
14677 init_tv(&di->di_tv);
14678 dictitem_remove(d, di);
14679 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014681 }
14682 }
14683 else if (argvars[0].v_type != VAR_LIST)
14684 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014685 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014686 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014688 int error = FALSE;
14689
14690 idx = get_tv_number_chk(&argvars[1], &error);
14691 if (error)
14692 ; /* type error: do nothing, errmsg already given */
14693 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014694 EMSGN(_(e_listidx), idx);
14695 else
14696 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014697 if (argvars[2].v_type == VAR_UNKNOWN)
14698 {
14699 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014700 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014701 *rettv = item->li_tv;
14702 vim_free(item);
14703 }
14704 else
14705 {
14706 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014707 end = get_tv_number_chk(&argvars[2], &error);
14708 if (error)
14709 ; /* type error: do nothing */
14710 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014711 EMSGN(_(e_listidx), end);
14712 else
14713 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014714 int cnt = 0;
14715
14716 for (li = item; li != NULL; li = li->li_next)
14717 {
14718 ++cnt;
14719 if (li == item2)
14720 break;
14721 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014722 if (li == NULL) /* didn't find "item2" after "item" */
14723 EMSG(_(e_invrange));
14724 else
14725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014726 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014727 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014728 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014729 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014730 l->lv_first = item;
14731 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014732 item->li_prev = NULL;
14733 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014734 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014735 }
14736 }
14737 }
14738 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014739 }
14740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741}
14742
14743/*
14744 * "rename({from}, {to})" function
14745 */
14746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014747f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014748 typval_T *argvars;
14749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750{
14751 char_u buf[NUMBUFLEN];
14752
14753 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014756 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14757 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758}
14759
14760/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014761 * "repeat()" function
14762 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014763 static void
14764f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014765 typval_T *argvars;
14766 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014767{
14768 char_u *p;
14769 int n;
14770 int slen;
14771 int len;
14772 char_u *r;
14773 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014774
14775 n = get_tv_number(&argvars[1]);
14776 if (argvars[0].v_type == VAR_LIST)
14777 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014778 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014779 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014780 if (list_extend(rettv->vval.v_list,
14781 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014782 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014783 }
14784 else
14785 {
14786 p = get_tv_string(&argvars[0]);
14787 rettv->v_type = VAR_STRING;
14788 rettv->vval.v_string = NULL;
14789
14790 slen = (int)STRLEN(p);
14791 len = slen * n;
14792 if (len <= 0)
14793 return;
14794
14795 r = alloc(len + 1);
14796 if (r != NULL)
14797 {
14798 for (i = 0; i < n; i++)
14799 mch_memmove(r + i * slen, p, (size_t)slen);
14800 r[len] = NUL;
14801 }
14802
14803 rettv->vval.v_string = r;
14804 }
14805}
14806
14807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808 * "resolve()" function
14809 */
14810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014811f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014812 typval_T *argvars;
14813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014814{
14815 char_u *p;
14816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014817 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818#ifdef FEAT_SHORTCUT
14819 {
14820 char_u *v = NULL;
14821
14822 v = mch_resolve_shortcut(p);
14823 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014824 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014826 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 }
14828#else
14829# ifdef HAVE_READLINK
14830 {
14831 char_u buf[MAXPATHL + 1];
14832 char_u *cpy;
14833 int len;
14834 char_u *remain = NULL;
14835 char_u *q;
14836 int is_relative_to_current = FALSE;
14837 int has_trailing_pathsep = FALSE;
14838 int limit = 100;
14839
14840 p = vim_strsave(p);
14841
14842 if (p[0] == '.' && (vim_ispathsep(p[1])
14843 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14844 is_relative_to_current = TRUE;
14845
14846 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014847 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848 has_trailing_pathsep = TRUE;
14849
14850 q = getnextcomp(p);
14851 if (*q != NUL)
14852 {
14853 /* Separate the first path component in "p", and keep the
14854 * remainder (beginning with the path separator). */
14855 remain = vim_strsave(q - 1);
14856 q[-1] = NUL;
14857 }
14858
14859 for (;;)
14860 {
14861 for (;;)
14862 {
14863 len = readlink((char *)p, (char *)buf, MAXPATHL);
14864 if (len <= 0)
14865 break;
14866 buf[len] = NUL;
14867
14868 if (limit-- == 0)
14869 {
14870 vim_free(p);
14871 vim_free(remain);
14872 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014873 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874 goto fail;
14875 }
14876
14877 /* Ensure that the result will have a trailing path separator
14878 * if the argument has one. */
14879 if (remain == NULL && has_trailing_pathsep)
14880 add_pathsep(buf);
14881
14882 /* Separate the first path component in the link value and
14883 * concatenate the remainders. */
14884 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14885 if (*q != NUL)
14886 {
14887 if (remain == NULL)
14888 remain = vim_strsave(q - 1);
14889 else
14890 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014891 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892 if (cpy != NULL)
14893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014894 vim_free(remain);
14895 remain = cpy;
14896 }
14897 }
14898 q[-1] = NUL;
14899 }
14900
14901 q = gettail(p);
14902 if (q > p && *q == NUL)
14903 {
14904 /* Ignore trailing path separator. */
14905 q[-1] = NUL;
14906 q = gettail(p);
14907 }
14908 if (q > p && !mch_isFullName(buf))
14909 {
14910 /* symlink is relative to directory of argument */
14911 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14912 if (cpy != NULL)
14913 {
14914 STRCPY(cpy, p);
14915 STRCPY(gettail(cpy), buf);
14916 vim_free(p);
14917 p = cpy;
14918 }
14919 }
14920 else
14921 {
14922 vim_free(p);
14923 p = vim_strsave(buf);
14924 }
14925 }
14926
14927 if (remain == NULL)
14928 break;
14929
14930 /* Append the first path component of "remain" to "p". */
14931 q = getnextcomp(remain + 1);
14932 len = q - remain - (*q != NUL);
14933 cpy = vim_strnsave(p, STRLEN(p) + len);
14934 if (cpy != NULL)
14935 {
14936 STRNCAT(cpy, remain, len);
14937 vim_free(p);
14938 p = cpy;
14939 }
14940 /* Shorten "remain". */
14941 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014942 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943 else
14944 {
14945 vim_free(remain);
14946 remain = NULL;
14947 }
14948 }
14949
14950 /* If the result is a relative path name, make it explicitly relative to
14951 * the current directory if and only if the argument had this form. */
14952 if (!vim_ispathsep(*p))
14953 {
14954 if (is_relative_to_current
14955 && *p != NUL
14956 && !(p[0] == '.'
14957 && (p[1] == NUL
14958 || vim_ispathsep(p[1])
14959 || (p[1] == '.'
14960 && (p[2] == NUL
14961 || vim_ispathsep(p[2]))))))
14962 {
14963 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014964 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965 if (cpy != NULL)
14966 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967 vim_free(p);
14968 p = cpy;
14969 }
14970 }
14971 else if (!is_relative_to_current)
14972 {
14973 /* Strip leading "./". */
14974 q = p;
14975 while (q[0] == '.' && vim_ispathsep(q[1]))
14976 q += 2;
14977 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014978 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 }
14980 }
14981
14982 /* Ensure that the result will have no trailing path separator
14983 * if the argument had none. But keep "/" or "//". */
14984 if (!has_trailing_pathsep)
14985 {
14986 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014987 if (after_pathsep(p, q))
14988 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 }
14990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014991 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 }
14993# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014994 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995# endif
14996#endif
14997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014998 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999
15000#ifdef HAVE_READLINK
15001fail:
15002#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015003 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004}
15005
15006/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015007 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 */
15009 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015010f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015011 typval_T *argvars;
15012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013{
Bram Moolenaar33570922005-01-25 22:26:29 +000015014 list_T *l;
15015 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016
Bram Moolenaar0d660222005-01-07 21:51:51 +000015017 if (argvars[0].v_type != VAR_LIST)
15018 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015019 else if ((l = argvars[0].vval.v_list) != NULL
15020 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015021 {
15022 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015023 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015024 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015025 while (li != NULL)
15026 {
15027 ni = li->li_prev;
15028 list_append(l, li);
15029 li = ni;
15030 }
15031 rettv->vval.v_list = l;
15032 rettv->v_type = VAR_LIST;
15033 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015034 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036}
15037
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015038#define SP_NOMOVE 0x01 /* don't move cursor */
15039#define SP_REPEAT 0x02 /* repeat to find outer pair */
15040#define SP_RETCOUNT 0x04 /* return matchcount */
15041#define SP_SETPCMARK 0x08 /* set previous context mark */
15042#define SP_START 0x10 /* accept match at start position */
15043#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15044#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015045
Bram Moolenaar33570922005-01-25 22:26:29 +000015046static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015047
15048/*
15049 * Get flags for a search function.
15050 * Possibly sets "p_ws".
15051 * Returns BACKWARD, FORWARD or zero (for an error).
15052 */
15053 static int
15054get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015055 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015056 int *flagsp;
15057{
15058 int dir = FORWARD;
15059 char_u *flags;
15060 char_u nbuf[NUMBUFLEN];
15061 int mask;
15062
15063 if (varp->v_type != VAR_UNKNOWN)
15064 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015065 flags = get_tv_string_buf_chk(varp, nbuf);
15066 if (flags == NULL)
15067 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068 while (*flags != NUL)
15069 {
15070 switch (*flags)
15071 {
15072 case 'b': dir = BACKWARD; break;
15073 case 'w': p_ws = TRUE; break;
15074 case 'W': p_ws = FALSE; break;
15075 default: mask = 0;
15076 if (flagsp != NULL)
15077 switch (*flags)
15078 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015079 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015080 case 'e': mask = SP_END; break;
15081 case 'm': mask = SP_RETCOUNT; break;
15082 case 'n': mask = SP_NOMOVE; break;
15083 case 'p': mask = SP_SUBPAT; break;
15084 case 'r': mask = SP_REPEAT; break;
15085 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015086 }
15087 if (mask == 0)
15088 {
15089 EMSG2(_(e_invarg2), flags);
15090 dir = 0;
15091 }
15092 else
15093 *flagsp |= mask;
15094 }
15095 if (dir == 0)
15096 break;
15097 ++flags;
15098 }
15099 }
15100 return dir;
15101}
15102
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015104 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015106 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015107search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015108 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015109 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015110 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015112 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 char_u *pat;
15114 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015115 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116 int save_p_ws = p_ws;
15117 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015118 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015119 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015120 proftime_T tm;
15121#ifdef FEAT_RELTIME
15122 long time_limit = 0;
15123#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015124 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015125 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015127 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015128 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015129 if (dir == 0)
15130 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015131 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015132 if (flags & SP_START)
15133 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015134 if (flags & SP_END)
15135 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015136
Bram Moolenaar76929292008-01-06 19:07:36 +000015137 /* Optional arguments: line number to stop searching and timeout. */
15138 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015139 {
15140 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15141 if (lnum_stop < 0)
15142 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015143#ifdef FEAT_RELTIME
15144 if (argvars[3].v_type != VAR_UNKNOWN)
15145 {
15146 time_limit = get_tv_number_chk(&argvars[3], NULL);
15147 if (time_limit < 0)
15148 goto theend;
15149 }
15150#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015151 }
15152
Bram Moolenaar76929292008-01-06 19:07:36 +000015153#ifdef FEAT_RELTIME
15154 /* Set the time limit, if there is one. */
15155 profile_setlimit(time_limit, &tm);
15156#endif
15157
Bram Moolenaar231334e2005-07-25 20:46:57 +000015158 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015159 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015160 * Check to make sure only those flags are set.
15161 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15162 * flags cannot be set. Check for that condition also.
15163 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015164 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015165 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015167 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015168 goto theend;
15169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015171 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015172 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015173 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015174 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015176 if (flags & SP_SUBPAT)
15177 retval = subpatnum;
15178 else
15179 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015180 if (flags & SP_SETPCMARK)
15181 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015183 if (match_pos != NULL)
15184 {
15185 /* Store the match cursor position */
15186 match_pos->lnum = pos.lnum;
15187 match_pos->col = pos.col + 1;
15188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189 /* "/$" will put the cursor after the end of the line, may need to
15190 * correct that here */
15191 check_cursor();
15192 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015193
15194 /* If 'n' flag is used: restore cursor position. */
15195 if (flags & SP_NOMOVE)
15196 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015197 else
15198 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015199theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015201
15202 return retval;
15203}
15204
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015205#ifdef FEAT_FLOAT
15206/*
15207 * "round({float})" function
15208 */
15209 static void
15210f_round(argvars, rettv)
15211 typval_T *argvars;
15212 typval_T *rettv;
15213{
15214 float_T f;
15215
15216 rettv->v_type = VAR_FLOAT;
15217 if (get_float_arg(argvars, &f) == OK)
15218 /* round() is not in C90, use ceil() or floor() instead. */
15219 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15220 else
15221 rettv->vval.v_float = 0.0;
15222}
15223#endif
15224
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015225/*
15226 * "search()" function
15227 */
15228 static void
15229f_search(argvars, rettv)
15230 typval_T *argvars;
15231 typval_T *rettv;
15232{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015233 int flags = 0;
15234
15235 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236}
15237
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015239 * "searchdecl()" function
15240 */
15241 static void
15242f_searchdecl(argvars, rettv)
15243 typval_T *argvars;
15244 typval_T *rettv;
15245{
15246 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015247 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015248 int error = FALSE;
15249 char_u *name;
15250
15251 rettv->vval.v_number = 1; /* default: FAIL */
15252
15253 name = get_tv_string_chk(&argvars[0]);
15254 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015255 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015256 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015257 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15258 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15259 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015260 if (!error && name != NULL)
15261 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015262 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015263}
15264
15265/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015266 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015268 static int
15269searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015270 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015271 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272{
15273 char_u *spat, *mpat, *epat;
15274 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276 int dir;
15277 int flags = 0;
15278 char_u nbuf1[NUMBUFLEN];
15279 char_u nbuf2[NUMBUFLEN];
15280 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015281 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015282 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015283 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015286 spat = get_tv_string_chk(&argvars[0]);
15287 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15288 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15289 if (spat == NULL || mpat == NULL || epat == NULL)
15290 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292 /* Handle the optional fourth argument: flags */
15293 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015294 if (dir == 0)
15295 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015296
15297 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015298 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15299 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015300 if ((flags & (SP_END | SP_SUBPAT)) != 0
15301 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015302 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015303 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015304 goto theend;
15305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015307 /* Using 'r' implies 'W', otherwise it doesn't work. */
15308 if (flags & SP_REPEAT)
15309 p_ws = FALSE;
15310
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015311 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015312 if (argvars[3].v_type == VAR_UNKNOWN
15313 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 skip = (char_u *)"";
15315 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015317 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015318 if (argvars[5].v_type != VAR_UNKNOWN)
15319 {
15320 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15321 if (lnum_stop < 0)
15322 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015323#ifdef FEAT_RELTIME
15324 if (argvars[6].v_type != VAR_UNKNOWN)
15325 {
15326 time_limit = get_tv_number_chk(&argvars[6], NULL);
15327 if (time_limit < 0)
15328 goto theend;
15329 }
15330#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015331 }
15332 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015333 if (skip == NULL)
15334 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015336 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015337 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015338
15339theend:
15340 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015341
15342 return retval;
15343}
15344
15345/*
15346 * "searchpair()" function
15347 */
15348 static void
15349f_searchpair(argvars, rettv)
15350 typval_T *argvars;
15351 typval_T *rettv;
15352{
15353 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15354}
15355
15356/*
15357 * "searchpairpos()" function
15358 */
15359 static void
15360f_searchpairpos(argvars, rettv)
15361 typval_T *argvars;
15362 typval_T *rettv;
15363{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015364 pos_T match_pos;
15365 int lnum = 0;
15366 int col = 0;
15367
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015368 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015369 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015370
15371 if (searchpair_cmn(argvars, &match_pos) > 0)
15372 {
15373 lnum = match_pos.lnum;
15374 col = match_pos.col;
15375 }
15376
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015377 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15378 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015379}
15380
15381/*
15382 * Search for a start/middle/end thing.
15383 * Used by searchpair(), see its documentation for the details.
15384 * Returns 0 or -1 for no match,
15385 */
15386 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015387do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15388 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015389 char_u *spat; /* start pattern */
15390 char_u *mpat; /* middle pattern */
15391 char_u *epat; /* end pattern */
15392 int dir; /* BACKWARD or FORWARD */
15393 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015394 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015395 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015396 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015397 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015398{
15399 char_u *save_cpo;
15400 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15401 long retval = 0;
15402 pos_T pos;
15403 pos_T firstpos;
15404 pos_T foundpos;
15405 pos_T save_cursor;
15406 pos_T save_pos;
15407 int n;
15408 int r;
15409 int nest = 1;
15410 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015411 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015412 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015413
15414 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15415 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015416 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015417
Bram Moolenaar76929292008-01-06 19:07:36 +000015418#ifdef FEAT_RELTIME
15419 /* Set the time limit, if there is one. */
15420 profile_setlimit(time_limit, &tm);
15421#endif
15422
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015423 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15424 * start/middle/end (pat3, for the top pair). */
15425 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15426 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15427 if (pat2 == NULL || pat3 == NULL)
15428 goto theend;
15429 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15430 if (*mpat == NUL)
15431 STRCPY(pat3, pat2);
15432 else
15433 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15434 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015435 if (flags & SP_START)
15436 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015437
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438 save_cursor = curwin->w_cursor;
15439 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015440 clearpos(&firstpos);
15441 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 pat = pat3;
15443 for (;;)
15444 {
15445 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015446 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15448 /* didn't find it or found the first match again: FAIL */
15449 break;
15450
15451 if (firstpos.lnum == 0)
15452 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015453 if (equalpos(pos, foundpos))
15454 {
15455 /* Found the same position again. Can happen with a pattern that
15456 * has "\zs" at the end and searching backwards. Advance one
15457 * character and try again. */
15458 if (dir == BACKWARD)
15459 decl(&pos);
15460 else
15461 incl(&pos);
15462 }
15463 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015465 /* clear the start flag to avoid getting stuck here */
15466 options &= ~SEARCH_START;
15467
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 /* If the skip pattern matches, ignore this match. */
15469 if (*skip != NUL)
15470 {
15471 save_pos = curwin->w_cursor;
15472 curwin->w_cursor = pos;
15473 r = eval_to_bool(skip, &err, NULL, FALSE);
15474 curwin->w_cursor = save_pos;
15475 if (err)
15476 {
15477 /* Evaluating {skip} caused an error, break here. */
15478 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015479 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 break;
15481 }
15482 if (r)
15483 continue;
15484 }
15485
15486 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15487 {
15488 /* Found end when searching backwards or start when searching
15489 * forward: nested pair. */
15490 ++nest;
15491 pat = pat2; /* nested, don't search for middle */
15492 }
15493 else
15494 {
15495 /* Found end when searching forward or start when searching
15496 * backward: end of (nested) pair; or found middle in outer pair. */
15497 if (--nest == 1)
15498 pat = pat3; /* outer level, search for middle */
15499 }
15500
15501 if (nest == 0)
15502 {
15503 /* Found the match: return matchcount or line number. */
15504 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015505 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015507 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015508 if (flags & SP_SETPCMARK)
15509 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 curwin->w_cursor = pos;
15511 if (!(flags & SP_REPEAT))
15512 break;
15513 nest = 1; /* search for next unmatched */
15514 }
15515 }
15516
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015517 if (match_pos != NULL)
15518 {
15519 /* Store the match cursor position */
15520 match_pos->lnum = curwin->w_cursor.lnum;
15521 match_pos->col = curwin->w_cursor.col + 1;
15522 }
15523
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015525 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 curwin->w_cursor = save_cursor;
15527
15528theend:
15529 vim_free(pat2);
15530 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015531 if (p_cpo == empty_option)
15532 p_cpo = save_cpo;
15533 else
15534 /* Darn, evaluating the {skip} expression changed the value. */
15535 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015536
15537 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538}
15539
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015540/*
15541 * "searchpos()" function
15542 */
15543 static void
15544f_searchpos(argvars, rettv)
15545 typval_T *argvars;
15546 typval_T *rettv;
15547{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015548 pos_T match_pos;
15549 int lnum = 0;
15550 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015551 int n;
15552 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015553
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015554 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015555 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015556
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015557 n = search_cmn(argvars, &match_pos, &flags);
15558 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015559 {
15560 lnum = match_pos.lnum;
15561 col = match_pos.col;
15562 }
15563
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015564 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15565 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015566 if (flags & SP_SUBPAT)
15567 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015568}
15569
15570
Bram Moolenaar0d660222005-01-07 21:51:51 +000015571 static void
15572f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015573 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015576#ifdef FEAT_CLIENTSERVER
15577 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015578 char_u *server = get_tv_string_chk(&argvars[0]);
15579 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580
Bram Moolenaar0d660222005-01-07 21:51:51 +000015581 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015582 if (server == NULL || reply == NULL)
15583 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015584 if (check_restricted() || check_secure())
15585 return;
15586# ifdef FEAT_X11
15587 if (check_connection() == FAIL)
15588 return;
15589# endif
15590
15591 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015593 EMSG(_("E258: Unable to send to client"));
15594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015596 rettv->vval.v_number = 0;
15597#else
15598 rettv->vval.v_number = -1;
15599#endif
15600}
15601
Bram Moolenaar0d660222005-01-07 21:51:51 +000015602 static void
15603f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015604 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015605 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015606{
15607 char_u *r = NULL;
15608
15609#ifdef FEAT_CLIENTSERVER
15610# ifdef WIN32
15611 r = serverGetVimNames();
15612# else
15613 make_connection();
15614 if (X_DISPLAY != NULL)
15615 r = serverGetVimNames(X_DISPLAY);
15616# endif
15617#endif
15618 rettv->v_type = VAR_STRING;
15619 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620}
15621
15622/*
15623 * "setbufvar()" function
15624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015626f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015627 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015628 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629{
15630 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015633 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 char_u nbuf[NUMBUFLEN];
15635
15636 if (check_restricted() || check_secure())
15637 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015638 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15639 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015640 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 varp = &argvars[2];
15642
15643 if (buf != NULL && varname != NULL && varp != NULL)
15644 {
15645 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647
15648 if (*varname == '&')
15649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015650 long numval;
15651 char_u *strval;
15652 int error = FALSE;
15653
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015655 numval = get_tv_number_chk(varp, &error);
15656 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015657 if (!error && strval != NULL)
15658 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659 }
15660 else
15661 {
15662 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15663 if (bufvarname != NULL)
15664 {
15665 STRCPY(bufvarname, "b:");
15666 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015667 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 vim_free(bufvarname);
15669 }
15670 }
15671
15672 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675}
15676
15677/*
15678 * "setcmdpos()" function
15679 */
15680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015681f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015682 typval_T *argvars;
15683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015685 int pos = (int)get_tv_number(&argvars[0]) - 1;
15686
15687 if (pos >= 0)
15688 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689}
15690
15691/*
15692 * "setline()" function
15693 */
15694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015695f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015696 typval_T *argvars;
15697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698{
15699 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015700 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015701 list_T *l = NULL;
15702 listitem_T *li = NULL;
15703 long added = 0;
15704 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015706 lnum = get_tv_lnum(&argvars[0]);
15707 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015709 l = argvars[1].vval.v_list;
15710 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015712 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015713 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015714
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015715 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015716 for (;;)
15717 {
15718 if (l != NULL)
15719 {
15720 /* list argument, get next string */
15721 if (li == NULL)
15722 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015723 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015724 li = li->li_next;
15725 }
15726
15727 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015728 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015729 break;
15730 if (lnum <= curbuf->b_ml.ml_line_count)
15731 {
15732 /* existing line, replace it */
15733 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15734 {
15735 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015736 if (lnum == curwin->w_cursor.lnum)
15737 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015738 rettv->vval.v_number = 0; /* OK */
15739 }
15740 }
15741 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15742 {
15743 /* lnum is one past the last line, append the line */
15744 ++added;
15745 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15746 rettv->vval.v_number = 0; /* OK */
15747 }
15748
15749 if (l == NULL) /* only one string argument */
15750 break;
15751 ++lnum;
15752 }
15753
15754 if (added > 0)
15755 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756}
15757
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015758static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15759
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015761 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015762 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015763 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015764set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015765 win_T *wp UNUSED;
15766 typval_T *list_arg UNUSED;
15767 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015768 typval_T *rettv;
15769{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015770#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015771 char_u *act;
15772 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015773#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015774
Bram Moolenaar2641f772005-03-25 21:58:17 +000015775 rettv->vval.v_number = -1;
15776
15777#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015778 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015779 EMSG(_(e_listreq));
15780 else
15781 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015782 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015783
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015784 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015785 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015786 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015787 if (act == NULL)
15788 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015789 if (*act == 'a' || *act == 'r')
15790 action = *act;
15791 }
15792
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015793 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015794 rettv->vval.v_number = 0;
15795 }
15796#endif
15797}
15798
15799/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015800 * "setloclist()" function
15801 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015802 static void
15803f_setloclist(argvars, rettv)
15804 typval_T *argvars;
15805 typval_T *rettv;
15806{
15807 win_T *win;
15808
15809 rettv->vval.v_number = -1;
15810
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015811 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015812 if (win != NULL)
15813 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15814}
15815
15816/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015817 * "setmatches()" function
15818 */
15819 static void
15820f_setmatches(argvars, rettv)
15821 typval_T *argvars;
15822 typval_T *rettv;
15823{
15824#ifdef FEAT_SEARCH_EXTRA
15825 list_T *l;
15826 listitem_T *li;
15827 dict_T *d;
15828
15829 rettv->vval.v_number = -1;
15830 if (argvars[0].v_type != VAR_LIST)
15831 {
15832 EMSG(_(e_listreq));
15833 return;
15834 }
15835 if ((l = argvars[0].vval.v_list) != NULL)
15836 {
15837
15838 /* To some extent make sure that we are dealing with a list from
15839 * "getmatches()". */
15840 li = l->lv_first;
15841 while (li != NULL)
15842 {
15843 if (li->li_tv.v_type != VAR_DICT
15844 || (d = li->li_tv.vval.v_dict) == NULL)
15845 {
15846 EMSG(_(e_invarg));
15847 return;
15848 }
15849 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15850 && dict_find(d, (char_u *)"pattern", -1) != NULL
15851 && dict_find(d, (char_u *)"priority", -1) != NULL
15852 && dict_find(d, (char_u *)"id", -1) != NULL))
15853 {
15854 EMSG(_(e_invarg));
15855 return;
15856 }
15857 li = li->li_next;
15858 }
15859
15860 clear_matches(curwin);
15861 li = l->lv_first;
15862 while (li != NULL)
15863 {
15864 d = li->li_tv.vval.v_dict;
15865 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15866 get_dict_string(d, (char_u *)"pattern", FALSE),
15867 (int)get_dict_number(d, (char_u *)"priority"),
15868 (int)get_dict_number(d, (char_u *)"id"));
15869 li = li->li_next;
15870 }
15871 rettv->vval.v_number = 0;
15872 }
15873#endif
15874}
15875
15876/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015877 * "setpos()" function
15878 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015879 static void
15880f_setpos(argvars, rettv)
15881 typval_T *argvars;
15882 typval_T *rettv;
15883{
15884 pos_T pos;
15885 int fnum;
15886 char_u *name;
15887
Bram Moolenaar08250432008-02-13 11:42:46 +000015888 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015889 name = get_tv_string_chk(argvars);
15890 if (name != NULL)
15891 {
15892 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15893 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015894 if (--pos.col < 0)
15895 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015896 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015897 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015898 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015899 if (fnum == curbuf->b_fnum)
15900 {
15901 curwin->w_cursor = pos;
15902 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015903 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015904 }
15905 else
15906 EMSG(_(e_invarg));
15907 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015908 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15909 {
15910 /* set mark */
15911 if (setmark_pos(name[1], &pos, fnum) == OK)
15912 rettv->vval.v_number = 0;
15913 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015914 else
15915 EMSG(_(e_invarg));
15916 }
15917 }
15918}
15919
15920/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015921 * "setqflist()" function
15922 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015923 static void
15924f_setqflist(argvars, rettv)
15925 typval_T *argvars;
15926 typval_T *rettv;
15927{
15928 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15929}
15930
15931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932 * "setreg()" function
15933 */
15934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015935f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015936 typval_T *argvars;
15937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938{
15939 int regname;
15940 char_u *strregname;
15941 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015942 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943 int append;
15944 char_u yank_type;
15945 long block_len;
15946
15947 block_len = -1;
15948 yank_type = MAUTO;
15949 append = FALSE;
15950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015951 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015952 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015954 if (strregname == NULL)
15955 return; /* type error; errmsg already given */
15956 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957 if (regname == 0 || regname == '@')
15958 regname = '"';
15959 else if (regname == '=')
15960 return;
15961
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015962 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015963 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015964 stropt = get_tv_string_chk(&argvars[2]);
15965 if (stropt == NULL)
15966 return; /* type error */
15967 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968 switch (*stropt)
15969 {
15970 case 'a': case 'A': /* append */
15971 append = TRUE;
15972 break;
15973 case 'v': case 'c': /* character-wise selection */
15974 yank_type = MCHAR;
15975 break;
15976 case 'V': case 'l': /* line-wise selection */
15977 yank_type = MLINE;
15978 break;
15979#ifdef FEAT_VISUAL
15980 case 'b': case Ctrl_V: /* block-wise selection */
15981 yank_type = MBLOCK;
15982 if (VIM_ISDIGIT(stropt[1]))
15983 {
15984 ++stropt;
15985 block_len = getdigits(&stropt) - 1;
15986 --stropt;
15987 }
15988 break;
15989#endif
15990 }
15991 }
15992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015993 strval = get_tv_string_chk(&argvars[1]);
15994 if (strval != NULL)
15995 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015996 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015997 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998}
15999
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016000/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016001 * "settabvar()" function
16002 */
16003 static void
16004f_settabvar(argvars, rettv)
16005 typval_T *argvars;
16006 typval_T *rettv;
16007{
16008 tabpage_T *save_curtab;
16009 char_u *varname, *tabvarname;
16010 typval_T *varp;
16011 tabpage_T *tp;
16012
16013 rettv->vval.v_number = 0;
16014
16015 if (check_restricted() || check_secure())
16016 return;
16017
16018 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16019 varname = get_tv_string_chk(&argvars[1]);
16020 varp = &argvars[2];
16021
16022 if (tp != NULL && varname != NULL && varp != NULL)
16023 {
16024 save_curtab = curtab;
16025 goto_tabpage_tp(tp);
16026
16027 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16028 if (tabvarname != NULL)
16029 {
16030 STRCPY(tabvarname, "t:");
16031 STRCPY(tabvarname + 2, varname);
16032 set_var(tabvarname, varp, TRUE);
16033 vim_free(tabvarname);
16034 }
16035
16036 /* Restore current tabpage */
16037 if (valid_tabpage(save_curtab))
16038 goto_tabpage_tp(save_curtab);
16039 }
16040}
16041
16042/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016043 * "settabwinvar()" function
16044 */
16045 static void
16046f_settabwinvar(argvars, rettv)
16047 typval_T *argvars;
16048 typval_T *rettv;
16049{
16050 setwinvar(argvars, rettv, 1);
16051}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052
16053/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016054 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016057f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016058 typval_T *argvars;
16059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016061 setwinvar(argvars, rettv, 0);
16062}
16063
16064/*
16065 * "setwinvar()" and "settabwinvar()" functions
16066 */
16067 static void
16068setwinvar(argvars, rettv, off)
16069 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016070 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016071 int off;
16072{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073 win_T *win;
16074#ifdef FEAT_WINDOWS
16075 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016076 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077#endif
16078 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016079 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016081 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082
16083 if (check_restricted() || check_secure())
16084 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016085
16086#ifdef FEAT_WINDOWS
16087 if (off == 1)
16088 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16089 else
16090 tp = curtab;
16091#endif
16092 win = find_win_by_nr(&argvars[off], tp);
16093 varname = get_tv_string_chk(&argvars[off + 1]);
16094 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095
16096 if (win != NULL && varname != NULL && varp != NULL)
16097 {
16098#ifdef FEAT_WINDOWS
16099 /* set curwin to be our win, temporarily */
16100 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016101 save_curtab = curtab;
16102 goto_tabpage_tp(tp);
16103 if (!win_valid(win))
16104 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 curwin = win;
16106 curbuf = curwin->w_buffer;
16107#endif
16108
16109 if (*varname == '&')
16110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016111 long numval;
16112 char_u *strval;
16113 int error = FALSE;
16114
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016116 numval = get_tv_number_chk(varp, &error);
16117 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016118 if (!error && strval != NULL)
16119 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 }
16121 else
16122 {
16123 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16124 if (winvarname != NULL)
16125 {
16126 STRCPY(winvarname, "w:");
16127 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016128 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129 vim_free(winvarname);
16130 }
16131 }
16132
16133#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016134 /* Restore current tabpage and window, if still valid (autocomands can
16135 * make them invalid). */
16136 if (valid_tabpage(save_curtab))
16137 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138 if (win_valid(save_curwin))
16139 {
16140 curwin = save_curwin;
16141 curbuf = curwin->w_buffer;
16142 }
16143#endif
16144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145}
16146
16147/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016148 * "shellescape({string})" function
16149 */
16150 static void
16151f_shellescape(argvars, rettv)
16152 typval_T *argvars;
16153 typval_T *rettv;
16154{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016155 rettv->vval.v_string = vim_strsave_shellescape(
16156 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016157 rettv->v_type = VAR_STRING;
16158}
16159
16160/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016161 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162 */
16163 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016164f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016165 typval_T *argvars;
16166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016168 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169
Bram Moolenaar0d660222005-01-07 21:51:51 +000016170 p = get_tv_string(&argvars[0]);
16171 rettv->vval.v_string = vim_strsave(p);
16172 simplify_filename(rettv->vval.v_string); /* simplify in place */
16173 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174}
16175
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016176#ifdef FEAT_FLOAT
16177/*
16178 * "sin()" function
16179 */
16180 static void
16181f_sin(argvars, rettv)
16182 typval_T *argvars;
16183 typval_T *rettv;
16184{
16185 float_T f;
16186
16187 rettv->v_type = VAR_FLOAT;
16188 if (get_float_arg(argvars, &f) == OK)
16189 rettv->vval.v_float = sin(f);
16190 else
16191 rettv->vval.v_float = 0.0;
16192}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016193
16194/*
16195 * "sinh()" function
16196 */
16197 static void
16198f_sinh(argvars, rettv)
16199 typval_T *argvars;
16200 typval_T *rettv;
16201{
16202 float_T f;
16203
16204 rettv->v_type = VAR_FLOAT;
16205 if (get_float_arg(argvars, &f) == OK)
16206 rettv->vval.v_float = sinh(f);
16207 else
16208 rettv->vval.v_float = 0.0;
16209}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016210#endif
16211
Bram Moolenaar0d660222005-01-07 21:51:51 +000016212static int
16213#ifdef __BORLANDC__
16214 _RTLENTRYF
16215#endif
16216 item_compare __ARGS((const void *s1, const void *s2));
16217static int
16218#ifdef __BORLANDC__
16219 _RTLENTRYF
16220#endif
16221 item_compare2 __ARGS((const void *s1, const void *s2));
16222
16223static int item_compare_ic;
16224static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016225static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016226#define ITEM_COMPARE_FAIL 999
16227
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016229 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016231 static int
16232#ifdef __BORLANDC__
16233_RTLENTRYF
16234#endif
16235item_compare(s1, s2)
16236 const void *s1;
16237 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016239 char_u *p1, *p2;
16240 char_u *tofree1, *tofree2;
16241 int res;
16242 char_u numbuf1[NUMBUFLEN];
16243 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016245 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16246 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016247 if (p1 == NULL)
16248 p1 = (char_u *)"";
16249 if (p2 == NULL)
16250 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016251 if (item_compare_ic)
16252 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016254 res = STRCMP(p1, p2);
16255 vim_free(tofree1);
16256 vim_free(tofree2);
16257 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258}
16259
16260 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016261#ifdef __BORLANDC__
16262_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016264item_compare2(s1, s2)
16265 const void *s1;
16266 const void *s2;
16267{
16268 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016270 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016271 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016273 /* shortcut after failure in previous call; compare all items equal */
16274 if (item_compare_func_err)
16275 return 0;
16276
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016277 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16278 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016279 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16280 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016281
16282 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016283 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016284 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016285 clear_tv(&argv[0]);
16286 clear_tv(&argv[1]);
16287
16288 if (res == FAIL)
16289 res = ITEM_COMPARE_FAIL;
16290 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016291 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16292 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016293 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016294 clear_tv(&rettv);
16295 return res;
16296}
16297
16298/*
16299 * "sort({list})" function
16300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016302f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016303 typval_T *argvars;
16304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305{
Bram Moolenaar33570922005-01-25 22:26:29 +000016306 list_T *l;
16307 listitem_T *li;
16308 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016309 long len;
16310 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311
Bram Moolenaar0d660222005-01-07 21:51:51 +000016312 if (argvars[0].v_type != VAR_LIST)
16313 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 else
16315 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016317 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016318 return;
16319 rettv->vval.v_list = l;
16320 rettv->v_type = VAR_LIST;
16321 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322
Bram Moolenaar0d660222005-01-07 21:51:51 +000016323 len = list_len(l);
16324 if (len <= 1)
16325 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326
Bram Moolenaar0d660222005-01-07 21:51:51 +000016327 item_compare_ic = FALSE;
16328 item_compare_func = NULL;
16329 if (argvars[1].v_type != VAR_UNKNOWN)
16330 {
16331 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016332 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016333 else
16334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016335 int error = FALSE;
16336
16337 i = get_tv_number_chk(&argvars[1], &error);
16338 if (error)
16339 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016340 if (i == 1)
16341 item_compare_ic = TRUE;
16342 else
16343 item_compare_func = get_tv_string(&argvars[1]);
16344 }
16345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346
Bram Moolenaar0d660222005-01-07 21:51:51 +000016347 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016348 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016349 if (ptrs == NULL)
16350 return;
16351 i = 0;
16352 for (li = l->lv_first; li != NULL; li = li->li_next)
16353 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016355 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016356 /* test the compare function */
16357 if (item_compare_func != NULL
16358 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16359 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016360 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016362 {
16363 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016364 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016365 item_compare_func == NULL ? item_compare : item_compare2);
16366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016367 if (!item_compare_func_err)
16368 {
16369 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016370 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016371 l->lv_len = 0;
16372 for (i = 0; i < len; ++i)
16373 list_append(l, ptrs[i]);
16374 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016375 }
16376
16377 vim_free(ptrs);
16378 }
16379}
16380
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016381/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016382 * "soundfold({word})" function
16383 */
16384 static void
16385f_soundfold(argvars, rettv)
16386 typval_T *argvars;
16387 typval_T *rettv;
16388{
16389 char_u *s;
16390
16391 rettv->v_type = VAR_STRING;
16392 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016393#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016394 rettv->vval.v_string = eval_soundfold(s);
16395#else
16396 rettv->vval.v_string = vim_strsave(s);
16397#endif
16398}
16399
16400/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016401 * "spellbadword()" function
16402 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016403 static void
16404f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016405 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016406 typval_T *rettv;
16407{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016408 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016409 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016410 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016411
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016412 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016413 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016414
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016415#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016416 if (argvars[0].v_type == VAR_UNKNOWN)
16417 {
16418 /* Find the start and length of the badly spelled word. */
16419 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16420 if (len != 0)
16421 word = ml_get_cursor();
16422 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016423 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016424 {
16425 char_u *str = get_tv_string_chk(&argvars[0]);
16426 int capcol = -1;
16427
16428 if (str != NULL)
16429 {
16430 /* Check the argument for spelling. */
16431 while (*str != NUL)
16432 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016433 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016434 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016435 {
16436 word = str;
16437 break;
16438 }
16439 str += len;
16440 }
16441 }
16442 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016443#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016444
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016445 list_append_string(rettv->vval.v_list, word, len);
16446 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016447 attr == HLF_SPB ? "bad" :
16448 attr == HLF_SPR ? "rare" :
16449 attr == HLF_SPL ? "local" :
16450 attr == HLF_SPC ? "caps" :
16451 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016452}
16453
16454/*
16455 * "spellsuggest()" function
16456 */
16457 static void
16458f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016459 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016460 typval_T *rettv;
16461{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016462#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016463 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016464 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016465 int maxcount;
16466 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016467 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016468 listitem_T *li;
16469 int need_capital = FALSE;
16470#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016471
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016472 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016473 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016474
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016475#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016476 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016477 {
16478 str = get_tv_string(&argvars[0]);
16479 if (argvars[1].v_type != VAR_UNKNOWN)
16480 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016481 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016482 if (maxcount <= 0)
16483 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016484 if (argvars[2].v_type != VAR_UNKNOWN)
16485 {
16486 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16487 if (typeerr)
16488 return;
16489 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016490 }
16491 else
16492 maxcount = 25;
16493
Bram Moolenaar4770d092006-01-12 23:22:24 +000016494 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016495
16496 for (i = 0; i < ga.ga_len; ++i)
16497 {
16498 str = ((char_u **)ga.ga_data)[i];
16499
16500 li = listitem_alloc();
16501 if (li == NULL)
16502 vim_free(str);
16503 else
16504 {
16505 li->li_tv.v_type = VAR_STRING;
16506 li->li_tv.v_lock = 0;
16507 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016508 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016509 }
16510 }
16511 ga_clear(&ga);
16512 }
16513#endif
16514}
16515
Bram Moolenaar0d660222005-01-07 21:51:51 +000016516 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016517f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016518 typval_T *argvars;
16519 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016520{
16521 char_u *str;
16522 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016523 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016524 regmatch_T regmatch;
16525 char_u patbuf[NUMBUFLEN];
16526 char_u *save_cpo;
16527 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016528 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016529 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016530 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016531
16532 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16533 save_cpo = p_cpo;
16534 p_cpo = (char_u *)"";
16535
16536 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016537 if (argvars[1].v_type != VAR_UNKNOWN)
16538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016539 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16540 if (pat == NULL)
16541 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016542 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016543 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016544 }
16545 if (pat == NULL || *pat == NUL)
16546 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016547
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016548 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016550 if (typeerr)
16551 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552
Bram Moolenaar0d660222005-01-07 21:51:51 +000016553 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16554 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016556 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016557 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016558 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016559 if (*str == NUL)
16560 match = FALSE; /* empty item at the end */
16561 else
16562 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016563 if (match)
16564 end = regmatch.startp[0];
16565 else
16566 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016567 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16568 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016569 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016570 if (list_append_string(rettv->vval.v_list, str,
16571 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016572 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016573 }
16574 if (!match)
16575 break;
16576 /* Advance to just after the match. */
16577 if (regmatch.endp[0] > str)
16578 col = 0;
16579 else
16580 {
16581 /* Don't get stuck at the same match. */
16582#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016583 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016584#else
16585 col = 1;
16586#endif
16587 }
16588 str = regmatch.endp[0];
16589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590
Bram Moolenaar0d660222005-01-07 21:51:51 +000016591 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593
Bram Moolenaar0d660222005-01-07 21:51:51 +000016594 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595}
16596
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016597#ifdef FEAT_FLOAT
16598/*
16599 * "sqrt()" function
16600 */
16601 static void
16602f_sqrt(argvars, rettv)
16603 typval_T *argvars;
16604 typval_T *rettv;
16605{
16606 float_T f;
16607
16608 rettv->v_type = VAR_FLOAT;
16609 if (get_float_arg(argvars, &f) == OK)
16610 rettv->vval.v_float = sqrt(f);
16611 else
16612 rettv->vval.v_float = 0.0;
16613}
16614
16615/*
16616 * "str2float()" function
16617 */
16618 static void
16619f_str2float(argvars, rettv)
16620 typval_T *argvars;
16621 typval_T *rettv;
16622{
16623 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16624
16625 if (*p == '+')
16626 p = skipwhite(p + 1);
16627 (void)string2float(p, &rettv->vval.v_float);
16628 rettv->v_type = VAR_FLOAT;
16629}
16630#endif
16631
Bram Moolenaar2c932302006-03-18 21:42:09 +000016632/*
16633 * "str2nr()" function
16634 */
16635 static void
16636f_str2nr(argvars, rettv)
16637 typval_T *argvars;
16638 typval_T *rettv;
16639{
16640 int base = 10;
16641 char_u *p;
16642 long n;
16643
16644 if (argvars[1].v_type != VAR_UNKNOWN)
16645 {
16646 base = get_tv_number(&argvars[1]);
16647 if (base != 8 && base != 10 && base != 16)
16648 {
16649 EMSG(_(e_invarg));
16650 return;
16651 }
16652 }
16653
16654 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016655 if (*p == '+')
16656 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016657 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16658 rettv->vval.v_number = n;
16659}
16660
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661#ifdef HAVE_STRFTIME
16662/*
16663 * "strftime({format}[, {time}])" function
16664 */
16665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016666f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016667 typval_T *argvars;
16668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669{
16670 char_u result_buf[256];
16671 struct tm *curtime;
16672 time_t seconds;
16673 char_u *p;
16674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016675 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016677 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016678 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 seconds = time(NULL);
16680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016681 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682 curtime = localtime(&seconds);
16683 /* MSVC returns NULL for an invalid value of seconds. */
16684 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016685 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686 else
16687 {
16688# ifdef FEAT_MBYTE
16689 vimconv_T conv;
16690 char_u *enc;
16691
16692 conv.vc_type = CONV_NONE;
16693 enc = enc_locale();
16694 convert_setup(&conv, p_enc, enc);
16695 if (conv.vc_type != CONV_NONE)
16696 p = string_convert(&conv, p, NULL);
16697# endif
16698 if (p != NULL)
16699 (void)strftime((char *)result_buf, sizeof(result_buf),
16700 (char *)p, curtime);
16701 else
16702 result_buf[0] = NUL;
16703
16704# ifdef FEAT_MBYTE
16705 if (conv.vc_type != CONV_NONE)
16706 vim_free(p);
16707 convert_setup(&conv, enc, p_enc);
16708 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016709 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 else
16711# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016712 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713
16714# ifdef FEAT_MBYTE
16715 /* Release conversion descriptors */
16716 convert_setup(&conv, NULL, NULL);
16717 vim_free(enc);
16718# endif
16719 }
16720}
16721#endif
16722
16723/*
16724 * "stridx()" function
16725 */
16726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016727f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016728 typval_T *argvars;
16729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730{
16731 char_u buf[NUMBUFLEN];
16732 char_u *needle;
16733 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016734 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016736 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016738 needle = get_tv_string_chk(&argvars[1]);
16739 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016740 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016741 if (needle == NULL || haystack == NULL)
16742 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743
Bram Moolenaar33570922005-01-25 22:26:29 +000016744 if (argvars[2].v_type != VAR_UNKNOWN)
16745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016746 int error = FALSE;
16747
16748 start_idx = get_tv_number_chk(&argvars[2], &error);
16749 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016750 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016751 if (start_idx >= 0)
16752 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016753 }
16754
16755 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16756 if (pos != NULL)
16757 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758}
16759
16760/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016761 * "string()" function
16762 */
16763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016764f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016765 typval_T *argvars;
16766 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016767{
16768 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016769 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016771 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016772 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016773 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016774 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016775 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776}
16777
16778/*
16779 * "strlen()" function
16780 */
16781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016782f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016783 typval_T *argvars;
16784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016786 rettv->vval.v_number = (varnumber_T)(STRLEN(
16787 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788}
16789
16790/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016791 * "strchars()" function
16792 */
16793 static void
16794f_strchars(argvars, rettv)
16795 typval_T *argvars;
16796 typval_T *rettv;
16797{
16798 char_u *s = get_tv_string(&argvars[0]);
16799#ifdef FEAT_MBYTE
16800 varnumber_T len = 0;
16801
16802 while (*s != NUL)
16803 {
16804 mb_cptr2char_adv(&s);
16805 ++len;
16806 }
16807 rettv->vval.v_number = len;
16808#else
16809 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16810#endif
16811}
16812
16813/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016814 * "strdisplaywidth()" function
16815 */
16816 static void
16817f_strdisplaywidth(argvars, rettv)
16818 typval_T *argvars;
16819 typval_T *rettv;
16820{
16821 char_u *s = get_tv_string(&argvars[0]);
16822 int col = 0;
16823
16824 if (argvars[1].v_type != VAR_UNKNOWN)
16825 col = get_tv_number(&argvars[1]);
16826
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016827 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016828}
16829
16830/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016831 * "strwidth()" function
16832 */
16833 static void
16834f_strwidth(argvars, rettv)
16835 typval_T *argvars;
16836 typval_T *rettv;
16837{
16838 char_u *s = get_tv_string(&argvars[0]);
16839
16840 rettv->vval.v_number = (varnumber_T)(
16841#ifdef FEAT_MBYTE
16842 mb_string2cells(s, -1)
16843#else
16844 STRLEN(s)
16845#endif
16846 );
16847}
16848
16849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 * "strpart()" function
16851 */
16852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016853f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016854 typval_T *argvars;
16855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856{
16857 char_u *p;
16858 int n;
16859 int len;
16860 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016861 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016863 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 slen = (int)STRLEN(p);
16865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016866 n = get_tv_number_chk(&argvars[1], &error);
16867 if (error)
16868 len = 0;
16869 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016870 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 else
16872 len = slen - n; /* default len: all bytes that are available. */
16873
16874 /*
16875 * Only return the overlap between the specified part and the actual
16876 * string.
16877 */
16878 if (n < 0)
16879 {
16880 len += n;
16881 n = 0;
16882 }
16883 else if (n > slen)
16884 n = slen;
16885 if (len < 0)
16886 len = 0;
16887 else if (n + len > slen)
16888 len = slen - n;
16889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016890 rettv->v_type = VAR_STRING;
16891 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892}
16893
16894/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895 * "strridx()" function
16896 */
16897 static void
16898f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016899 typval_T *argvars;
16900 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016901{
16902 char_u buf[NUMBUFLEN];
16903 char_u *needle;
16904 char_u *haystack;
16905 char_u *rest;
16906 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016907 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016909 needle = get_tv_string_chk(&argvars[1]);
16910 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016911
16912 rettv->vval.v_number = -1;
16913 if (needle == NULL || haystack == NULL)
16914 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016915
16916 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016917 if (argvars[2].v_type != VAR_UNKNOWN)
16918 {
16919 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016920 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016921 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016922 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016923 }
16924 else
16925 end_idx = haystack_len;
16926
Bram Moolenaar0d660222005-01-07 21:51:51 +000016927 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016928 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016929 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016930 lastmatch = haystack + end_idx;
16931 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016932 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016933 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016934 for (rest = haystack; *rest != '\0'; ++rest)
16935 {
16936 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016937 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938 break;
16939 lastmatch = rest;
16940 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016941 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942
16943 if (lastmatch == NULL)
16944 rettv->vval.v_number = -1;
16945 else
16946 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16947}
16948
16949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 * "strtrans()" function
16951 */
16952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016953f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016954 typval_T *argvars;
16955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016957 rettv->v_type = VAR_STRING;
16958 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959}
16960
16961/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962 * "submatch()" function
16963 */
16964 static void
16965f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016966 typval_T *argvars;
16967 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968{
16969 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016970 rettv->vval.v_string =
16971 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972}
16973
16974/*
16975 * "substitute()" function
16976 */
16977 static void
16978f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016979 typval_T *argvars;
16980 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016981{
16982 char_u patbuf[NUMBUFLEN];
16983 char_u subbuf[NUMBUFLEN];
16984 char_u flagsbuf[NUMBUFLEN];
16985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016986 char_u *str = get_tv_string_chk(&argvars[0]);
16987 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16988 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16989 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16990
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016992 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16993 rettv->vval.v_string = NULL;
16994 else
16995 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016996}
16997
16998/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016999 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017002f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017003 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005{
17006 int id = 0;
17007#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017008 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009 long col;
17010 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017011 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017013 lnum = get_tv_lnum(argvars); /* -1 on type error */
17014 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17015 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017017 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017018 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017019 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020#endif
17021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017022 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023}
17024
17025/*
17026 * "synIDattr(id, what [, mode])" function
17027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017029f_synIDattr(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 char_u *p = NULL;
17034#ifdef FEAT_SYN_HL
17035 int id;
17036 char_u *what;
17037 char_u *mode;
17038 char_u modebuf[NUMBUFLEN];
17039 int modec;
17040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017041 id = get_tv_number(&argvars[0]);
17042 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017043 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017045 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017047 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 modec = 0; /* replace invalid with current */
17049 }
17050 else
17051 {
17052#ifdef FEAT_GUI
17053 if (gui.in_use)
17054 modec = 'g';
17055 else
17056#endif
17057 if (t_colors > 1)
17058 modec = 'c';
17059 else
17060 modec = 't';
17061 }
17062
17063
17064 switch (TOLOWER_ASC(what[0]))
17065 {
17066 case 'b':
17067 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17068 p = highlight_color(id, what, modec);
17069 else /* bold */
17070 p = highlight_has_attr(id, HL_BOLD, modec);
17071 break;
17072
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017073 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074 p = highlight_color(id, what, modec);
17075 break;
17076
17077 case 'i':
17078 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17079 p = highlight_has_attr(id, HL_INVERSE, modec);
17080 else /* italic */
17081 p = highlight_has_attr(id, HL_ITALIC, modec);
17082 break;
17083
17084 case 'n': /* name */
17085 p = get_highlight_name(NULL, id - 1);
17086 break;
17087
17088 case 'r': /* reverse */
17089 p = highlight_has_attr(id, HL_INVERSE, modec);
17090 break;
17091
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017092 case 's':
17093 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17094 p = highlight_color(id, what, modec);
17095 else /* standout */
17096 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 break;
17098
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017099 case 'u':
17100 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17101 /* underline */
17102 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17103 else
17104 /* undercurl */
17105 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106 break;
17107 }
17108
17109 if (p != NULL)
17110 p = vim_strsave(p);
17111#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017112 rettv->v_type = VAR_STRING;
17113 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114}
17115
17116/*
17117 * "synIDtrans(id)" function
17118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017120f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017121 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123{
17124 int id;
17125
17126#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017127 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128
17129 if (id > 0)
17130 id = syn_get_final_id(id);
17131 else
17132#endif
17133 id = 0;
17134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017135 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136}
17137
17138/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017139 * "synconcealed(lnum, col)" function
17140 */
17141 static void
17142f_synconcealed(argvars, rettv)
17143 typval_T *argvars UNUSED;
17144 typval_T *rettv;
17145{
17146#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17147 long lnum;
17148 long col;
17149 int syntax_flags = 0;
17150 int cchar;
17151 int matchid = 0;
17152 char_u str[NUMBUFLEN];
17153#endif
17154
17155 rettv->v_type = VAR_LIST;
17156 rettv->vval.v_list = NULL;
17157
17158#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17159 lnum = get_tv_lnum(argvars); /* -1 on type error */
17160 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17161
17162 vim_memset(str, NUL, sizeof(str));
17163
17164 if (rettv_list_alloc(rettv) != FAIL)
17165 {
17166 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17167 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17168 && curwin->w_p_cole > 0)
17169 {
17170 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17171 syntax_flags = get_syntax_info(&matchid);
17172
17173 /* get the conceal character */
17174 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17175 {
17176 cchar = syn_get_sub_char();
17177 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17178 cchar = lcs_conceal;
17179 if (cchar != NUL)
17180 {
17181# ifdef FEAT_MBYTE
17182 if (has_mbyte)
17183 (*mb_char2bytes)(cchar, str);
17184 else
17185# endif
17186 str[0] = cchar;
17187 }
17188 }
17189 }
17190
17191 list_append_number(rettv->vval.v_list,
17192 (syntax_flags & HL_CONCEAL) != 0);
17193 /* -1 to auto-determine strlen */
17194 list_append_string(rettv->vval.v_list, str, -1);
17195 list_append_number(rettv->vval.v_list, matchid);
17196 }
17197#endif
17198}
17199
17200/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017201 * "synstack(lnum, col)" function
17202 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017203 static void
17204f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017205 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017206 typval_T *rettv;
17207{
17208#ifdef FEAT_SYN_HL
17209 long lnum;
17210 long col;
17211 int i;
17212 int id;
17213#endif
17214
17215 rettv->v_type = VAR_LIST;
17216 rettv->vval.v_list = NULL;
17217
17218#ifdef FEAT_SYN_HL
17219 lnum = get_tv_lnum(argvars); /* -1 on type error */
17220 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17221
17222 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017223 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017224 && rettv_list_alloc(rettv) != FAIL)
17225 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017226 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017227 for (i = 0; ; ++i)
17228 {
17229 id = syn_get_stack_item(i);
17230 if (id < 0)
17231 break;
17232 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17233 break;
17234 }
17235 }
17236#endif
17237}
17238
17239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 * "system()" function
17241 */
17242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017243f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017244 typval_T *argvars;
17245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017247 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017249 char_u *infile = NULL;
17250 char_u buf[NUMBUFLEN];
17251 int err = FALSE;
17252 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017254 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017255 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017256
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017257 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017258 {
17259 /*
17260 * Write the string to a temp file, to be used for input of the shell
17261 * command.
17262 */
17263 if ((infile = vim_tempname('i')) == NULL)
17264 {
17265 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017266 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017267 }
17268
17269 fd = mch_fopen((char *)infile, WRITEBIN);
17270 if (fd == NULL)
17271 {
17272 EMSG2(_(e_notopen), infile);
17273 goto done;
17274 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017275 p = get_tv_string_buf_chk(&argvars[1], buf);
17276 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017277 {
17278 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017279 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017280 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017281 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17282 err = TRUE;
17283 if (fclose(fd) != 0)
17284 err = TRUE;
17285 if (err)
17286 {
17287 EMSG(_("E677: Error writing temp file"));
17288 goto done;
17289 }
17290 }
17291
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017292 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17293 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017294
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295#ifdef USE_CR
17296 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017297 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 {
17299 char_u *s;
17300
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017301 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302 {
17303 if (*s == CAR)
17304 *s = NL;
17305 }
17306 }
17307#else
17308# ifdef USE_CRNL
17309 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017310 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311 {
17312 char_u *s, *d;
17313
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017314 d = res;
17315 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 {
17317 if (s[0] == CAR && s[1] == NL)
17318 ++s;
17319 *d++ = *s;
17320 }
17321 *d = NUL;
17322 }
17323# endif
17324#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017325
17326done:
17327 if (infile != NULL)
17328 {
17329 mch_remove(infile);
17330 vim_free(infile);
17331 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017332 rettv->v_type = VAR_STRING;
17333 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334}
17335
17336/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017337 * "tabpagebuflist()" function
17338 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017339 static void
17340f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017341 typval_T *argvars UNUSED;
17342 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017343{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017344#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017345 tabpage_T *tp;
17346 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017347
17348 if (argvars[0].v_type == VAR_UNKNOWN)
17349 wp = firstwin;
17350 else
17351 {
17352 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17353 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017354 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017355 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017356 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017357 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017358 for (; wp != NULL; wp = wp->w_next)
17359 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017360 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017361 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017362 }
17363#endif
17364}
17365
17366
17367/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017368 * "tabpagenr()" function
17369 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017370 static void
17371f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017372 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017373 typval_T *rettv;
17374{
17375 int nr = 1;
17376#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017377 char_u *arg;
17378
17379 if (argvars[0].v_type != VAR_UNKNOWN)
17380 {
17381 arg = get_tv_string_chk(&argvars[0]);
17382 nr = 0;
17383 if (arg != NULL)
17384 {
17385 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017386 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017387 else
17388 EMSG2(_(e_invexpr2), arg);
17389 }
17390 }
17391 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017392 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017393#endif
17394 rettv->vval.v_number = nr;
17395}
17396
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017397
17398#ifdef FEAT_WINDOWS
17399static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17400
17401/*
17402 * Common code for tabpagewinnr() and winnr().
17403 */
17404 static int
17405get_winnr(tp, argvar)
17406 tabpage_T *tp;
17407 typval_T *argvar;
17408{
17409 win_T *twin;
17410 int nr = 1;
17411 win_T *wp;
17412 char_u *arg;
17413
17414 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17415 if (argvar->v_type != VAR_UNKNOWN)
17416 {
17417 arg = get_tv_string_chk(argvar);
17418 if (arg == NULL)
17419 nr = 0; /* type error; errmsg already given */
17420 else if (STRCMP(arg, "$") == 0)
17421 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17422 else if (STRCMP(arg, "#") == 0)
17423 {
17424 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17425 if (twin == NULL)
17426 nr = 0;
17427 }
17428 else
17429 {
17430 EMSG2(_(e_invexpr2), arg);
17431 nr = 0;
17432 }
17433 }
17434
17435 if (nr > 0)
17436 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17437 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017438 {
17439 if (wp == NULL)
17440 {
17441 /* didn't find it in this tabpage */
17442 nr = 0;
17443 break;
17444 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017445 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017446 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017447 return nr;
17448}
17449#endif
17450
17451/*
17452 * "tabpagewinnr()" function
17453 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017454 static void
17455f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017456 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017457 typval_T *rettv;
17458{
17459 int nr = 1;
17460#ifdef FEAT_WINDOWS
17461 tabpage_T *tp;
17462
17463 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17464 if (tp == NULL)
17465 nr = 0;
17466 else
17467 nr = get_winnr(tp, &argvars[1]);
17468#endif
17469 rettv->vval.v_number = nr;
17470}
17471
17472
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017473/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017474 * "tagfiles()" function
17475 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017476 static void
17477f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017478 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017479 typval_T *rettv;
17480{
17481 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017482 tagname_T tn;
17483 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017484
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017485 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017486 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017487
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017488 for (first = TRUE; ; first = FALSE)
17489 if (get_tagfname(&tn, first, fname) == FAIL
17490 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017491 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017492 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017493}
17494
17495/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017496 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017497 */
17498 static void
17499f_taglist(argvars, rettv)
17500 typval_T *argvars;
17501 typval_T *rettv;
17502{
17503 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017504
17505 tag_pattern = get_tv_string(&argvars[0]);
17506
17507 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017508 if (*tag_pattern == NUL)
17509 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017510
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017511 if (rettv_list_alloc(rettv) == OK)
17512 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017513}
17514
17515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 * "tempname()" function
17517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017519f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017520 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522{
17523 static int x = 'A';
17524
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017525 rettv->v_type = VAR_STRING;
17526 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527
17528 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17529 * names. Skip 'I' and 'O', they are used for shell redirection. */
17530 do
17531 {
17532 if (x == 'Z')
17533 x = '0';
17534 else if (x == '9')
17535 x = 'A';
17536 else
17537 {
17538#ifdef EBCDIC
17539 if (x == 'I')
17540 x = 'J';
17541 else if (x == 'R')
17542 x = 'S';
17543 else
17544#endif
17545 ++x;
17546 }
17547 } while (x == 'I' || x == 'O');
17548}
17549
17550/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017551 * "test(list)" function: Just checking the walls...
17552 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017553 static void
17554f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017555 typval_T *argvars UNUSED;
17556 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017557{
17558 /* Used for unit testing. Change the code below to your liking. */
17559#if 0
17560 listitem_T *li;
17561 list_T *l;
17562 char_u *bad, *good;
17563
17564 if (argvars[0].v_type != VAR_LIST)
17565 return;
17566 l = argvars[0].vval.v_list;
17567 if (l == NULL)
17568 return;
17569 li = l->lv_first;
17570 if (li == NULL)
17571 return;
17572 bad = get_tv_string(&li->li_tv);
17573 li = li->li_next;
17574 if (li == NULL)
17575 return;
17576 good = get_tv_string(&li->li_tv);
17577 rettv->vval.v_number = test_edit_score(bad, good);
17578#endif
17579}
17580
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017581#ifdef FEAT_FLOAT
17582/*
17583 * "tan()" function
17584 */
17585 static void
17586f_tan(argvars, rettv)
17587 typval_T *argvars;
17588 typval_T *rettv;
17589{
17590 float_T f;
17591
17592 rettv->v_type = VAR_FLOAT;
17593 if (get_float_arg(argvars, &f) == OK)
17594 rettv->vval.v_float = tan(f);
17595 else
17596 rettv->vval.v_float = 0.0;
17597}
17598
17599/*
17600 * "tanh()" function
17601 */
17602 static void
17603f_tanh(argvars, rettv)
17604 typval_T *argvars;
17605 typval_T *rettv;
17606{
17607 float_T f;
17608
17609 rettv->v_type = VAR_FLOAT;
17610 if (get_float_arg(argvars, &f) == OK)
17611 rettv->vval.v_float = tanh(f);
17612 else
17613 rettv->vval.v_float = 0.0;
17614}
17615#endif
17616
Bram Moolenaard52d9742005-08-21 22:20:28 +000017617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 * "tolower(string)" function
17619 */
17620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017621f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017622 typval_T *argvars;
17623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624{
17625 char_u *p;
17626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017627 p = vim_strsave(get_tv_string(&argvars[0]));
17628 rettv->v_type = VAR_STRING;
17629 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630
17631 if (p != NULL)
17632 while (*p != NUL)
17633 {
17634#ifdef FEAT_MBYTE
17635 int l;
17636
17637 if (enc_utf8)
17638 {
17639 int c, lc;
17640
17641 c = utf_ptr2char(p);
17642 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017643 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 /* TODO: reallocate string when byte count changes. */
17645 if (utf_char2len(lc) == l)
17646 utf_char2bytes(lc, p);
17647 p += l;
17648 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017649 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 p += l; /* skip multi-byte character */
17651 else
17652#endif
17653 {
17654 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17655 ++p;
17656 }
17657 }
17658}
17659
17660/*
17661 * "toupper(string)" function
17662 */
17663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017664f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017665 typval_T *argvars;
17666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017668 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017669 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670}
17671
17672/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017673 * "tr(string, fromstr, tostr)" function
17674 */
17675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017676f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017677 typval_T *argvars;
17678 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017679{
17680 char_u *instr;
17681 char_u *fromstr;
17682 char_u *tostr;
17683 char_u *p;
17684#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017685 int inlen;
17686 int fromlen;
17687 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017688 int idx;
17689 char_u *cpstr;
17690 int cplen;
17691 int first = TRUE;
17692#endif
17693 char_u buf[NUMBUFLEN];
17694 char_u buf2[NUMBUFLEN];
17695 garray_T ga;
17696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017697 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017698 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17699 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017700
17701 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017702 rettv->v_type = VAR_STRING;
17703 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017704 if (fromstr == NULL || tostr == NULL)
17705 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017706 ga_init2(&ga, (int)sizeof(char), 80);
17707
17708#ifdef FEAT_MBYTE
17709 if (!has_mbyte)
17710#endif
17711 /* not multi-byte: fromstr and tostr must be the same length */
17712 if (STRLEN(fromstr) != STRLEN(tostr))
17713 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017714#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017715error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017716#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017717 EMSG2(_(e_invarg2), fromstr);
17718 ga_clear(&ga);
17719 return;
17720 }
17721
17722 /* fromstr and tostr have to contain the same number of chars */
17723 while (*instr != NUL)
17724 {
17725#ifdef FEAT_MBYTE
17726 if (has_mbyte)
17727 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017728 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017729 cpstr = instr;
17730 cplen = inlen;
17731 idx = 0;
17732 for (p = fromstr; *p != NUL; p += fromlen)
17733 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017734 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017735 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17736 {
17737 for (p = tostr; *p != NUL; p += tolen)
17738 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017739 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017740 if (idx-- == 0)
17741 {
17742 cplen = tolen;
17743 cpstr = p;
17744 break;
17745 }
17746 }
17747 if (*p == NUL) /* tostr is shorter than fromstr */
17748 goto error;
17749 break;
17750 }
17751 ++idx;
17752 }
17753
17754 if (first && cpstr == instr)
17755 {
17756 /* Check that fromstr and tostr have the same number of
17757 * (multi-byte) characters. Done only once when a character
17758 * of instr doesn't appear in fromstr. */
17759 first = FALSE;
17760 for (p = tostr; *p != NUL; p += tolen)
17761 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017762 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017763 --idx;
17764 }
17765 if (idx != 0)
17766 goto error;
17767 }
17768
17769 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017770 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017771 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017772
17773 instr += inlen;
17774 }
17775 else
17776#endif
17777 {
17778 /* When not using multi-byte chars we can do it faster. */
17779 p = vim_strchr(fromstr, *instr);
17780 if (p != NULL)
17781 ga_append(&ga, tostr[p - fromstr]);
17782 else
17783 ga_append(&ga, *instr);
17784 ++instr;
17785 }
17786 }
17787
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017788 /* add a terminating NUL */
17789 ga_grow(&ga, 1);
17790 ga_append(&ga, NUL);
17791
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017792 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017793}
17794
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017795#ifdef FEAT_FLOAT
17796/*
17797 * "trunc({float})" function
17798 */
17799 static void
17800f_trunc(argvars, rettv)
17801 typval_T *argvars;
17802 typval_T *rettv;
17803{
17804 float_T f;
17805
17806 rettv->v_type = VAR_FLOAT;
17807 if (get_float_arg(argvars, &f) == OK)
17808 /* trunc() is not in C90, use floor() or ceil() instead. */
17809 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17810 else
17811 rettv->vval.v_float = 0.0;
17812}
17813#endif
17814
Bram Moolenaar8299df92004-07-10 09:47:34 +000017815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816 * "type(expr)" function
17817 */
17818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017819f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017820 typval_T *argvars;
17821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017823 int n;
17824
17825 switch (argvars[0].v_type)
17826 {
17827 case VAR_NUMBER: n = 0; break;
17828 case VAR_STRING: n = 1; break;
17829 case VAR_FUNC: n = 2; break;
17830 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017831 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017832#ifdef FEAT_FLOAT
17833 case VAR_FLOAT: n = 5; break;
17834#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017835 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17836 }
17837 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838}
17839
17840/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017841 * "undofile(name)" function
17842 */
17843 static void
17844f_undofile(argvars, rettv)
17845 typval_T *argvars;
17846 typval_T *rettv;
17847{
17848 rettv->v_type = VAR_STRING;
17849#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017850 {
17851 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17852
17853 if (ffname != NULL)
17854 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17855 vim_free(ffname);
17856 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017857#else
17858 rettv->vval.v_string = NULL;
17859#endif
17860}
17861
17862/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017863 * "undotree()" function
17864 */
17865 static void
17866f_undotree(argvars, rettv)
17867 typval_T *argvars UNUSED;
17868 typval_T *rettv;
17869{
17870 if (rettv_dict_alloc(rettv) == OK)
17871 {
17872 dict_T *dict = rettv->vval.v_dict;
17873 list_T *list;
17874
Bram Moolenaar730cde92010-06-27 05:18:54 +020017875 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017876 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017877 dict_add_nr_str(dict, "save_last",
17878 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017879 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17880 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017881 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017882
17883 list = list_alloc();
17884 if (list != NULL)
17885 {
17886 u_eval_tree(curbuf->b_u_oldhead, list);
17887 dict_add_list(dict, "entries", list);
17888 }
17889 }
17890}
17891
17892/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017893 * "values(dict)" function
17894 */
17895 static void
17896f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017897 typval_T *argvars;
17898 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017899{
17900 dict_list(argvars, rettv, 1);
17901}
17902
17903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 * "virtcol(string)" function
17905 */
17906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017907f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017908 typval_T *argvars;
17909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910{
17911 colnr_T vcol = 0;
17912 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017913 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017915 fp = var2fpos(&argvars[0], FALSE, &fnum);
17916 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17917 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918 {
17919 getvvcol(curwin, fp, NULL, NULL, &vcol);
17920 ++vcol;
17921 }
17922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017923 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924}
17925
17926/*
17927 * "visualmode()" function
17928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017930f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017931 typval_T *argvars UNUSED;
17932 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933{
17934#ifdef FEAT_VISUAL
17935 char_u str[2];
17936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017937 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 str[0] = curbuf->b_visual_mode_eval;
17939 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017940 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941
17942 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017943 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945#endif
17946}
17947
17948/*
17949 * "winbufnr(nr)" function
17950 */
17951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017952f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017953 typval_T *argvars;
17954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955{
17956 win_T *wp;
17957
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017958 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017960 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017962 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963}
17964
17965/*
17966 * "wincol()" function
17967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017969f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017970 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972{
17973 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017974 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975}
17976
17977/*
17978 * "winheight(nr)" function
17979 */
17980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017981f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017982 typval_T *argvars;
17983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984{
17985 win_T *wp;
17986
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017987 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017989 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017991 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992}
17993
17994/*
17995 * "winline()" function
17996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017998f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017999 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001{
18002 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018003 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004}
18005
18006/*
18007 * "winnr()" function
18008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018010f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018011 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013{
18014 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018015
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018017 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018019 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020}
18021
18022/*
18023 * "winrestcmd()" function
18024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018026f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018027 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029{
18030#ifdef FEAT_WINDOWS
18031 win_T *wp;
18032 int winnr = 1;
18033 garray_T ga;
18034 char_u buf[50];
18035
18036 ga_init2(&ga, (int)sizeof(char), 70);
18037 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18038 {
18039 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18040 ga_concat(&ga, buf);
18041# ifdef FEAT_VERTSPLIT
18042 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18043 ga_concat(&ga, buf);
18044# endif
18045 ++winnr;
18046 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018047 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018049 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018051 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018053 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054}
18055
18056/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018057 * "winrestview()" function
18058 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018059 static void
18060f_winrestview(argvars, rettv)
18061 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018062 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018063{
18064 dict_T *dict;
18065
18066 if (argvars[0].v_type != VAR_DICT
18067 || (dict = argvars[0].vval.v_dict) == NULL)
18068 EMSG(_(e_invarg));
18069 else
18070 {
18071 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18072 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18073#ifdef FEAT_VIRTUALEDIT
18074 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18075#endif
18076 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018077 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018078
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018079 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018080#ifdef FEAT_DIFF
18081 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18082#endif
18083 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18084 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18085
18086 check_cursor();
18087 changed_cline_bef_curs();
18088 invalidate_botline();
18089 redraw_later(VALID);
18090
18091 if (curwin->w_topline == 0)
18092 curwin->w_topline = 1;
18093 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18094 curwin->w_topline = curbuf->b_ml.ml_line_count;
18095#ifdef FEAT_DIFF
18096 check_topfill(curwin, TRUE);
18097#endif
18098 }
18099}
18100
18101/*
18102 * "winsaveview()" function
18103 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018104 static void
18105f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018106 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018107 typval_T *rettv;
18108{
18109 dict_T *dict;
18110
Bram Moolenaara800b422010-06-27 01:15:55 +020018111 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018112 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018113 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018114
18115 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18116 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18117#ifdef FEAT_VIRTUALEDIT
18118 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18119#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018120 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018121 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18122
18123 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18124#ifdef FEAT_DIFF
18125 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18126#endif
18127 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18128 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18129}
18130
18131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 * "winwidth(nr)" function
18133 */
18134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018135f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018136 typval_T *argvars;
18137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138{
18139 win_T *wp;
18140
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018141 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018143 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144 else
18145#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018146 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018148 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149#endif
18150}
18151
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018153 * "writefile()" function
18154 */
18155 static void
18156f_writefile(argvars, rettv)
18157 typval_T *argvars;
18158 typval_T *rettv;
18159{
18160 int binary = FALSE;
18161 char_u *fname;
18162 FILE *fd;
18163 listitem_T *li;
18164 char_u *s;
18165 int ret = 0;
18166 int c;
18167
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018168 if (check_restricted() || check_secure())
18169 return;
18170
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018171 if (argvars[0].v_type != VAR_LIST)
18172 {
18173 EMSG2(_(e_listarg), "writefile()");
18174 return;
18175 }
18176 if (argvars[0].vval.v_list == NULL)
18177 return;
18178
18179 if (argvars[2].v_type != VAR_UNKNOWN
18180 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18181 binary = TRUE;
18182
18183 /* Always open the file in binary mode, library functions have a mind of
18184 * their own about CR-LF conversion. */
18185 fname = get_tv_string(&argvars[1]);
18186 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18187 {
18188 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18189 ret = -1;
18190 }
18191 else
18192 {
18193 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18194 li = li->li_next)
18195 {
18196 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18197 {
18198 if (*s == '\n')
18199 c = putc(NUL, fd);
18200 else
18201 c = putc(*s, fd);
18202 if (c == EOF)
18203 {
18204 ret = -1;
18205 break;
18206 }
18207 }
18208 if (!binary || li->li_next != NULL)
18209 if (putc('\n', fd) == EOF)
18210 {
18211 ret = -1;
18212 break;
18213 }
18214 if (ret < 0)
18215 {
18216 EMSG(_(e_write));
18217 break;
18218 }
18219 }
18220 fclose(fd);
18221 }
18222
18223 rettv->vval.v_number = ret;
18224}
18225
18226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018228 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 */
18230 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018231var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018232 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018233 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018234 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018236 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018238 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239
Bram Moolenaara5525202006-03-02 22:52:09 +000018240 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018241 if (varp->v_type == VAR_LIST)
18242 {
18243 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018244 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018245 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018246 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018247
18248 l = varp->vval.v_list;
18249 if (l == NULL)
18250 return NULL;
18251
18252 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018253 pos.lnum = list_find_nr(l, 0L, &error);
18254 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018255 return NULL; /* invalid line number */
18256
18257 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018258 pos.col = list_find_nr(l, 1L, &error);
18259 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018260 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018261 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018262
18263 /* We accept "$" for the column number: last column. */
18264 li = list_find(l, 1L);
18265 if (li != NULL && li->li_tv.v_type == VAR_STRING
18266 && li->li_tv.vval.v_string != NULL
18267 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18268 pos.col = len + 1;
18269
Bram Moolenaara5525202006-03-02 22:52:09 +000018270 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018271 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018272 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018273 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018274
Bram Moolenaara5525202006-03-02 22:52:09 +000018275#ifdef FEAT_VIRTUALEDIT
18276 /* Get the virtual offset. Defaults to zero. */
18277 pos.coladd = list_find_nr(l, 2L, &error);
18278 if (error)
18279 pos.coladd = 0;
18280#endif
18281
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018282 return &pos;
18283 }
18284
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018285 name = get_tv_string_chk(varp);
18286 if (name == NULL)
18287 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018288 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018290#ifdef FEAT_VISUAL
18291 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18292 {
18293 if (VIsual_active)
18294 return &VIsual;
18295 return &curwin->w_cursor;
18296 }
18297#endif
18298 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018300 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18302 return NULL;
18303 return pp;
18304 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018305
18306#ifdef FEAT_VIRTUALEDIT
18307 pos.coladd = 0;
18308#endif
18309
Bram Moolenaar477933c2007-07-17 14:32:23 +000018310 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018311 {
18312 pos.col = 0;
18313 if (name[1] == '0') /* "w0": first visible line */
18314 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018315 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018316 pos.lnum = curwin->w_topline;
18317 return &pos;
18318 }
18319 else if (name[1] == '$') /* "w$": last visible line */
18320 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018321 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018322 pos.lnum = curwin->w_botline - 1;
18323 return &pos;
18324 }
18325 }
18326 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018328 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329 {
18330 pos.lnum = curbuf->b_ml.ml_line_count;
18331 pos.col = 0;
18332 }
18333 else
18334 {
18335 pos.lnum = curwin->w_cursor.lnum;
18336 pos.col = (colnr_T)STRLEN(ml_get_curline());
18337 }
18338 return &pos;
18339 }
18340 return NULL;
18341}
18342
18343/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018344 * Convert list in "arg" into a position and optional file number.
18345 * When "fnump" is NULL there is no file number, only 3 items.
18346 * Note that the column is passed on as-is, the caller may want to decrement
18347 * it to use 1 for the first column.
18348 * Return FAIL when conversion is not possible, doesn't check the position for
18349 * validity.
18350 */
18351 static int
18352list2fpos(arg, posp, fnump)
18353 typval_T *arg;
18354 pos_T *posp;
18355 int *fnump;
18356{
18357 list_T *l = arg->vval.v_list;
18358 long i = 0;
18359 long n;
18360
Bram Moolenaarbde35262006-07-23 20:12:24 +000018361 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18362 * when "fnump" isn't NULL and "coladd" is optional. */
18363 if (arg->v_type != VAR_LIST
18364 || l == NULL
18365 || l->lv_len < (fnump == NULL ? 2 : 3)
18366 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018367 return FAIL;
18368
18369 if (fnump != NULL)
18370 {
18371 n = list_find_nr(l, i++, NULL); /* fnum */
18372 if (n < 0)
18373 return FAIL;
18374 if (n == 0)
18375 n = curbuf->b_fnum; /* current buffer */
18376 *fnump = n;
18377 }
18378
18379 n = list_find_nr(l, i++, NULL); /* lnum */
18380 if (n < 0)
18381 return FAIL;
18382 posp->lnum = n;
18383
18384 n = list_find_nr(l, i++, NULL); /* col */
18385 if (n < 0)
18386 return FAIL;
18387 posp->col = n;
18388
18389#ifdef FEAT_VIRTUALEDIT
18390 n = list_find_nr(l, i, NULL);
18391 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018392 posp->coladd = 0;
18393 else
18394 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018395#endif
18396
18397 return OK;
18398}
18399
18400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 * Get the length of an environment variable name.
18402 * Advance "arg" to the first character after the name.
18403 * Return 0 for error.
18404 */
18405 static int
18406get_env_len(arg)
18407 char_u **arg;
18408{
18409 char_u *p;
18410 int len;
18411
18412 for (p = *arg; vim_isIDc(*p); ++p)
18413 ;
18414 if (p == *arg) /* no name found */
18415 return 0;
18416
18417 len = (int)(p - *arg);
18418 *arg = p;
18419 return len;
18420}
18421
18422/*
18423 * Get the length of the name of a function or internal variable.
18424 * "arg" is advanced to the first non-white character after the name.
18425 * Return 0 if something is wrong.
18426 */
18427 static int
18428get_id_len(arg)
18429 char_u **arg;
18430{
18431 char_u *p;
18432 int len;
18433
18434 /* Find the end of the name. */
18435 for (p = *arg; eval_isnamec(*p); ++p)
18436 ;
18437 if (p == *arg) /* no name found */
18438 return 0;
18439
18440 len = (int)(p - *arg);
18441 *arg = skipwhite(p);
18442
18443 return len;
18444}
18445
18446/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018447 * Get the length of the name of a variable or function.
18448 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018450 * Return -1 if curly braces expansion failed.
18451 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452 * If the name contains 'magic' {}'s, expand them and return the
18453 * expanded name in an allocated string via 'alias' - caller must free.
18454 */
18455 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018456get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457 char_u **arg;
18458 char_u **alias;
18459 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018460 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461{
18462 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 char_u *p;
18464 char_u *expr_start;
18465 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466
18467 *alias = NULL; /* default to no alias */
18468
18469 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18470 && (*arg)[2] == (int)KE_SNR)
18471 {
18472 /* hard coded <SNR>, already translated */
18473 *arg += 3;
18474 return get_id_len(arg) + 3;
18475 }
18476 len = eval_fname_script(*arg);
18477 if (len > 0)
18478 {
18479 /* literal "<SID>", "s:" or "<SNR>" */
18480 *arg += len;
18481 }
18482
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018484 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018486 p = find_name_end(*arg, &expr_start, &expr_end,
18487 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 if (expr_start != NULL)
18489 {
18490 char_u *temp_string;
18491
18492 if (!evaluate)
18493 {
18494 len += (int)(p - *arg);
18495 *arg = skipwhite(p);
18496 return len;
18497 }
18498
18499 /*
18500 * Include any <SID> etc in the expanded string:
18501 * Thus the -len here.
18502 */
18503 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18504 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018505 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506 *alias = temp_string;
18507 *arg = skipwhite(p);
18508 return (int)STRLEN(temp_string);
18509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018510
18511 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018512 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513 EMSG2(_(e_invexpr2), *arg);
18514
18515 return len;
18516}
18517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018518/*
18519 * Find the end of a variable or function name, taking care of magic braces.
18520 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18521 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018522 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018523 * Return a pointer to just after the name. Equal to "arg" if there is no
18524 * valid name.
18525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018527find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528 char_u *arg;
18529 char_u **expr_start;
18530 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018531 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018533 int mb_nest = 0;
18534 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535 char_u *p;
18536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018537 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018539 *expr_start = NULL;
18540 *expr_end = NULL;
18541 }
18542
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018543 /* Quick check for valid starting character. */
18544 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18545 return arg;
18546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018547 for (p = arg; *p != NUL
18548 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018549 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018550 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018551 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018552 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018553 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018554 if (*p == '\'')
18555 {
18556 /* skip over 'string' to avoid counting [ and ] inside it. */
18557 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18558 ;
18559 if (*p == NUL)
18560 break;
18561 }
18562 else if (*p == '"')
18563 {
18564 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18565 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18566 if (*p == '\\' && p[1] != NUL)
18567 ++p;
18568 if (*p == NUL)
18569 break;
18570 }
18571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018574 if (*p == '[')
18575 ++br_nest;
18576 else if (*p == ']')
18577 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018580 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018582 if (*p == '{')
18583 {
18584 mb_nest++;
18585 if (expr_start != NULL && *expr_start == NULL)
18586 *expr_start = p;
18587 }
18588 else if (*p == '}')
18589 {
18590 mb_nest--;
18591 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18592 *expr_end = p;
18593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 }
18596
18597 return p;
18598}
18599
18600/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018601 * Expands out the 'magic' {}'s in a variable/function name.
18602 * Note that this can call itself recursively, to deal with
18603 * constructs like foo{bar}{baz}{bam}
18604 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18605 * "in_start" ^
18606 * "expr_start" ^
18607 * "expr_end" ^
18608 * "in_end" ^
18609 *
18610 * Returns a new allocated string, which the caller must free.
18611 * Returns NULL for failure.
18612 */
18613 static char_u *
18614make_expanded_name(in_start, expr_start, expr_end, in_end)
18615 char_u *in_start;
18616 char_u *expr_start;
18617 char_u *expr_end;
18618 char_u *in_end;
18619{
18620 char_u c1;
18621 char_u *retval = NULL;
18622 char_u *temp_result;
18623 char_u *nextcmd = NULL;
18624
18625 if (expr_end == NULL || in_end == NULL)
18626 return NULL;
18627 *expr_start = NUL;
18628 *expr_end = NUL;
18629 c1 = *in_end;
18630 *in_end = NUL;
18631
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018632 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018633 if (temp_result != NULL && nextcmd == NULL)
18634 {
18635 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18636 + (in_end - expr_end) + 1));
18637 if (retval != NULL)
18638 {
18639 STRCPY(retval, in_start);
18640 STRCAT(retval, temp_result);
18641 STRCAT(retval, expr_end + 1);
18642 }
18643 }
18644 vim_free(temp_result);
18645
18646 *in_end = c1; /* put char back for error messages */
18647 *expr_start = '{';
18648 *expr_end = '}';
18649
18650 if (retval != NULL)
18651 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018652 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018653 if (expr_start != NULL)
18654 {
18655 /* Further expansion! */
18656 temp_result = make_expanded_name(retval, expr_start,
18657 expr_end, temp_result);
18658 vim_free(retval);
18659 retval = temp_result;
18660 }
18661 }
18662
18663 return retval;
18664}
18665
18666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018668 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669 */
18670 static int
18671eval_isnamec(c)
18672 int c;
18673{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018674 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18675}
18676
18677/*
18678 * Return TRUE if character "c" can be used as the first character in a
18679 * variable or function name (excluding '{' and '}').
18680 */
18681 static int
18682eval_isnamec1(c)
18683 int c;
18684{
18685 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686}
18687
18688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689 * Set number v: variable to "val".
18690 */
18691 void
18692set_vim_var_nr(idx, val)
18693 int idx;
18694 long val;
18695{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018696 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697}
18698
18699/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018700 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701 */
18702 long
18703get_vim_var_nr(idx)
18704 int idx;
18705{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018706 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707}
18708
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018709/*
18710 * Get string v: variable value. Uses a static buffer, can only be used once.
18711 */
18712 char_u *
18713get_vim_var_str(idx)
18714 int idx;
18715{
18716 return get_tv_string(&vimvars[idx].vv_tv);
18717}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018718
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018720 * Get List v: variable value. Caller must take care of reference count when
18721 * needed.
18722 */
18723 list_T *
18724get_vim_var_list(idx)
18725 int idx;
18726{
18727 return vimvars[idx].vv_list;
18728}
18729
18730/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018731 * Set v:char to character "c".
18732 */
18733 void
18734set_vim_var_char(c)
18735 int c;
18736{
18737#ifdef FEAT_MBYTE
18738 char_u buf[MB_MAXBYTES];
18739#else
18740 char_u buf[2];
18741#endif
18742
18743#ifdef FEAT_MBYTE
18744 if (has_mbyte)
18745 buf[(*mb_char2bytes)(c, buf)] = NUL;
18746 else
18747#endif
18748 {
18749 buf[0] = c;
18750 buf[1] = NUL;
18751 }
18752 set_vim_var_string(VV_CHAR, buf, -1);
18753}
18754
18755/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018756 * Set v:count to "count" and v:count1 to "count1".
18757 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 */
18759 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018760set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 long count;
18762 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018763 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018765 if (set_prevcount)
18766 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018767 vimvars[VV_COUNT].vv_nr = count;
18768 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769}
18770
18771/*
18772 * Set string v: variable to a copy of "val".
18773 */
18774 void
18775set_vim_var_string(idx, val, len)
18776 int idx;
18777 char_u *val;
18778 int len; /* length of "val" to use or -1 (whole string) */
18779{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018780 /* Need to do this (at least) once, since we can't initialize a union.
18781 * Will always be invoked when "v:progname" is set. */
18782 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18783
Bram Moolenaare9a41262005-01-15 22:18:47 +000018784 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018786 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018788 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018790 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791}
18792
18793/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018794 * Set List v: variable to "val".
18795 */
18796 void
18797set_vim_var_list(idx, val)
18798 int idx;
18799 list_T *val;
18800{
18801 list_unref(vimvars[idx].vv_list);
18802 vimvars[idx].vv_list = val;
18803 if (val != NULL)
18804 ++val->lv_refcount;
18805}
18806
18807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808 * Set v:register if needed.
18809 */
18810 void
18811set_reg_var(c)
18812 int c;
18813{
18814 char_u regname;
18815
18816 if (c == 0 || c == ' ')
18817 regname = '"';
18818 else
18819 regname = c;
18820 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018821 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 set_vim_var_string(VV_REG, &regname, 1);
18823}
18824
18825/*
18826 * Get or set v:exception. If "oldval" == NULL, return the current value.
18827 * Otherwise, restore the value to "oldval" and return NULL.
18828 * Must always be called in pairs to save and restore v:exception! Does not
18829 * take care of memory allocations.
18830 */
18831 char_u *
18832v_exception(oldval)
18833 char_u *oldval;
18834{
18835 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018836 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837
Bram Moolenaare9a41262005-01-15 22:18:47 +000018838 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839 return NULL;
18840}
18841
18842/*
18843 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18844 * Otherwise, restore the value to "oldval" and return NULL.
18845 * Must always be called in pairs to save and restore v:throwpoint! Does not
18846 * take care of memory allocations.
18847 */
18848 char_u *
18849v_throwpoint(oldval)
18850 char_u *oldval;
18851{
18852 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018853 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854
Bram Moolenaare9a41262005-01-15 22:18:47 +000018855 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 return NULL;
18857}
18858
18859#if defined(FEAT_AUTOCMD) || defined(PROTO)
18860/*
18861 * Set v:cmdarg.
18862 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18863 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18864 * Must always be called in pairs!
18865 */
18866 char_u *
18867set_cmdarg(eap, oldarg)
18868 exarg_T *eap;
18869 char_u *oldarg;
18870{
18871 char_u *oldval;
18872 char_u *newval;
18873 unsigned len;
18874
Bram Moolenaare9a41262005-01-15 22:18:47 +000018875 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018876 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018878 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018879 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018880 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 }
18882
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018883 if (eap->force_bin == FORCE_BIN)
18884 len = 6;
18885 else if (eap->force_bin == FORCE_NOBIN)
18886 len = 8;
18887 else
18888 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018889
18890 if (eap->read_edit)
18891 len += 7;
18892
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018893 if (eap->force_ff != 0)
18894 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18895# ifdef FEAT_MBYTE
18896 if (eap->force_enc != 0)
18897 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018898 if (eap->bad_char != 0)
18899 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018900# endif
18901
18902 newval = alloc(len + 1);
18903 if (newval == NULL)
18904 return NULL;
18905
18906 if (eap->force_bin == FORCE_BIN)
18907 sprintf((char *)newval, " ++bin");
18908 else if (eap->force_bin == FORCE_NOBIN)
18909 sprintf((char *)newval, " ++nobin");
18910 else
18911 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018912
18913 if (eap->read_edit)
18914 STRCAT(newval, " ++edit");
18915
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018916 if (eap->force_ff != 0)
18917 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18918 eap->cmd + eap->force_ff);
18919# ifdef FEAT_MBYTE
18920 if (eap->force_enc != 0)
18921 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18922 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018923 if (eap->bad_char == BAD_KEEP)
18924 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18925 else if (eap->bad_char == BAD_DROP)
18926 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18927 else if (eap->bad_char != 0)
18928 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018929# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018930 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018931 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932}
18933#endif
18934
18935/*
18936 * Get the value of internal variable "name".
18937 * Return OK or FAIL.
18938 */
18939 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018940get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 char_u *name;
18942 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018943 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018944 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945{
18946 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018947 typval_T *tv = NULL;
18948 typval_T atv;
18949 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951
18952 /* truncate the name, so that we can use strcmp() */
18953 cc = name[len];
18954 name[len] = NUL;
18955
18956 /*
18957 * Check for "b:changedtick".
18958 */
18959 if (STRCMP(name, "b:changedtick") == 0)
18960 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018961 atv.v_type = VAR_NUMBER;
18962 atv.vval.v_number = curbuf->b_changedtick;
18963 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964 }
18965
18966 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 * Check for user-defined variables.
18968 */
18969 else
18970 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018971 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018973 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 }
18975
Bram Moolenaare9a41262005-01-15 22:18:47 +000018976 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018978 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018979 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 ret = FAIL;
18981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018982 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018983 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984
18985 name[len] = cc;
18986
18987 return ret;
18988}
18989
18990/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018991 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18992 * Also handle function call with Funcref variable: func(expr)
18993 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18994 */
18995 static int
18996handle_subscript(arg, rettv, evaluate, verbose)
18997 char_u **arg;
18998 typval_T *rettv;
18999 int evaluate; /* do more than finding the end */
19000 int verbose; /* give error messages */
19001{
19002 int ret = OK;
19003 dict_T *selfdict = NULL;
19004 char_u *s;
19005 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019006 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019007
19008 while (ret == OK
19009 && (**arg == '['
19010 || (**arg == '.' && rettv->v_type == VAR_DICT)
19011 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19012 && !vim_iswhite(*(*arg - 1)))
19013 {
19014 if (**arg == '(')
19015 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019016 /* need to copy the funcref so that we can clear rettv */
19017 functv = *rettv;
19018 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019019
19020 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019021 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019022 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019023 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19024 &len, evaluate, selfdict);
19025
19026 /* Clear the funcref afterwards, so that deleting it while
19027 * evaluating the arguments is possible (see test55). */
19028 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019029
19030 /* Stop the expression evaluation when immediately aborting on
19031 * error, or when an interrupt occurred or an exception was thrown
19032 * but not caught. */
19033 if (aborting())
19034 {
19035 if (ret == OK)
19036 clear_tv(rettv);
19037 ret = FAIL;
19038 }
19039 dict_unref(selfdict);
19040 selfdict = NULL;
19041 }
19042 else /* **arg == '[' || **arg == '.' */
19043 {
19044 dict_unref(selfdict);
19045 if (rettv->v_type == VAR_DICT)
19046 {
19047 selfdict = rettv->vval.v_dict;
19048 if (selfdict != NULL)
19049 ++selfdict->dv_refcount;
19050 }
19051 else
19052 selfdict = NULL;
19053 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19054 {
19055 clear_tv(rettv);
19056 ret = FAIL;
19057 }
19058 }
19059 }
19060 dict_unref(selfdict);
19061 return ret;
19062}
19063
19064/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019065 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019066 * value).
19067 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019068 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019069alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019070{
Bram Moolenaar33570922005-01-25 22:26:29 +000019071 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019072}
19073
19074/*
19075 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 * The string "s" must have been allocated, it is consumed.
19077 * Return NULL for out of memory, the variable otherwise.
19078 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019079 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019080alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 char_u *s;
19082{
Bram Moolenaar33570922005-01-25 22:26:29 +000019083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019085 rettv = alloc_tv();
19086 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019088 rettv->v_type = VAR_STRING;
19089 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 }
19091 else
19092 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019093 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094}
19095
19096/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019097 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019099 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019100free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019101 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102{
19103 if (varp != NULL)
19104 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019105 switch (varp->v_type)
19106 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019107 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019108 func_unref(varp->vval.v_string);
19109 /*FALLTHROUGH*/
19110 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019111 vim_free(varp->vval.v_string);
19112 break;
19113 case VAR_LIST:
19114 list_unref(varp->vval.v_list);
19115 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019116 case VAR_DICT:
19117 dict_unref(varp->vval.v_dict);
19118 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019119 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019120#ifdef FEAT_FLOAT
19121 case VAR_FLOAT:
19122#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019123 case VAR_UNKNOWN:
19124 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019125 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019126 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019127 break;
19128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 vim_free(varp);
19130 }
19131}
19132
19133/*
19134 * Free the memory for a variable value and set the value to NULL or 0.
19135 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019136 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019137clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019138 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139{
19140 if (varp != NULL)
19141 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019142 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019144 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019145 func_unref(varp->vval.v_string);
19146 /*FALLTHROUGH*/
19147 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019148 vim_free(varp->vval.v_string);
19149 varp->vval.v_string = NULL;
19150 break;
19151 case VAR_LIST:
19152 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019153 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019154 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019155 case VAR_DICT:
19156 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019157 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019158 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019159 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019160 varp->vval.v_number = 0;
19161 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019162#ifdef FEAT_FLOAT
19163 case VAR_FLOAT:
19164 varp->vval.v_float = 0.0;
19165 break;
19166#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019167 case VAR_UNKNOWN:
19168 break;
19169 default:
19170 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019172 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 }
19174}
19175
19176/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019177 * Set the value of a variable to NULL without freeing items.
19178 */
19179 static void
19180init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019181 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019182{
19183 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019184 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019185}
19186
19187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 * Get the number value of a variable.
19189 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019190 * For incompatible types, return 0.
19191 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19192 * caller of incompatible types: it sets *denote to TRUE if "denote"
19193 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019194 */
19195 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019196get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019197 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019199 int error = FALSE;
19200
19201 return get_tv_number_chk(varp, &error); /* return 0L on error */
19202}
19203
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019204 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019205get_tv_number_chk(varp, denote)
19206 typval_T *varp;
19207 int *denote;
19208{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019209 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019211 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019213 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019214 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019215#ifdef FEAT_FLOAT
19216 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019217 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019218 break;
19219#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019220 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019221 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019222 break;
19223 case VAR_STRING:
19224 if (varp->vval.v_string != NULL)
19225 vim_str2nr(varp->vval.v_string, NULL, NULL,
19226 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019227 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019228 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019229 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019230 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019231 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019232 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019233 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019234 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019235 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019236 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019238 if (denote == NULL) /* useful for values that must be unsigned */
19239 n = -1;
19240 else
19241 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019242 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243}
19244
19245/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019246 * Get the lnum from the first argument.
19247 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019248 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 */
19250 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019251get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019252 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253{
Bram Moolenaar33570922005-01-25 22:26:29 +000019254 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255 linenr_T lnum;
19256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019257 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258 if (lnum == 0) /* no valid number, try using line() */
19259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019260 rettv.v_type = VAR_NUMBER;
19261 f_line(argvars, &rettv);
19262 lnum = rettv.vval.v_number;
19263 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264 }
19265 return lnum;
19266}
19267
19268/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019269 * Get the lnum from the first argument.
19270 * Also accepts "$", then "buf" is used.
19271 * Returns 0 on error.
19272 */
19273 static linenr_T
19274get_tv_lnum_buf(argvars, buf)
19275 typval_T *argvars;
19276 buf_T *buf;
19277{
19278 if (argvars[0].v_type == VAR_STRING
19279 && argvars[0].vval.v_string != NULL
19280 && argvars[0].vval.v_string[0] == '$'
19281 && buf != NULL)
19282 return buf->b_ml.ml_line_count;
19283 return get_tv_number_chk(&argvars[0], NULL);
19284}
19285
19286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 * Get the string value of a variable.
19288 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019289 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19290 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291 * If the String variable has never been set, return an empty string.
19292 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019293 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19294 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 */
19296 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019297get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019298 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299{
19300 static char_u mybuf[NUMBUFLEN];
19301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019302 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303}
19304
19305 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019306get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019307 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019308 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019310 char_u *res = get_tv_string_buf_chk(varp, buf);
19311
19312 return res != NULL ? res : (char_u *)"";
19313}
19314
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019315 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019316get_tv_string_chk(varp)
19317 typval_T *varp;
19318{
19319 static char_u mybuf[NUMBUFLEN];
19320
19321 return get_tv_string_buf_chk(varp, mybuf);
19322}
19323
19324 static char_u *
19325get_tv_string_buf_chk(varp, buf)
19326 typval_T *varp;
19327 char_u *buf;
19328{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019329 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019331 case VAR_NUMBER:
19332 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19333 return buf;
19334 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019335 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019336 break;
19337 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019338 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019339 break;
19340 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019341 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019342 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019343#ifdef FEAT_FLOAT
19344 case VAR_FLOAT:
19345 EMSG(_("E806: using Float as a String"));
19346 break;
19347#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019348 case VAR_STRING:
19349 if (varp->vval.v_string != NULL)
19350 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019351 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019352 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019353 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019354 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019356 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357}
19358
19359/*
19360 * Find variable "name" in the list of variables.
19361 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019362 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019363 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019364 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019366 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019367find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019369 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019372 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373
Bram Moolenaara7043832005-01-21 11:56:39 +000019374 ht = find_var_ht(name, &varname);
19375 if (htp != NULL)
19376 *htp = ht;
19377 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019379 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380}
19381
19382/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019383 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019384 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019386 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019387find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019388 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019389 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019390 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019391{
Bram Moolenaar33570922005-01-25 22:26:29 +000019392 hashitem_T *hi;
19393
19394 if (*varname == NUL)
19395 {
19396 /* Must be something like "s:", otherwise "ht" would be NULL. */
19397 switch (varname[-2])
19398 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019399 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019400 case 'g': return &globvars_var;
19401 case 'v': return &vimvars_var;
19402 case 'b': return &curbuf->b_bufvar;
19403 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019404#ifdef FEAT_WINDOWS
19405 case 't': return &curtab->tp_winvar;
19406#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019407 case 'l': return current_funccal == NULL
19408 ? NULL : &current_funccal->l_vars_var;
19409 case 'a': return current_funccal == NULL
19410 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019411 }
19412 return NULL;
19413 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019414
19415 hi = hash_find(ht, varname);
19416 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019417 {
19418 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019419 * worked find the variable again. Don't auto-load a script if it was
19420 * loaded already, otherwise it would be loaded every time when
19421 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019422 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019423 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019424 hi = hash_find(ht, varname);
19425 if (HASHITEM_EMPTY(hi))
19426 return NULL;
19427 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019428 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019429}
19430
19431/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019432 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019433 * Set "varname" to the start of name without ':'.
19434 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019435 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019436find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437 char_u *name;
19438 char_u **varname;
19439{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019440 hashitem_T *hi;
19441
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442 if (name[1] != ':')
19443 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019444 /* The name must not start with a colon or #. */
19445 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 return NULL;
19447 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019448
19449 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019450 hi = hash_find(&compat_hashtab, name);
19451 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019452 return &compat_hashtab;
19453
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019455 return &globvarht; /* global variable */
19456 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 }
19458 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019459 if (*name == 'g') /* global variable */
19460 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019461 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19462 */
19463 if (vim_strchr(name + 2, ':') != NULL
19464 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019465 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019467 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019469 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019470#ifdef FEAT_WINDOWS
19471 if (*name == 't') /* tab page variable */
19472 return &curtab->tp_vars.dv_hashtab;
19473#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019474 if (*name == 'v') /* v: variable */
19475 return &vimvarht;
19476 if (*name == 'a' && current_funccal != NULL) /* function argument */
19477 return &current_funccal->l_avars.dv_hashtab;
19478 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19479 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 if (*name == 's' /* script variable */
19481 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19482 return &SCRIPT_VARS(current_SID);
19483 return NULL;
19484}
19485
19486/*
19487 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019488 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 * Returns NULL when it doesn't exist.
19490 */
19491 char_u *
19492get_var_value(name)
19493 char_u *name;
19494{
Bram Moolenaar33570922005-01-25 22:26:29 +000019495 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496
Bram Moolenaara7043832005-01-21 11:56:39 +000019497 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498 if (v == NULL)
19499 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019500 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501}
19502
19503/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019504 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 * sourcing this script and when executing functions defined in the script.
19506 */
19507 void
19508new_script_vars(id)
19509 scid_T id;
19510{
Bram Moolenaara7043832005-01-21 11:56:39 +000019511 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019512 hashtab_T *ht;
19513 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019514
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19516 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019517 /* Re-allocating ga_data means that an ht_array pointing to
19518 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019519 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019520 for (i = 1; i <= ga_scripts.ga_len; ++i)
19521 {
19522 ht = &SCRIPT_VARS(i);
19523 if (ht->ht_mask == HT_INIT_SIZE - 1)
19524 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019525 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019526 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019527 }
19528
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 while (ga_scripts.ga_len < id)
19530 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019531 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019532 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019533 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 }
19536 }
19537}
19538
19539/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019540 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19541 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 */
19543 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019544init_var_dict(dict, dict_var)
19545 dict_T *dict;
19546 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547{
Bram Moolenaar33570922005-01-25 22:26:29 +000019548 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019549 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019550 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019551 dict_var->di_tv.vval.v_dict = dict;
19552 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019553 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019554 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19555 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556}
19557
19558/*
19559 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019560 * Frees all allocated variables and the value they contain.
19561 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562 */
19563 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019564vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019565 hashtab_T *ht;
19566{
19567 vars_clear_ext(ht, TRUE);
19568}
19569
19570/*
19571 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19572 */
19573 static void
19574vars_clear_ext(ht, free_val)
19575 hashtab_T *ht;
19576 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577{
Bram Moolenaara7043832005-01-21 11:56:39 +000019578 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019579 hashitem_T *hi;
19580 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581
Bram Moolenaar33570922005-01-25 22:26:29 +000019582 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019583 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019584 for (hi = ht->ht_array; todo > 0; ++hi)
19585 {
19586 if (!HASHITEM_EMPTY(hi))
19587 {
19588 --todo;
19589
Bram Moolenaar33570922005-01-25 22:26:29 +000019590 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019591 * ht_array might change then. hash_clear() takes care of it
19592 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019593 v = HI2DI(hi);
19594 if (free_val)
19595 clear_tv(&v->di_tv);
19596 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19597 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019598 }
19599 }
19600 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019601 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602}
19603
Bram Moolenaara7043832005-01-21 11:56:39 +000019604/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019605 * Delete a variable from hashtab "ht" at item "hi".
19606 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019609delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019610 hashtab_T *ht;
19611 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612{
Bram Moolenaar33570922005-01-25 22:26:29 +000019613 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019614
19615 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019616 clear_tv(&di->di_tv);
19617 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618}
19619
19620/*
19621 * List the value of one internal variable.
19622 */
19623 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019624list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019627 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019629 char_u *tofree;
19630 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019631 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019632
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019633 current_copyID += COPYID_INC;
19634 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019635 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019636 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019637 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638}
19639
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019641list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642 char_u *prefix;
19643 char_u *name;
19644 int type;
19645 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019646 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647{
Bram Moolenaar31859182007-08-14 20:41:13 +000019648 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19649 msg_start();
19650 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 if (name != NULL) /* "a:" vars don't have a name stored */
19652 msg_puts(name);
19653 msg_putchar(' ');
19654 msg_advance(22);
19655 if (type == VAR_NUMBER)
19656 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019657 else if (type == VAR_FUNC)
19658 msg_putchar('*');
19659 else if (type == VAR_LIST)
19660 {
19661 msg_putchar('[');
19662 if (*string == '[')
19663 ++string;
19664 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019665 else if (type == VAR_DICT)
19666 {
19667 msg_putchar('{');
19668 if (*string == '{')
19669 ++string;
19670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 else
19672 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019673
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019675
19676 if (type == VAR_FUNC)
19677 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019678 if (*first)
19679 {
19680 msg_clr_eos();
19681 *first = FALSE;
19682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683}
19684
19685/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019686 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 * If the variable already exists, the value is updated.
19688 * Otherwise the variable is created.
19689 */
19690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019691set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019693 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019694 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695{
Bram Moolenaar33570922005-01-25 22:26:29 +000019696 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019698 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019699 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019701 ht = find_var_ht(name, &varname);
19702 if (ht == NULL || *varname == NUL)
19703 {
19704 EMSG2(_(e_illvar), name);
19705 return;
19706 }
19707 v = find_var_in_ht(ht, varname, TRUE);
19708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019709 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019710 {
19711 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19712 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19713 ? name[2] : name[0]))
19714 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019715 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019716 return;
19717 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019718 /* Don't allow hiding a function. When "v" is not NULL we migth be
19719 * assigning another function to the same var, the type is checked
19720 * below. */
19721 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019722 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019723 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019724 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019725 return;
19726 }
19727 }
19728
Bram Moolenaar33570922005-01-25 22:26:29 +000019729 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019731 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019732 if (var_check_ro(v->di_flags, name)
19733 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019734 return;
19735 if (v->di_tv.v_type != tv->v_type
19736 && !((v->di_tv.v_type == VAR_STRING
19737 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019738 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019739 || tv->v_type == VAR_NUMBER))
19740#ifdef FEAT_FLOAT
19741 && !((v->di_tv.v_type == VAR_NUMBER
19742 || v->di_tv.v_type == VAR_FLOAT)
19743 && (tv->v_type == VAR_NUMBER
19744 || tv->v_type == VAR_FLOAT))
19745#endif
19746 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019747 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019748 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019749 return;
19750 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019751
19752 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019753 * Handle setting internal v: variables separately: we don't change
19754 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019755 */
19756 if (ht == &vimvarht)
19757 {
19758 if (v->di_tv.v_type == VAR_STRING)
19759 {
19760 vim_free(v->di_tv.vval.v_string);
19761 if (copy || tv->v_type != VAR_STRING)
19762 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19763 else
19764 {
19765 /* Take over the string to avoid an extra alloc/free. */
19766 v->di_tv.vval.v_string = tv->vval.v_string;
19767 tv->vval.v_string = NULL;
19768 }
19769 }
19770 else if (v->di_tv.v_type != VAR_NUMBER)
19771 EMSG2(_(e_intern2), "set_var()");
19772 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019773 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019774 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019775 if (STRCMP(varname, "searchforward") == 0)
19776 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19777 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019778 return;
19779 }
19780
19781 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 }
19783 else /* add a new variable */
19784 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019785 /* Can't add "v:" variable. */
19786 if (ht == &vimvarht)
19787 {
19788 EMSG2(_(e_illvar), name);
19789 return;
19790 }
19791
Bram Moolenaar92124a32005-06-17 22:03:40 +000019792 /* Make sure the variable name is valid. */
19793 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019794 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19795 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019796 {
19797 EMSG2(_(e_illvar), varname);
19798 return;
19799 }
19800
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019801 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19802 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019803 if (v == NULL)
19804 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019805 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019808 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 return;
19810 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019811 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019813
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019814 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019815 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019816 else
19817 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019818 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019819 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019820 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822}
19823
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019824/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019825 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019826 * Also give an error message.
19827 */
19828 static int
19829var_check_ro(flags, name)
19830 int flags;
19831 char_u *name;
19832{
19833 if (flags & DI_FLAGS_RO)
19834 {
19835 EMSG2(_(e_readonlyvar), name);
19836 return TRUE;
19837 }
19838 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19839 {
19840 EMSG2(_(e_readonlysbx), name);
19841 return TRUE;
19842 }
19843 return FALSE;
19844}
19845
19846/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019847 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19848 * Also give an error message.
19849 */
19850 static int
19851var_check_fixed(flags, name)
19852 int flags;
19853 char_u *name;
19854{
19855 if (flags & DI_FLAGS_FIX)
19856 {
19857 EMSG2(_("E795: Cannot delete variable %s"), name);
19858 return TRUE;
19859 }
19860 return FALSE;
19861}
19862
19863/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019864 * Return TRUE if typeval "tv" is set to be locked (immutable).
19865 * Also give an error message, using "name".
19866 */
19867 static int
19868tv_check_lock(lock, name)
19869 int lock;
19870 char_u *name;
19871{
19872 if (lock & VAR_LOCKED)
19873 {
19874 EMSG2(_("E741: Value is locked: %s"),
19875 name == NULL ? (char_u *)_("Unknown") : name);
19876 return TRUE;
19877 }
19878 if (lock & VAR_FIXED)
19879 {
19880 EMSG2(_("E742: Cannot change value of %s"),
19881 name == NULL ? (char_u *)_("Unknown") : name);
19882 return TRUE;
19883 }
19884 return FALSE;
19885}
19886
19887/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019888 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019889 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019890 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019891 * It is OK for "from" and "to" to point to the same item. This is used to
19892 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019893 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019894 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019895copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019896 typval_T *from;
19897 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019899 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019900 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019901 switch (from->v_type)
19902 {
19903 case VAR_NUMBER:
19904 to->vval.v_number = from->vval.v_number;
19905 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019906#ifdef FEAT_FLOAT
19907 case VAR_FLOAT:
19908 to->vval.v_float = from->vval.v_float;
19909 break;
19910#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019911 case VAR_STRING:
19912 case VAR_FUNC:
19913 if (from->vval.v_string == NULL)
19914 to->vval.v_string = NULL;
19915 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019916 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019917 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019918 if (from->v_type == VAR_FUNC)
19919 func_ref(to->vval.v_string);
19920 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019921 break;
19922 case VAR_LIST:
19923 if (from->vval.v_list == NULL)
19924 to->vval.v_list = NULL;
19925 else
19926 {
19927 to->vval.v_list = from->vval.v_list;
19928 ++to->vval.v_list->lv_refcount;
19929 }
19930 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019931 case VAR_DICT:
19932 if (from->vval.v_dict == NULL)
19933 to->vval.v_dict = NULL;
19934 else
19935 {
19936 to->vval.v_dict = from->vval.v_dict;
19937 ++to->vval.v_dict->dv_refcount;
19938 }
19939 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019940 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019941 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019942 break;
19943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944}
19945
19946/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019947 * Make a copy of an item.
19948 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019949 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19950 * reference to an already copied list/dict can be used.
19951 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019952 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019953 static int
19954item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019955 typval_T *from;
19956 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019957 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019958 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019959{
19960 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019961 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019962
Bram Moolenaar33570922005-01-25 22:26:29 +000019963 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019964 {
19965 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019966 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019967 }
19968 ++recurse;
19969
19970 switch (from->v_type)
19971 {
19972 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019973#ifdef FEAT_FLOAT
19974 case VAR_FLOAT:
19975#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019976 case VAR_STRING:
19977 case VAR_FUNC:
19978 copy_tv(from, to);
19979 break;
19980 case VAR_LIST:
19981 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019982 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019983 if (from->vval.v_list == NULL)
19984 to->vval.v_list = NULL;
19985 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19986 {
19987 /* use the copy made earlier */
19988 to->vval.v_list = from->vval.v_list->lv_copylist;
19989 ++to->vval.v_list->lv_refcount;
19990 }
19991 else
19992 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19993 if (to->vval.v_list == NULL)
19994 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019995 break;
19996 case VAR_DICT:
19997 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019998 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019999 if (from->vval.v_dict == NULL)
20000 to->vval.v_dict = NULL;
20001 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20002 {
20003 /* use the copy made earlier */
20004 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20005 ++to->vval.v_dict->dv_refcount;
20006 }
20007 else
20008 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20009 if (to->vval.v_dict == NULL)
20010 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020011 break;
20012 default:
20013 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020014 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020015 }
20016 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020017 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020018}
20019
20020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021 * ":echo expr1 ..." print each argument separated with a space, add a
20022 * newline at the end.
20023 * ":echon expr1 ..." print each argument plain.
20024 */
20025 void
20026ex_echo(eap)
20027 exarg_T *eap;
20028{
20029 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020030 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020031 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 char_u *p;
20033 int needclr = TRUE;
20034 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020035 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036
20037 if (eap->skip)
20038 ++emsg_skip;
20039 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20040 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020041 /* If eval1() causes an error message the text from the command may
20042 * still need to be cleared. E.g., "echo 22,44". */
20043 need_clr_eos = needclr;
20044
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020046 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 {
20048 /*
20049 * Report the invalid expression unless the expression evaluation
20050 * has been cancelled due to an aborting error, an interrupt, or an
20051 * exception.
20052 */
20053 if (!aborting())
20054 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020055 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 break;
20057 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020058 need_clr_eos = FALSE;
20059
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 if (!eap->skip)
20061 {
20062 if (atstart)
20063 {
20064 atstart = FALSE;
20065 /* Call msg_start() after eval1(), evaluating the expression
20066 * may cause a message to appear. */
20067 if (eap->cmdidx == CMD_echo)
20068 msg_start();
20069 }
20070 else if (eap->cmdidx == CMD_echo)
20071 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020072 current_copyID += COPYID_INC;
20073 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020074 if (p != NULL)
20075 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020077 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020079 if (*p != TAB && needclr)
20080 {
20081 /* remove any text still there from the command */
20082 msg_clr_eos();
20083 needclr = FALSE;
20084 }
20085 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086 }
20087 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020088 {
20089#ifdef FEAT_MBYTE
20090 if (has_mbyte)
20091 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020092 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020093
20094 (void)msg_outtrans_len_attr(p, i, echo_attr);
20095 p += i - 1;
20096 }
20097 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020099 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020102 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020104 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 arg = skipwhite(arg);
20106 }
20107 eap->nextcmd = check_nextcmd(arg);
20108
20109 if (eap->skip)
20110 --emsg_skip;
20111 else
20112 {
20113 /* remove text that may still be there from the command */
20114 if (needclr)
20115 msg_clr_eos();
20116 if (eap->cmdidx == CMD_echo)
20117 msg_end();
20118 }
20119}
20120
20121/*
20122 * ":echohl {name}".
20123 */
20124 void
20125ex_echohl(eap)
20126 exarg_T *eap;
20127{
20128 int id;
20129
20130 id = syn_name2id(eap->arg);
20131 if (id == 0)
20132 echo_attr = 0;
20133 else
20134 echo_attr = syn_id2attr(id);
20135}
20136
20137/*
20138 * ":execute expr1 ..." execute the result of an expression.
20139 * ":echomsg expr1 ..." Print a message
20140 * ":echoerr expr1 ..." Print an error
20141 * Each gets spaces around each argument and a newline at the end for
20142 * echo commands
20143 */
20144 void
20145ex_execute(eap)
20146 exarg_T *eap;
20147{
20148 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020149 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 int ret = OK;
20151 char_u *p;
20152 garray_T ga;
20153 int len;
20154 int save_did_emsg;
20155
20156 ga_init2(&ga, 1, 80);
20157
20158 if (eap->skip)
20159 ++emsg_skip;
20160 while (*arg != NUL && *arg != '|' && *arg != '\n')
20161 {
20162 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020163 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 {
20165 /*
20166 * Report the invalid expression unless the expression evaluation
20167 * has been cancelled due to an aborting error, an interrupt, or an
20168 * exception.
20169 */
20170 if (!aborting())
20171 EMSG2(_(e_invexpr2), p);
20172 ret = FAIL;
20173 break;
20174 }
20175
20176 if (!eap->skip)
20177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020178 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 len = (int)STRLEN(p);
20180 if (ga_grow(&ga, len + 2) == FAIL)
20181 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020182 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 ret = FAIL;
20184 break;
20185 }
20186 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 ga.ga_len += len;
20190 }
20191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020192 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 arg = skipwhite(arg);
20194 }
20195
20196 if (ret != FAIL && ga.ga_data != NULL)
20197 {
20198 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020201 out_flush();
20202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 else if (eap->cmdidx == CMD_echoerr)
20204 {
20205 /* We don't want to abort following commands, restore did_emsg. */
20206 save_did_emsg = did_emsg;
20207 EMSG((char_u *)ga.ga_data);
20208 if (!force_abort)
20209 did_emsg = save_did_emsg;
20210 }
20211 else if (eap->cmdidx == CMD_execute)
20212 do_cmdline((char_u *)ga.ga_data,
20213 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20214 }
20215
20216 ga_clear(&ga);
20217
20218 if (eap->skip)
20219 --emsg_skip;
20220
20221 eap->nextcmd = check_nextcmd(arg);
20222}
20223
20224/*
20225 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20226 * "arg" points to the "&" or '+' when called, to "option" when returning.
20227 * Returns NULL when no option name found. Otherwise pointer to the char
20228 * after the option name.
20229 */
20230 static char_u *
20231find_option_end(arg, opt_flags)
20232 char_u **arg;
20233 int *opt_flags;
20234{
20235 char_u *p = *arg;
20236
20237 ++p;
20238 if (*p == 'g' && p[1] == ':')
20239 {
20240 *opt_flags = OPT_GLOBAL;
20241 p += 2;
20242 }
20243 else if (*p == 'l' && p[1] == ':')
20244 {
20245 *opt_flags = OPT_LOCAL;
20246 p += 2;
20247 }
20248 else
20249 *opt_flags = 0;
20250
20251 if (!ASCII_ISALPHA(*p))
20252 return NULL;
20253 *arg = p;
20254
20255 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20256 p += 4; /* termcap option */
20257 else
20258 while (ASCII_ISALPHA(*p))
20259 ++p;
20260 return p;
20261}
20262
20263/*
20264 * ":function"
20265 */
20266 void
20267ex_function(eap)
20268 exarg_T *eap;
20269{
20270 char_u *theline;
20271 int j;
20272 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 char_u *name = NULL;
20275 char_u *p;
20276 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020277 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 garray_T newargs;
20279 garray_T newlines;
20280 int varargs = FALSE;
20281 int mustend = FALSE;
20282 int flags = 0;
20283 ufunc_T *fp;
20284 int indent;
20285 int nesting;
20286 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020287 dictitem_T *v;
20288 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020289 static int func_nr = 0; /* number for nameless function */
20290 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020291 hashtab_T *ht;
20292 int todo;
20293 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020294 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295
20296 /*
20297 * ":function" without argument: list functions.
20298 */
20299 if (ends_excmd(*eap->arg))
20300 {
20301 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020302 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020303 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020304 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020305 {
20306 if (!HASHITEM_EMPTY(hi))
20307 {
20308 --todo;
20309 fp = HI2UF(hi);
20310 if (!isdigit(*fp->uf_name))
20311 list_func_head(fp, FALSE);
20312 }
20313 }
20314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 eap->nextcmd = check_nextcmd(eap->arg);
20316 return;
20317 }
20318
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020319 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020320 * ":function /pat": list functions matching pattern.
20321 */
20322 if (*eap->arg == '/')
20323 {
20324 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20325 if (!eap->skip)
20326 {
20327 regmatch_T regmatch;
20328
20329 c = *p;
20330 *p = NUL;
20331 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20332 *p = c;
20333 if (regmatch.regprog != NULL)
20334 {
20335 regmatch.rm_ic = p_ic;
20336
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020337 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020338 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20339 {
20340 if (!HASHITEM_EMPTY(hi))
20341 {
20342 --todo;
20343 fp = HI2UF(hi);
20344 if (!isdigit(*fp->uf_name)
20345 && vim_regexec(&regmatch, fp->uf_name, 0))
20346 list_func_head(fp, FALSE);
20347 }
20348 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020349 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020350 }
20351 }
20352 if (*p == '/')
20353 ++p;
20354 eap->nextcmd = check_nextcmd(p);
20355 return;
20356 }
20357
20358 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020359 * Get the function name. There are these situations:
20360 * func normal function name
20361 * "name" == func, "fudi.fd_dict" == NULL
20362 * dict.func new dictionary entry
20363 * "name" == NULL, "fudi.fd_dict" set,
20364 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20365 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020366 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020367 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20368 * dict.func existing dict entry that's not a Funcref
20369 * "name" == NULL, "fudi.fd_dict" set,
20370 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020373 name = trans_function_name(&p, eap->skip, 0, &fudi);
20374 paren = (vim_strchr(p, '(') != NULL);
20375 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 {
20377 /*
20378 * Return on an invalid expression in braces, unless the expression
20379 * evaluation has been cancelled due to an aborting error, an
20380 * interrupt, or an exception.
20381 */
20382 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020383 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020384 if (!eap->skip && fudi.fd_newkey != NULL)
20385 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020386 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389 else
20390 eap->skip = TRUE;
20391 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020392
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 /* An error in a function call during evaluation of an expression in magic
20394 * braces should not cause the function not to be defined. */
20395 saved_did_emsg = did_emsg;
20396 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397
20398 /*
20399 * ":function func" with only function name: list function.
20400 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020401 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402 {
20403 if (!ends_excmd(*skipwhite(p)))
20404 {
20405 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020406 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 }
20408 eap->nextcmd = check_nextcmd(p);
20409 if (eap->nextcmd != NULL)
20410 *p = NUL;
20411 if (!eap->skip && !got_int)
20412 {
20413 fp = find_func(name);
20414 if (fp != NULL)
20415 {
20416 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020417 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020419 if (FUNCLINE(fp, j) == NULL)
20420 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 msg_putchar('\n');
20422 msg_outnum((long)(j + 1));
20423 if (j < 9)
20424 msg_putchar(' ');
20425 if (j < 99)
20426 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020427 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 out_flush(); /* show a line at a time */
20429 ui_breakcheck();
20430 }
20431 if (!got_int)
20432 {
20433 msg_putchar('\n');
20434 msg_puts((char_u *)" endfunction");
20435 }
20436 }
20437 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020438 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020440 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 }
20442
20443 /*
20444 * ":function name(arg1, arg2)" Define function.
20445 */
20446 p = skipwhite(p);
20447 if (*p != '(')
20448 {
20449 if (!eap->skip)
20450 {
20451 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020452 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 }
20454 /* attempt to continue by skipping some text */
20455 if (vim_strchr(p, '(') != NULL)
20456 p = vim_strchr(p, '(');
20457 }
20458 p = skipwhite(p + 1);
20459
20460 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20461 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20462
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020463 if (!eap->skip)
20464 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020465 /* Check the name of the function. Unless it's a dictionary function
20466 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020467 if (name != NULL)
20468 arg = name;
20469 else
20470 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020471 if (arg != NULL && (fudi.fd_di == NULL
20472 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020473 {
20474 if (*arg == K_SPECIAL)
20475 j = 3;
20476 else
20477 j = 0;
20478 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20479 : eval_isnamec(arg[j])))
20480 ++j;
20481 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020482 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020483 }
20484 }
20485
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486 /*
20487 * Isolate the arguments: "arg1, arg2, ...)"
20488 */
20489 while (*p != ')')
20490 {
20491 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20492 {
20493 varargs = TRUE;
20494 p += 3;
20495 mustend = TRUE;
20496 }
20497 else
20498 {
20499 arg = p;
20500 while (ASCII_ISALNUM(*p) || *p == '_')
20501 ++p;
20502 if (arg == p || isdigit(*arg)
20503 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20504 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20505 {
20506 if (!eap->skip)
20507 EMSG2(_("E125: Illegal argument: %s"), arg);
20508 break;
20509 }
20510 if (ga_grow(&newargs, 1) == FAIL)
20511 goto erret;
20512 c = *p;
20513 *p = NUL;
20514 arg = vim_strsave(arg);
20515 if (arg == NULL)
20516 goto erret;
20517 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20518 *p = c;
20519 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 if (*p == ',')
20521 ++p;
20522 else
20523 mustend = TRUE;
20524 }
20525 p = skipwhite(p);
20526 if (mustend && *p != ')')
20527 {
20528 if (!eap->skip)
20529 EMSG2(_(e_invarg2), eap->arg);
20530 break;
20531 }
20532 }
20533 ++p; /* skip the ')' */
20534
Bram Moolenaare9a41262005-01-15 22:18:47 +000020535 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 for (;;)
20537 {
20538 p = skipwhite(p);
20539 if (STRNCMP(p, "range", 5) == 0)
20540 {
20541 flags |= FC_RANGE;
20542 p += 5;
20543 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020544 else if (STRNCMP(p, "dict", 4) == 0)
20545 {
20546 flags |= FC_DICT;
20547 p += 4;
20548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549 else if (STRNCMP(p, "abort", 5) == 0)
20550 {
20551 flags |= FC_ABORT;
20552 p += 5;
20553 }
20554 else
20555 break;
20556 }
20557
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020558 /* When there is a line break use what follows for the function body.
20559 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20560 if (*p == '\n')
20561 line_arg = p + 1;
20562 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 EMSG(_(e_trailing));
20564
20565 /*
20566 * Read the body of the function, until ":endfunction" is found.
20567 */
20568 if (KeyTyped)
20569 {
20570 /* Check if the function already exists, don't let the user type the
20571 * whole function before telling him it doesn't work! For a script we
20572 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020573 if (!eap->skip && !eap->forceit)
20574 {
20575 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20576 EMSG(_(e_funcdict));
20577 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020578 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020581 if (!eap->skip && did_emsg)
20582 goto erret;
20583
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 msg_putchar('\n'); /* don't overwrite the function name */
20585 cmdline_row = msg_row;
20586 }
20587
20588 indent = 2;
20589 nesting = 0;
20590 for (;;)
20591 {
20592 msg_scroll = TRUE;
20593 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020594 sourcing_lnum_off = sourcing_lnum;
20595
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020596 if (line_arg != NULL)
20597 {
20598 /* Use eap->arg, split up in parts by line breaks. */
20599 theline = line_arg;
20600 p = vim_strchr(theline, '\n');
20601 if (p == NULL)
20602 line_arg += STRLEN(line_arg);
20603 else
20604 {
20605 *p = NUL;
20606 line_arg = p + 1;
20607 }
20608 }
20609 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 theline = getcmdline(':', 0L, indent);
20611 else
20612 theline = eap->getline(':', eap->cookie, indent);
20613 if (KeyTyped)
20614 lines_left = Rows - 1;
20615 if (theline == NULL)
20616 {
20617 EMSG(_("E126: Missing :endfunction"));
20618 goto erret;
20619 }
20620
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020621 /* Detect line continuation: sourcing_lnum increased more than one. */
20622 if (sourcing_lnum > sourcing_lnum_off + 1)
20623 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20624 else
20625 sourcing_lnum_off = 0;
20626
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 if (skip_until != NULL)
20628 {
20629 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20630 * don't check for ":endfunc". */
20631 if (STRCMP(theline, skip_until) == 0)
20632 {
20633 vim_free(skip_until);
20634 skip_until = NULL;
20635 }
20636 }
20637 else
20638 {
20639 /* skip ':' and blanks*/
20640 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20641 ;
20642
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020643 /* Check for "endfunction". */
20644 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020646 if (line_arg == NULL)
20647 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648 break;
20649 }
20650
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020651 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652 * at "end". */
20653 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20654 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020655 else if (STRNCMP(p, "if", 2) == 0
20656 || STRNCMP(p, "wh", 2) == 0
20657 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 || STRNCMP(p, "try", 3) == 0)
20659 indent += 2;
20660
20661 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020662 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020664 if (*p == '!')
20665 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666 p += eval_fname_script(p);
20667 if (ASCII_ISALPHA(*p))
20668 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020669 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020670 if (*skipwhite(p) == '(')
20671 {
20672 ++nesting;
20673 indent += 2;
20674 }
20675 }
20676 }
20677
20678 /* Check for ":append" or ":insert". */
20679 p = skip_range(p, NULL);
20680 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20681 || (p[0] == 'i'
20682 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20683 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20684 skip_until = vim_strsave((char_u *)".");
20685
20686 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20687 arg = skipwhite(skiptowhite(p));
20688 if (arg[0] == '<' && arg[1] =='<'
20689 && ((p[0] == 'p' && p[1] == 'y'
20690 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20691 || (p[0] == 'p' && p[1] == 'e'
20692 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20693 || (p[0] == 't' && p[1] == 'c'
20694 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20695 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20696 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020697 || (p[0] == 'm' && p[1] == 'z'
20698 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 ))
20700 {
20701 /* ":python <<" continues until a dot, like ":append" */
20702 p = skipwhite(arg + 2);
20703 if (*p == NUL)
20704 skip_until = vim_strsave((char_u *)".");
20705 else
20706 skip_until = vim_strsave(p);
20707 }
20708 }
20709
20710 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020711 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020712 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020713 if (line_arg == NULL)
20714 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020716 }
20717
20718 /* Copy the line to newly allocated memory. get_one_sourceline()
20719 * allocates 250 bytes per line, this saves 80% on average. The cost
20720 * is an extra alloc/free. */
20721 p = vim_strsave(theline);
20722 if (p != NULL)
20723 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020724 if (line_arg == NULL)
20725 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020726 theline = p;
20727 }
20728
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020729 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20730
20731 /* Add NULL lines for continuation lines, so that the line count is
20732 * equal to the index in the growarray. */
20733 while (sourcing_lnum_off-- > 0)
20734 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020735
20736 /* Check for end of eap->arg. */
20737 if (line_arg != NULL && *line_arg == NUL)
20738 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739 }
20740
20741 /* Don't define the function when skipping commands or when an error was
20742 * detected. */
20743 if (eap->skip || did_emsg)
20744 goto erret;
20745
20746 /*
20747 * If there are no errors, add the function
20748 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020749 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020750 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020751 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020752 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020753 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020754 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020755 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020756 goto erret;
20757 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020758
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020759 fp = find_func(name);
20760 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020762 if (!eap->forceit)
20763 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020764 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020765 goto erret;
20766 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020767 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020768 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020769 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020770 name);
20771 goto erret;
20772 }
20773 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020774 ga_clear_strings(&(fp->uf_args));
20775 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020776 vim_free(name);
20777 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 }
20780 else
20781 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020782 char numbuf[20];
20783
20784 fp = NULL;
20785 if (fudi.fd_newkey == NULL && !eap->forceit)
20786 {
20787 EMSG(_(e_funcdict));
20788 goto erret;
20789 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020790 if (fudi.fd_di == NULL)
20791 {
20792 /* Can't add a function to a locked dictionary */
20793 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20794 goto erret;
20795 }
20796 /* Can't change an existing function if it is locked */
20797 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20798 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020799
20800 /* Give the function a sequential number. Can only be used with a
20801 * Funcref! */
20802 vim_free(name);
20803 sprintf(numbuf, "%d", ++func_nr);
20804 name = vim_strsave((char_u *)numbuf);
20805 if (name == NULL)
20806 goto erret;
20807 }
20808
20809 if (fp == NULL)
20810 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020811 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020812 {
20813 int slen, plen;
20814 char_u *scriptname;
20815
20816 /* Check that the autoload name matches the script name. */
20817 j = FAIL;
20818 if (sourcing_name != NULL)
20819 {
20820 scriptname = autoload_name(name);
20821 if (scriptname != NULL)
20822 {
20823 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020824 plen = (int)STRLEN(p);
20825 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020826 if (slen > plen && fnamecmp(p,
20827 sourcing_name + slen - plen) == 0)
20828 j = OK;
20829 vim_free(scriptname);
20830 }
20831 }
20832 if (j == FAIL)
20833 {
20834 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20835 goto erret;
20836 }
20837 }
20838
20839 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 if (fp == NULL)
20841 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020842
20843 if (fudi.fd_dict != NULL)
20844 {
20845 if (fudi.fd_di == NULL)
20846 {
20847 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020848 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020849 if (fudi.fd_di == NULL)
20850 {
20851 vim_free(fp);
20852 goto erret;
20853 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020854 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20855 {
20856 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020857 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020858 goto erret;
20859 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020860 }
20861 else
20862 /* overwrite existing dict entry */
20863 clear_tv(&fudi.fd_di->di_tv);
20864 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020865 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020866 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020867 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020868
20869 /* behave like "dict" was used */
20870 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020871 }
20872
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020874 STRCPY(fp->uf_name, name);
20875 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020877 fp->uf_args = newargs;
20878 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020879#ifdef FEAT_PROFILE
20880 fp->uf_tml_count = NULL;
20881 fp->uf_tml_total = NULL;
20882 fp->uf_tml_self = NULL;
20883 fp->uf_profiling = FALSE;
20884 if (prof_def_func())
20885 func_do_profile(fp);
20886#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020887 fp->uf_varargs = varargs;
20888 fp->uf_flags = flags;
20889 fp->uf_calls = 0;
20890 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020891 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892
20893erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894 ga_clear_strings(&newargs);
20895 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020896ret_free:
20897 vim_free(skip_until);
20898 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901}
20902
20903/*
20904 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020905 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020907 * flags:
20908 * TFN_INT: internal function name OK
20909 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 * Advances "pp" to just after the function name (if no error).
20911 */
20912 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020913trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 char_u **pp;
20915 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020916 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020917 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020919 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 char_u *start;
20921 char_u *end;
20922 int lead;
20923 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020925 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020926
20927 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020928 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020930
20931 /* Check for hard coded <SNR>: already translated function ID (from a user
20932 * command). */
20933 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20934 && (*pp)[2] == (int)KE_SNR)
20935 {
20936 *pp += 3;
20937 len = get_id_len(pp) + 3;
20938 return vim_strnsave(start, len);
20939 }
20940
20941 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20942 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020944 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020946
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020947 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20948 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020949 if (end == start)
20950 {
20951 if (!skip)
20952 EMSG(_("E129: Function name required"));
20953 goto theend;
20954 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020955 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020956 {
20957 /*
20958 * Report an invalid expression in braces, unless the expression
20959 * evaluation has been cancelled due to an aborting error, an
20960 * interrupt, or an exception.
20961 */
20962 if (!aborting())
20963 {
20964 if (end != NULL)
20965 EMSG2(_(e_invarg2), start);
20966 }
20967 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020968 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020969 goto theend;
20970 }
20971
20972 if (lv.ll_tv != NULL)
20973 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020974 if (fdp != NULL)
20975 {
20976 fdp->fd_dict = lv.ll_dict;
20977 fdp->fd_newkey = lv.ll_newkey;
20978 lv.ll_newkey = NULL;
20979 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020980 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020981 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20982 {
20983 name = vim_strsave(lv.ll_tv->vval.v_string);
20984 *pp = end;
20985 }
20986 else
20987 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020988 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20989 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020990 EMSG(_(e_funcref));
20991 else
20992 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020993 name = NULL;
20994 }
20995 goto theend;
20996 }
20997
20998 if (lv.ll_name == NULL)
20999 {
21000 /* Error found, but continue after the function name. */
21001 *pp = end;
21002 goto theend;
21003 }
21004
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021005 /* Check if the name is a Funcref. If so, use the value. */
21006 if (lv.ll_exp_name != NULL)
21007 {
21008 len = (int)STRLEN(lv.ll_exp_name);
21009 name = deref_func_name(lv.ll_exp_name, &len);
21010 if (name == lv.ll_exp_name)
21011 name = NULL;
21012 }
21013 else
21014 {
21015 len = (int)(end - *pp);
21016 name = deref_func_name(*pp, &len);
21017 if (name == *pp)
21018 name = NULL;
21019 }
21020 if (name != NULL)
21021 {
21022 name = vim_strsave(name);
21023 *pp = end;
21024 goto theend;
21025 }
21026
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021027 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021028 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021029 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021030 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21031 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21032 {
21033 /* When there was "s:" already or the name expanded to get a
21034 * leading "s:" then remove it. */
21035 lv.ll_name += 2;
21036 len -= 2;
21037 lead = 2;
21038 }
21039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021040 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021041 {
21042 if (lead == 2) /* skip over "s:" */
21043 lv.ll_name += 2;
21044 len = (int)(end - lv.ll_name);
21045 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021046
21047 /*
21048 * Copy the function name to allocated memory.
21049 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21050 * Accept <SNR>123_name() outside a script.
21051 */
21052 if (skip)
21053 lead = 0; /* do nothing */
21054 else if (lead > 0)
21055 {
21056 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021057 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21058 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021059 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021060 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021061 if (current_SID <= 0)
21062 {
21063 EMSG(_(e_usingsid));
21064 goto theend;
21065 }
21066 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21067 lead += (int)STRLEN(sid_buf);
21068 }
21069 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021070 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021071 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021072 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021073 goto theend;
21074 }
21075 name = alloc((unsigned)(len + lead + 1));
21076 if (name != NULL)
21077 {
21078 if (lead > 0)
21079 {
21080 name[0] = K_SPECIAL;
21081 name[1] = KS_EXTRA;
21082 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021083 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021084 STRCPY(name + 3, sid_buf);
21085 }
21086 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21087 name[len + lead] = NUL;
21088 }
21089 *pp = end;
21090
21091theend:
21092 clear_lval(&lv);
21093 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094}
21095
21096/*
21097 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21098 * Return 2 if "p" starts with "s:".
21099 * Return 0 otherwise.
21100 */
21101 static int
21102eval_fname_script(p)
21103 char_u *p;
21104{
21105 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21106 || STRNICMP(p + 1, "SNR>", 4) == 0))
21107 return 5;
21108 if (p[0] == 's' && p[1] == ':')
21109 return 2;
21110 return 0;
21111}
21112
21113/*
21114 * Return TRUE if "p" starts with "<SID>" or "s:".
21115 * Only works if eval_fname_script() returned non-zero for "p"!
21116 */
21117 static int
21118eval_fname_sid(p)
21119 char_u *p;
21120{
21121 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21122}
21123
21124/*
21125 * List the head of the function: "name(arg1, arg2)".
21126 */
21127 static void
21128list_func_head(fp, indent)
21129 ufunc_T *fp;
21130 int indent;
21131{
21132 int j;
21133
21134 msg_start();
21135 if (indent)
21136 MSG_PUTS(" ");
21137 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021138 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 {
21140 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021141 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 }
21143 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021144 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021146 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147 {
21148 if (j)
21149 MSG_PUTS(", ");
21150 msg_puts(FUNCARG(fp, j));
21151 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021152 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153 {
21154 if (j)
21155 MSG_PUTS(", ");
21156 MSG_PUTS("...");
21157 }
21158 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021159 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021160 if (p_verbose > 0)
21161 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162}
21163
21164/*
21165 * Find a function by name, return pointer to it in ufuncs.
21166 * Return NULL for unknown function.
21167 */
21168 static ufunc_T *
21169find_func(name)
21170 char_u *name;
21171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021172 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021174 hi = hash_find(&func_hashtab, name);
21175 if (!HASHITEM_EMPTY(hi))
21176 return HI2UF(hi);
21177 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178}
21179
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021180#if defined(EXITFREE) || defined(PROTO)
21181 void
21182free_all_functions()
21183{
21184 hashitem_T *hi;
21185
21186 /* Need to start all over every time, because func_free() may change the
21187 * hash table. */
21188 while (func_hashtab.ht_used > 0)
21189 for (hi = func_hashtab.ht_array; ; ++hi)
21190 if (!HASHITEM_EMPTY(hi))
21191 {
21192 func_free(HI2UF(hi));
21193 break;
21194 }
21195}
21196#endif
21197
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021198/*
21199 * Return TRUE if a function "name" exists.
21200 */
21201 static int
21202function_exists(name)
21203 char_u *name;
21204{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021205 char_u *nm = name;
21206 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021207 int n = FALSE;
21208
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021209 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021210 nm = skipwhite(nm);
21211
21212 /* Only accept "funcname", "funcname ", "funcname (..." and
21213 * "funcname(...", not "funcname!...". */
21214 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021215 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021216 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021217 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021218 else
21219 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021220 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021221 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021222 return n;
21223}
21224
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021225/*
21226 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021227 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021228 */
21229 static int
21230builtin_function(name)
21231 char_u *name;
21232{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021233 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21234 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021235}
21236
Bram Moolenaar05159a02005-02-26 23:04:13 +000021237#if defined(FEAT_PROFILE) || defined(PROTO)
21238/*
21239 * Start profiling function "fp".
21240 */
21241 static void
21242func_do_profile(fp)
21243 ufunc_T *fp;
21244{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021245 int len = fp->uf_lines.ga_len;
21246
21247 if (len == 0)
21248 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021249 fp->uf_tm_count = 0;
21250 profile_zero(&fp->uf_tm_self);
21251 profile_zero(&fp->uf_tm_total);
21252 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021253 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021254 if (fp->uf_tml_total == NULL)
21255 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021256 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021257 if (fp->uf_tml_self == NULL)
21258 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021259 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021260 fp->uf_tml_idx = -1;
21261 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21262 || fp->uf_tml_self == NULL)
21263 return; /* out of memory */
21264
21265 fp->uf_profiling = TRUE;
21266}
21267
21268/*
21269 * Dump the profiling results for all functions in file "fd".
21270 */
21271 void
21272func_dump_profile(fd)
21273 FILE *fd;
21274{
21275 hashitem_T *hi;
21276 int todo;
21277 ufunc_T *fp;
21278 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021279 ufunc_T **sorttab;
21280 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021281
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021282 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021283 if (todo == 0)
21284 return; /* nothing to dump */
21285
Bram Moolenaar73830342005-02-28 22:48:19 +000021286 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21287
Bram Moolenaar05159a02005-02-26 23:04:13 +000021288 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21289 {
21290 if (!HASHITEM_EMPTY(hi))
21291 {
21292 --todo;
21293 fp = HI2UF(hi);
21294 if (fp->uf_profiling)
21295 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021296 if (sorttab != NULL)
21297 sorttab[st_len++] = fp;
21298
Bram Moolenaar05159a02005-02-26 23:04:13 +000021299 if (fp->uf_name[0] == K_SPECIAL)
21300 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21301 else
21302 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21303 if (fp->uf_tm_count == 1)
21304 fprintf(fd, "Called 1 time\n");
21305 else
21306 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21307 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21308 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21309 fprintf(fd, "\n");
21310 fprintf(fd, "count total (s) self (s)\n");
21311
21312 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21313 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021314 if (FUNCLINE(fp, i) == NULL)
21315 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021316 prof_func_line(fd, fp->uf_tml_count[i],
21317 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021318 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21319 }
21320 fprintf(fd, "\n");
21321 }
21322 }
21323 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021324
21325 if (sorttab != NULL && st_len > 0)
21326 {
21327 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21328 prof_total_cmp);
21329 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21330 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21331 prof_self_cmp);
21332 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21333 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021334
21335 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021336}
Bram Moolenaar73830342005-02-28 22:48:19 +000021337
21338 static void
21339prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21340 FILE *fd;
21341 ufunc_T **sorttab;
21342 int st_len;
21343 char *title;
21344 int prefer_self; /* when equal print only self time */
21345{
21346 int i;
21347 ufunc_T *fp;
21348
21349 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21350 fprintf(fd, "count total (s) self (s) function\n");
21351 for (i = 0; i < 20 && i < st_len; ++i)
21352 {
21353 fp = sorttab[i];
21354 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21355 prefer_self);
21356 if (fp->uf_name[0] == K_SPECIAL)
21357 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21358 else
21359 fprintf(fd, " %s()\n", fp->uf_name);
21360 }
21361 fprintf(fd, "\n");
21362}
21363
21364/*
21365 * Print the count and times for one function or function line.
21366 */
21367 static void
21368prof_func_line(fd, count, total, self, prefer_self)
21369 FILE *fd;
21370 int count;
21371 proftime_T *total;
21372 proftime_T *self;
21373 int prefer_self; /* when equal print only self time */
21374{
21375 if (count > 0)
21376 {
21377 fprintf(fd, "%5d ", count);
21378 if (prefer_self && profile_equal(total, self))
21379 fprintf(fd, " ");
21380 else
21381 fprintf(fd, "%s ", profile_msg(total));
21382 if (!prefer_self && profile_equal(total, self))
21383 fprintf(fd, " ");
21384 else
21385 fprintf(fd, "%s ", profile_msg(self));
21386 }
21387 else
21388 fprintf(fd, " ");
21389}
21390
21391/*
21392 * Compare function for total time sorting.
21393 */
21394 static int
21395#ifdef __BORLANDC__
21396_RTLENTRYF
21397#endif
21398prof_total_cmp(s1, s2)
21399 const void *s1;
21400 const void *s2;
21401{
21402 ufunc_T *p1, *p2;
21403
21404 p1 = *(ufunc_T **)s1;
21405 p2 = *(ufunc_T **)s2;
21406 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21407}
21408
21409/*
21410 * Compare function for self time sorting.
21411 */
21412 static int
21413#ifdef __BORLANDC__
21414_RTLENTRYF
21415#endif
21416prof_self_cmp(s1, s2)
21417 const void *s1;
21418 const void *s2;
21419{
21420 ufunc_T *p1, *p2;
21421
21422 p1 = *(ufunc_T **)s1;
21423 p2 = *(ufunc_T **)s2;
21424 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21425}
21426
Bram Moolenaar05159a02005-02-26 23:04:13 +000021427#endif
21428
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021429/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021430 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021431 * Return TRUE if a package was loaded.
21432 */
21433 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021434script_autoload(name, reload)
21435 char_u *name;
21436 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021437{
21438 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021439 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021440 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021441 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021442
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021443 /* Return quickly when autoload disabled. */
21444 if (no_autoload)
21445 return FALSE;
21446
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021447 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021448 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021449 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021450 return FALSE;
21451
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021452 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021453
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021454 /* Find the name in the list of previously loaded package names. Skip
21455 * "autoload/", it's always the same. */
21456 for (i = 0; i < ga_loaded.ga_len; ++i)
21457 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21458 break;
21459 if (!reload && i < ga_loaded.ga_len)
21460 ret = FALSE; /* was loaded already */
21461 else
21462 {
21463 /* Remember the name if it wasn't loaded already. */
21464 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21465 {
21466 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21467 tofree = NULL;
21468 }
21469
21470 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021471 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021472 ret = TRUE;
21473 }
21474
21475 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021476 return ret;
21477}
21478
21479/*
21480 * Return the autoload script name for a function or variable name.
21481 * Returns NULL when out of memory.
21482 */
21483 static char_u *
21484autoload_name(name)
21485 char_u *name;
21486{
21487 char_u *p;
21488 char_u *scriptname;
21489
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021490 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021491 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21492 if (scriptname == NULL)
21493 return FALSE;
21494 STRCPY(scriptname, "autoload/");
21495 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021496 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021497 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021498 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021499 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021500 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021501}
21502
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21504
21505/*
21506 * Function given to ExpandGeneric() to obtain the list of user defined
21507 * function names.
21508 */
21509 char_u *
21510get_user_func_name(xp, idx)
21511 expand_T *xp;
21512 int idx;
21513{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021514 static long_u done;
21515 static hashitem_T *hi;
21516 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517
21518 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021520 done = 0;
21521 hi = func_hashtab.ht_array;
21522 }
21523 if (done < func_hashtab.ht_used)
21524 {
21525 if (done++ > 0)
21526 ++hi;
21527 while (HASHITEM_EMPTY(hi))
21528 ++hi;
21529 fp = HI2UF(hi);
21530
21531 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21532 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533
21534 cat_func_name(IObuff, fp);
21535 if (xp->xp_context != EXPAND_USER_FUNC)
21536 {
21537 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021538 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539 STRCAT(IObuff, ")");
21540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 return IObuff;
21542 }
21543 return NULL;
21544}
21545
21546#endif /* FEAT_CMDL_COMPL */
21547
21548/*
21549 * Copy the function name of "fp" to buffer "buf".
21550 * "buf" must be able to hold the function name plus three bytes.
21551 * Takes care of script-local function names.
21552 */
21553 static void
21554cat_func_name(buf, fp)
21555 char_u *buf;
21556 ufunc_T *fp;
21557{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021558 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 {
21560 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021561 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562 }
21563 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021564 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565}
21566
21567/*
21568 * ":delfunction {name}"
21569 */
21570 void
21571ex_delfunction(eap)
21572 exarg_T *eap;
21573{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021574 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 char_u *p;
21576 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021577 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578
21579 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021580 name = trans_function_name(&p, eap->skip, 0, &fudi);
21581 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021583 {
21584 if (fudi.fd_dict != NULL && !eap->skip)
21585 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 if (!ends_excmd(*skipwhite(p)))
21589 {
21590 vim_free(name);
21591 EMSG(_(e_trailing));
21592 return;
21593 }
21594 eap->nextcmd = check_nextcmd(p);
21595 if (eap->nextcmd != NULL)
21596 *p = NUL;
21597
21598 if (!eap->skip)
21599 fp = find_func(name);
21600 vim_free(name);
21601
21602 if (!eap->skip)
21603 {
21604 if (fp == NULL)
21605 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021606 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 return;
21608 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021609 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610 {
21611 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21612 return;
21613 }
21614
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021615 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021617 /* Delete the dict item that refers to the function, it will
21618 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021619 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021621 else
21622 func_free(fp);
21623 }
21624}
21625
21626/*
21627 * Free a function and remove it from the list of functions.
21628 */
21629 static void
21630func_free(fp)
21631 ufunc_T *fp;
21632{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021633 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021634
21635 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021636 ga_clear_strings(&(fp->uf_args));
21637 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021638#ifdef FEAT_PROFILE
21639 vim_free(fp->uf_tml_count);
21640 vim_free(fp->uf_tml_total);
21641 vim_free(fp->uf_tml_self);
21642#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021643
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021644 /* remove the function from the function hashtable */
21645 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21646 if (HASHITEM_EMPTY(hi))
21647 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021648 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021649 hash_remove(&func_hashtab, hi);
21650
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021651 vim_free(fp);
21652}
21653
21654/*
21655 * Unreference a Function: decrement the reference count and free it when it
21656 * becomes zero. Only for numbered functions.
21657 */
21658 static void
21659func_unref(name)
21660 char_u *name;
21661{
21662 ufunc_T *fp;
21663
21664 if (name != NULL && isdigit(*name))
21665 {
21666 fp = find_func(name);
21667 if (fp == NULL)
21668 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021669 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021670 {
21671 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021672 * when "uf_calls" becomes zero. */
21673 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021674 func_free(fp);
21675 }
21676 }
21677}
21678
21679/*
21680 * Count a reference to a Function.
21681 */
21682 static void
21683func_ref(name)
21684 char_u *name;
21685{
21686 ufunc_T *fp;
21687
21688 if (name != NULL && isdigit(*name))
21689 {
21690 fp = find_func(name);
21691 if (fp == NULL)
21692 EMSG2(_(e_intern2), "func_ref()");
21693 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021694 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 }
21696}
21697
21698/*
21699 * Call a user function.
21700 */
21701 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021702call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 ufunc_T *fp; /* pointer to function */
21704 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021705 typval_T *argvars; /* arguments */
21706 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 linenr_T firstline; /* first line of range */
21708 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021709 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710{
Bram Moolenaar33570922005-01-25 22:26:29 +000021711 char_u *save_sourcing_name;
21712 linenr_T save_sourcing_lnum;
21713 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021714 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021715 int save_did_emsg;
21716 static int depth = 0;
21717 dictitem_T *v;
21718 int fixvar_idx = 0; /* index in fixvar[] */
21719 int i;
21720 int ai;
21721 char_u numbuf[NUMBUFLEN];
21722 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021723#ifdef FEAT_PROFILE
21724 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021725 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727
21728 /* If depth of calling is getting too high, don't execute the function */
21729 if (depth >= p_mfd)
21730 {
21731 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021732 rettv->v_type = VAR_NUMBER;
21733 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 return;
21735 }
21736 ++depth;
21737
21738 line_breakcheck(); /* check for CTRL-C hit */
21739
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021740 fc = (funccall_T *)alloc(sizeof(funccall_T));
21741 fc->caller = current_funccal;
21742 current_funccal = fc;
21743 fc->func = fp;
21744 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021745 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021746 fc->linenr = 0;
21747 fc->returned = FALSE;
21748 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021750 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21751 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752
Bram Moolenaar33570922005-01-25 22:26:29 +000021753 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021754 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021755 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21756 * each argument variable and saves a lot of time.
21757 */
21758 /*
21759 * Init l: variables.
21760 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021761 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021762 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021763 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021764 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21765 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021766 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021767 name = v->di_key;
21768 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021769 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021770 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021771 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021772 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021773 v->di_tv.vval.v_dict = selfdict;
21774 ++selfdict->dv_refcount;
21775 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021776
Bram Moolenaar33570922005-01-25 22:26:29 +000021777 /*
21778 * Init a: variables.
21779 * Set a:0 to "argcount".
21780 * Set a:000 to a list with room for the "..." arguments.
21781 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021782 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21783 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021784 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021785 /* Use "name" to avoid a warning from some compiler that checks the
21786 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021787 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021788 name = v->di_key;
21789 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021790 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021791 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021792 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021793 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021794 v->di_tv.vval.v_list = &fc->l_varlist;
21795 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21796 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21797 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021798
21799 /*
21800 * Set a:firstline to "firstline" and a:lastline to "lastline".
21801 * Set a:name to named arguments.
21802 * Set a:N to the "..." arguments.
21803 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021804 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021805 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021806 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 (varnumber_T)lastline);
21808 for (i = 0; i < argcount; ++i)
21809 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021810 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021811 if (ai < 0)
21812 /* named argument a:name */
21813 name = FUNCARG(fp, i);
21814 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021815 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021816 /* "..." argument a:1, a:2, etc. */
21817 sprintf((char *)numbuf, "%d", ai + 1);
21818 name = numbuf;
21819 }
21820 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21821 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021822 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021823 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21824 }
21825 else
21826 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021827 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21828 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021829 if (v == NULL)
21830 break;
21831 v->di_flags = DI_FLAGS_RO;
21832 }
21833 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021834 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021835
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021836 /* Note: the values are copied directly to avoid alloc/free.
21837 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021838 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021839 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021840
21841 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21842 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021843 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21844 fc->l_listitems[ai].li_tv = argvars[i];
21845 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021846 }
21847 }
21848
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849 /* Don't redraw while executing the function. */
21850 ++RedrawingDisabled;
21851 save_sourcing_name = sourcing_name;
21852 save_sourcing_lnum = sourcing_lnum;
21853 sourcing_lnum = 1;
21854 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021855 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 if (sourcing_name != NULL)
21857 {
21858 if (save_sourcing_name != NULL
21859 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21860 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21861 else
21862 STRCPY(sourcing_name, "function ");
21863 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21864
21865 if (p_verbose >= 12)
21866 {
21867 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021868 verbose_enter_scroll();
21869
Bram Moolenaar555b2802005-05-19 21:08:39 +000021870 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 if (p_verbose >= 14)
21872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021874 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021875 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021876 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877
21878 msg_puts((char_u *)"(");
21879 for (i = 0; i < argcount; ++i)
21880 {
21881 if (i > 0)
21882 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021883 if (argvars[i].v_type == VAR_NUMBER)
21884 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 else
21886 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021887 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21888 if (s != NULL)
21889 {
21890 trunc_string(s, buf, MSG_BUF_CLEN);
21891 msg_puts(buf);
21892 vim_free(tofree);
21893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894 }
21895 }
21896 msg_puts((char_u *)")");
21897 }
21898 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021899
21900 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 --no_wait_return;
21902 }
21903 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021904#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021905 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021906 {
21907 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21908 func_do_profile(fp);
21909 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021910 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021911 {
21912 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021913 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021914 profile_zero(&fp->uf_tm_children);
21915 }
21916 script_prof_save(&wait_start);
21917 }
21918#endif
21919
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021921 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 save_did_emsg = did_emsg;
21923 did_emsg = FALSE;
21924
21925 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021926 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21928
21929 --RedrawingDisabled;
21930
21931 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021932 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021934 clear_tv(rettv);
21935 rettv->v_type = VAR_NUMBER;
21936 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 }
21938
Bram Moolenaar05159a02005-02-26 23:04:13 +000021939#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021940 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021941 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021942 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021943 profile_end(&call_start);
21944 profile_sub_wait(&wait_start, &call_start);
21945 profile_add(&fp->uf_tm_total, &call_start);
21946 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021947 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021948 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021949 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21950 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021951 }
21952 }
21953#endif
21954
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 /* when being verbose, mention the return value */
21956 if (p_verbose >= 12)
21957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021959 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021962 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021963 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021964 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021965 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021966 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021968 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021969 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021970 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021971 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021972
Bram Moolenaar555b2802005-05-19 21:08:39 +000021973 /* The value may be very long. Skip the middle part, so that we
21974 * have some idea how it starts and ends. smsg() would always
21975 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021976 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021977 if (s != NULL)
21978 {
21979 trunc_string(s, buf, MSG_BUF_CLEN);
21980 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21981 vim_free(tofree);
21982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 }
21984 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021985
21986 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 --no_wait_return;
21988 }
21989
21990 vim_free(sourcing_name);
21991 sourcing_name = save_sourcing_name;
21992 sourcing_lnum = save_sourcing_lnum;
21993 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021994#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021995 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021996 script_prof_restore(&wait_start);
21997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998
21999 if (p_verbose >= 12 && sourcing_name != NULL)
22000 {
22001 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022002 verbose_enter_scroll();
22003
Bram Moolenaar555b2802005-05-19 21:08:39 +000022004 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022006
22007 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008 --no_wait_return;
22009 }
22010
22011 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022012 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022014
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022015 /* If the a:000 list and the l: and a: dicts are not referenced we can
22016 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022017 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22018 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22019 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22020 {
22021 free_funccal(fc, FALSE);
22022 }
22023 else
22024 {
22025 hashitem_T *hi;
22026 listitem_T *li;
22027 int todo;
22028
22029 /* "fc" is still in use. This can happen when returning "a:000" or
22030 * assigning "l:" to a global variable.
22031 * Link "fc" in the list for garbage collection later. */
22032 fc->caller = previous_funccal;
22033 previous_funccal = fc;
22034
22035 /* Make a copy of the a: variables, since we didn't do that above. */
22036 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22037 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22038 {
22039 if (!HASHITEM_EMPTY(hi))
22040 {
22041 --todo;
22042 v = HI2DI(hi);
22043 copy_tv(&v->di_tv, &v->di_tv);
22044 }
22045 }
22046
22047 /* Make a copy of the a:000 items, since we didn't do that above. */
22048 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22049 copy_tv(&li->li_tv, &li->li_tv);
22050 }
22051}
22052
22053/*
22054 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022055 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022056 */
22057 static int
22058can_free_funccal(fc, copyID)
22059 funccall_T *fc;
22060 int copyID;
22061{
22062 return (fc->l_varlist.lv_copyID != copyID
22063 && fc->l_vars.dv_copyID != copyID
22064 && fc->l_avars.dv_copyID != copyID);
22065}
22066
22067/*
22068 * Free "fc" and what it contains.
22069 */
22070 static void
22071free_funccal(fc, free_val)
22072 funccall_T *fc;
22073 int free_val; /* a: vars were allocated */
22074{
22075 listitem_T *li;
22076
22077 /* The a: variables typevals may not have been allocated, only free the
22078 * allocated variables. */
22079 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22080
22081 /* free all l: variables */
22082 vars_clear(&fc->l_vars.dv_hashtab);
22083
22084 /* Free the a:000 variables if they were allocated. */
22085 if (free_val)
22086 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22087 clear_tv(&li->li_tv);
22088
22089 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090}
22091
22092/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022093 * Add a number variable "name" to dict "dp" with value "nr".
22094 */
22095 static void
22096add_nr_var(dp, v, name, nr)
22097 dict_T *dp;
22098 dictitem_T *v;
22099 char *name;
22100 varnumber_T nr;
22101{
22102 STRCPY(v->di_key, name);
22103 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22104 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22105 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022106 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022107 v->di_tv.vval.v_number = nr;
22108}
22109
22110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111 * ":return [expr]"
22112 */
22113 void
22114ex_return(eap)
22115 exarg_T *eap;
22116{
22117 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022118 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 int returning = FALSE;
22120
22121 if (current_funccal == NULL)
22122 {
22123 EMSG(_("E133: :return not inside a function"));
22124 return;
22125 }
22126
22127 if (eap->skip)
22128 ++emsg_skip;
22129
22130 eap->nextcmd = NULL;
22131 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022132 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022133 {
22134 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022135 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022137 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138 }
22139 /* It's safer to return also on error. */
22140 else if (!eap->skip)
22141 {
22142 /*
22143 * Return unless the expression evaluation has been cancelled due to an
22144 * aborting error, an interrupt, or an exception.
22145 */
22146 if (!aborting())
22147 returning = do_return(eap, FALSE, TRUE, NULL);
22148 }
22149
22150 /* When skipping or the return gets pending, advance to the next command
22151 * in this line (!returning). Otherwise, ignore the rest of the line.
22152 * Following lines will be ignored by get_func_line(). */
22153 if (returning)
22154 eap->nextcmd = NULL;
22155 else if (eap->nextcmd == NULL) /* no argument */
22156 eap->nextcmd = check_nextcmd(arg);
22157
22158 if (eap->skip)
22159 --emsg_skip;
22160}
22161
22162/*
22163 * Return from a function. Possibly makes the return pending. Also called
22164 * for a pending return at the ":endtry" or after returning from an extra
22165 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022166 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022167 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 * FALSE when the return gets pending.
22169 */
22170 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022171do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 exarg_T *eap;
22173 int reanimate;
22174 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022175 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176{
22177 int idx;
22178 struct condstack *cstack = eap->cstack;
22179
22180 if (reanimate)
22181 /* Undo the return. */
22182 current_funccal->returned = FALSE;
22183
22184 /*
22185 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22186 * not in its finally clause (which then is to be executed next) is found.
22187 * In this case, make the ":return" pending for execution at the ":endtry".
22188 * Otherwise, return normally.
22189 */
22190 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22191 if (idx >= 0)
22192 {
22193 cstack->cs_pending[idx] = CSTP_RETURN;
22194
22195 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022196 /* A pending return again gets pending. "rettv" points to an
22197 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022199 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 else
22201 {
22202 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022203 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022205 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022207 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208 {
22209 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022210 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022211 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212 else
22213 EMSG(_(e_outofmem));
22214 }
22215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022216 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217
22218 if (reanimate)
22219 {
22220 /* The pending return value could be overwritten by a ":return"
22221 * without argument in a finally clause; reset the default
22222 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022223 current_funccal->rettv->v_type = VAR_NUMBER;
22224 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225 }
22226 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022227 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 }
22229 else
22230 {
22231 current_funccal->returned = TRUE;
22232
22233 /* If the return is carried out now, store the return value. For
22234 * a return immediately after reanimation, the value is already
22235 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022236 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022238 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022239 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022241 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 }
22243 }
22244
22245 return idx < 0;
22246}
22247
22248/*
22249 * Free the variable with a pending return value.
22250 */
22251 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022252discard_pending_return(rettv)
22253 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022254{
Bram Moolenaar33570922005-01-25 22:26:29 +000022255 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256}
22257
22258/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022259 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 * is an allocated string. Used by report_pending() for verbose messages.
22261 */
22262 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022263get_return_cmd(rettv)
22264 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022266 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022267 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022268 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022270 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022271 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022272 if (s == NULL)
22273 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022274
22275 STRCPY(IObuff, ":return ");
22276 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22277 if (STRLEN(s) + 8 >= IOSIZE)
22278 STRCPY(IObuff + IOSIZE - 4, "...");
22279 vim_free(tofree);
22280 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281}
22282
22283/*
22284 * Get next function line.
22285 * Called by do_cmdline() to get the next line.
22286 * Returns allocated string, or NULL for end of function.
22287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288 char_u *
22289get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022290 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022292 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293{
Bram Moolenaar33570922005-01-25 22:26:29 +000022294 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022295 ufunc_T *fp = fcp->func;
22296 char_u *retval;
22297 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298
22299 /* If breakpoints have been added/deleted need to check for it. */
22300 if (fcp->dbg_tick != debug_tick)
22301 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022302 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303 sourcing_lnum);
22304 fcp->dbg_tick = debug_tick;
22305 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022306#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022307 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022308 func_line_end(cookie);
22309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310
Bram Moolenaar05159a02005-02-26 23:04:13 +000022311 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022312 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22313 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 retval = NULL;
22315 else
22316 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022317 /* Skip NULL lines (continuation lines). */
22318 while (fcp->linenr < gap->ga_len
22319 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22320 ++fcp->linenr;
22321 if (fcp->linenr >= gap->ga_len)
22322 retval = NULL;
22323 else
22324 {
22325 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22326 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022327#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022328 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022329 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022330#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332 }
22333
22334 /* Did we encounter a breakpoint? */
22335 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22336 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022337 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022339 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 sourcing_lnum);
22341 fcp->dbg_tick = debug_tick;
22342 }
22343
22344 return retval;
22345}
22346
Bram Moolenaar05159a02005-02-26 23:04:13 +000022347#if defined(FEAT_PROFILE) || defined(PROTO)
22348/*
22349 * Called when starting to read a function line.
22350 * "sourcing_lnum" must be correct!
22351 * When skipping lines it may not actually be executed, but we won't find out
22352 * until later and we need to store the time now.
22353 */
22354 void
22355func_line_start(cookie)
22356 void *cookie;
22357{
22358 funccall_T *fcp = (funccall_T *)cookie;
22359 ufunc_T *fp = fcp->func;
22360
22361 if (fp->uf_profiling && sourcing_lnum >= 1
22362 && sourcing_lnum <= fp->uf_lines.ga_len)
22363 {
22364 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022365 /* Skip continuation lines. */
22366 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22367 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022368 fp->uf_tml_execed = FALSE;
22369 profile_start(&fp->uf_tml_start);
22370 profile_zero(&fp->uf_tml_children);
22371 profile_get_wait(&fp->uf_tml_wait);
22372 }
22373}
22374
22375/*
22376 * Called when actually executing a function line.
22377 */
22378 void
22379func_line_exec(cookie)
22380 void *cookie;
22381{
22382 funccall_T *fcp = (funccall_T *)cookie;
22383 ufunc_T *fp = fcp->func;
22384
22385 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22386 fp->uf_tml_execed = TRUE;
22387}
22388
22389/*
22390 * Called when done with a function line.
22391 */
22392 void
22393func_line_end(cookie)
22394 void *cookie;
22395{
22396 funccall_T *fcp = (funccall_T *)cookie;
22397 ufunc_T *fp = fcp->func;
22398
22399 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22400 {
22401 if (fp->uf_tml_execed)
22402 {
22403 ++fp->uf_tml_count[fp->uf_tml_idx];
22404 profile_end(&fp->uf_tml_start);
22405 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022406 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022407 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22408 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022409 }
22410 fp->uf_tml_idx = -1;
22411 }
22412}
22413#endif
22414
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415/*
22416 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022417 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 */
22419 int
22420func_has_ended(cookie)
22421 void *cookie;
22422{
Bram Moolenaar33570922005-01-25 22:26:29 +000022423 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424
22425 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22426 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022427 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 || fcp->returned);
22429}
22430
22431/*
22432 * return TRUE if cookie indicates a function which "abort"s on errors.
22433 */
22434 int
22435func_has_abort(cookie)
22436 void *cookie;
22437{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022438 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439}
22440
22441#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22442typedef enum
22443{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022444 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22445 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22446 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447} var_flavour_T;
22448
22449static var_flavour_T var_flavour __ARGS((char_u *varname));
22450
22451 static var_flavour_T
22452var_flavour(varname)
22453 char_u *varname;
22454{
22455 char_u *p = varname;
22456
22457 if (ASCII_ISUPPER(*p))
22458 {
22459 while (*(++p))
22460 if (ASCII_ISLOWER(*p))
22461 return VAR_FLAVOUR_SESSION;
22462 return VAR_FLAVOUR_VIMINFO;
22463 }
22464 else
22465 return VAR_FLAVOUR_DEFAULT;
22466}
22467#endif
22468
22469#if defined(FEAT_VIMINFO) || defined(PROTO)
22470/*
22471 * Restore global vars that start with a capital from the viminfo file
22472 */
22473 int
22474read_viminfo_varlist(virp, writing)
22475 vir_T *virp;
22476 int writing;
22477{
22478 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022479 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022480 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481
22482 if (!writing && (find_viminfo_parameter('!') != NULL))
22483 {
22484 tab = vim_strchr(virp->vir_line + 1, '\t');
22485 if (tab != NULL)
22486 {
22487 *tab++ = '\0'; /* isolate the variable name */
22488 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022489 type = VAR_STRING;
22490#ifdef FEAT_FLOAT
22491 else if (*tab == 'F')
22492 type = VAR_FLOAT;
22493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494
22495 tab = vim_strchr(tab, '\t');
22496 if (tab != NULL)
22497 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022498 tv.v_type = type;
22499 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022500 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022502#ifdef FEAT_FLOAT
22503 else if (type == VAR_FLOAT)
22504 (void)string2float(tab + 1, &tv.vval.v_float);
22505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022507 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022508 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022509 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022510 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022511 }
22512 }
22513 }
22514
22515 return viminfo_readline(virp);
22516}
22517
22518/*
22519 * Write global vars that start with a capital to the viminfo file
22520 */
22521 void
22522write_viminfo_varlist(fp)
22523 FILE *fp;
22524{
Bram Moolenaar33570922005-01-25 22:26:29 +000022525 hashitem_T *hi;
22526 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022527 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022528 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022529 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022530 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022531 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022532
22533 if (find_viminfo_parameter('!') == NULL)
22534 return;
22535
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022536 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022537
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022538 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022539 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022541 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022543 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022544 this_var = HI2DI(hi);
22545 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022546 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022547 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022548 {
22549 case VAR_STRING: s = "STR"; break;
22550 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022551#ifdef FEAT_FLOAT
22552 case VAR_FLOAT: s = "FLO"; break;
22553#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022554 default: continue;
22555 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022556 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022557 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022558 if (p != NULL)
22559 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022560 vim_free(tofree);
22561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 }
22563 }
22564}
22565#endif
22566
22567#if defined(FEAT_SESSION) || defined(PROTO)
22568 int
22569store_session_globals(fd)
22570 FILE *fd;
22571{
Bram Moolenaar33570922005-01-25 22:26:29 +000022572 hashitem_T *hi;
22573 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022574 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575 char_u *p, *t;
22576
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022577 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022580 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022582 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022583 this_var = HI2DI(hi);
22584 if ((this_var->di_tv.v_type == VAR_NUMBER
22585 || this_var->di_tv.v_type == VAR_STRING)
22586 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022587 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022588 /* Escape special characters with a backslash. Turn a LF and
22589 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022590 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022591 (char_u *)"\\\"\n\r");
22592 if (p == NULL) /* out of memory */
22593 break;
22594 for (t = p; *t != NUL; ++t)
22595 if (*t == '\n')
22596 *t = 'n';
22597 else if (*t == '\r')
22598 *t = 'r';
22599 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022600 this_var->di_key,
22601 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22602 : ' ',
22603 p,
22604 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22605 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022606 || put_eol(fd) == FAIL)
22607 {
22608 vim_free(p);
22609 return FAIL;
22610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611 vim_free(p);
22612 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022613#ifdef FEAT_FLOAT
22614 else if (this_var->di_tv.v_type == VAR_FLOAT
22615 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22616 {
22617 float_T f = this_var->di_tv.vval.v_float;
22618 int sign = ' ';
22619
22620 if (f < 0)
22621 {
22622 f = -f;
22623 sign = '-';
22624 }
22625 if ((fprintf(fd, "let %s = %c&%f",
22626 this_var->di_key, sign, f) < 0)
22627 || put_eol(fd) == FAIL)
22628 return FAIL;
22629 }
22630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 }
22632 }
22633 return OK;
22634}
22635#endif
22636
Bram Moolenaar661b1822005-07-28 22:36:45 +000022637/*
22638 * Display script name where an item was last set.
22639 * Should only be invoked when 'verbose' is non-zero.
22640 */
22641 void
22642last_set_msg(scriptID)
22643 scid_T scriptID;
22644{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022645 char_u *p;
22646
Bram Moolenaar661b1822005-07-28 22:36:45 +000022647 if (scriptID != 0)
22648 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022649 p = home_replace_save(NULL, get_scriptname(scriptID));
22650 if (p != NULL)
22651 {
22652 verbose_enter();
22653 MSG_PUTS(_("\n\tLast set from "));
22654 MSG_PUTS(p);
22655 vim_free(p);
22656 verbose_leave();
22657 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022658 }
22659}
22660
Bram Moolenaard812df62008-11-09 12:46:09 +000022661/*
22662 * List v:oldfiles in a nice way.
22663 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022664 void
22665ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022666 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022667{
22668 list_T *l = vimvars[VV_OLDFILES].vv_list;
22669 listitem_T *li;
22670 int nr = 0;
22671
22672 if (l == NULL)
22673 msg((char_u *)_("No old files"));
22674 else
22675 {
22676 msg_start();
22677 msg_scroll = TRUE;
22678 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22679 {
22680 msg_outnum((long)++nr);
22681 MSG_PUTS(": ");
22682 msg_outtrans(get_tv_string(&li->li_tv));
22683 msg_putchar('\n');
22684 out_flush(); /* output one line at a time */
22685 ui_breakcheck();
22686 }
22687 /* Assume "got_int" was set to truncate the listing. */
22688 got_int = FALSE;
22689
22690#ifdef FEAT_BROWSE_CMD
22691 if (cmdmod.browse)
22692 {
22693 quit_more = FALSE;
22694 nr = prompt_for_number(FALSE);
22695 msg_starthere();
22696 if (nr > 0)
22697 {
22698 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22699 (long)nr);
22700
22701 if (p != NULL)
22702 {
22703 p = expand_env_save(p);
22704 eap->arg = p;
22705 eap->cmdidx = CMD_edit;
22706 cmdmod.browse = FALSE;
22707 do_exedit(eap, NULL);
22708 vim_free(p);
22709 }
22710 }
22711 }
22712#endif
22713 }
22714}
22715
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716#endif /* FEAT_EVAL */
22717
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022719#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720
22721#ifdef WIN3264
22722/*
22723 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22724 */
22725static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22726static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22727static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22728
22729/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022730 * Get the short path (8.3) for the filename in "fnamep".
22731 * Only works for a valid file name.
22732 * When the path gets longer "fnamep" is changed and the allocated buffer
22733 * is put in "bufp".
22734 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22735 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 */
22737 static int
22738get_short_pathname(fnamep, bufp, fnamelen)
22739 char_u **fnamep;
22740 char_u **bufp;
22741 int *fnamelen;
22742{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022743 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 char_u *newbuf;
22745
22746 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 l = GetShortPathName(*fnamep, *fnamep, len);
22748 if (l > len - 1)
22749 {
22750 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022751 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 newbuf = vim_strnsave(*fnamep, l);
22753 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022754 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755
22756 vim_free(*bufp);
22757 *fnamep = *bufp = newbuf;
22758
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022759 /* Really should always succeed, as the buffer is big enough. */
22760 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 }
22762
22763 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022764 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765}
22766
22767/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022768 * Get the short path (8.3) for the filename in "fname". The converted
22769 * path is returned in "bufp".
22770 *
22771 * Some of the directories specified in "fname" may not exist. This function
22772 * will shorten the existing directories at the beginning of the path and then
22773 * append the remaining non-existing path.
22774 *
22775 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022776 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022777 * bufp - Pointer to an allocated buffer for the filename.
22778 * fnamelen - Length of the filename pointed to by fname
22779 *
22780 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781 */
22782 static int
22783shortpath_for_invalid_fname(fname, bufp, fnamelen)
22784 char_u **fname;
22785 char_u **bufp;
22786 int *fnamelen;
22787{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022788 char_u *short_fname, *save_fname, *pbuf_unused;
22789 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022791 int old_len, len;
22792 int new_len, sfx_len;
22793 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794
22795 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022796 old_len = *fnamelen;
22797 save_fname = vim_strnsave(*fname, old_len);
22798 pbuf_unused = NULL;
22799 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022801 endp = save_fname + old_len - 1; /* Find the end of the copy */
22802 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022804 /*
22805 * Try shortening the supplied path till it succeeds by removing one
22806 * directory at a time from the tail of the path.
22807 */
22808 len = 0;
22809 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022811 /* go back one path-separator */
22812 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22813 --endp;
22814 if (endp <= save_fname)
22815 break; /* processed the complete path */
22816
22817 /*
22818 * Replace the path separator with a NUL and try to shorten the
22819 * resulting path.
22820 */
22821 ch = *endp;
22822 *endp = 0;
22823 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022824 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022825 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22826 {
22827 retval = FAIL;
22828 goto theend;
22829 }
22830 *endp = ch; /* preserve the string */
22831
22832 if (len > 0)
22833 break; /* successfully shortened the path */
22834
22835 /* failed to shorten the path. Skip the path separator */
22836 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 }
22838
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022839 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022841 /*
22842 * Succeeded in shortening the path. Now concatenate the shortened
22843 * path with the remaining path at the tail.
22844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022846 /* Compute the length of the new path. */
22847 sfx_len = (int)(save_endp - endp) + 1;
22848 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022850 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022852 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022854 /* There is not enough space in the currently allocated string,
22855 * copy it to a buffer big enough. */
22856 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022858 {
22859 retval = FAIL;
22860 goto theend;
22861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 }
22863 else
22864 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022865 /* Transfer short_fname to the main buffer (it's big enough),
22866 * unless get_short_pathname() did its work in-place. */
22867 *fname = *bufp = save_fname;
22868 if (short_fname != save_fname)
22869 vim_strncpy(save_fname, short_fname, len);
22870 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022872
22873 /* concat the not-shortened part of the path */
22874 vim_strncpy(*fname + len, endp, sfx_len);
22875 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022877
22878theend:
22879 vim_free(pbuf_unused);
22880 vim_free(save_fname);
22881
22882 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883}
22884
22885/*
22886 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022887 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 */
22889 static int
22890shortpath_for_partial(fnamep, bufp, fnamelen)
22891 char_u **fnamep;
22892 char_u **bufp;
22893 int *fnamelen;
22894{
22895 int sepcount, len, tflen;
22896 char_u *p;
22897 char_u *pbuf, *tfname;
22898 int hasTilde;
22899
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022900 /* Count up the path separators from the RHS.. so we know which part
22901 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022903 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 if (vim_ispathsep(*p))
22905 ++sepcount;
22906
22907 /* Need full path first (use expand_env() to remove a "~/") */
22908 hasTilde = (**fnamep == '~');
22909 if (hasTilde)
22910 pbuf = tfname = expand_env_save(*fnamep);
22911 else
22912 pbuf = tfname = FullName_save(*fnamep, FALSE);
22913
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022914 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022916 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22917 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918
22919 if (len == 0)
22920 {
22921 /* Don't have a valid filename, so shorten the rest of the
22922 * path if we can. This CAN give us invalid 8.3 filenames, but
22923 * there's not a lot of point in guessing what it might be.
22924 */
22925 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022926 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22927 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 }
22929
22930 /* Count the paths backward to find the beginning of the desired string. */
22931 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022932 {
22933#ifdef FEAT_MBYTE
22934 if (has_mbyte)
22935 p -= mb_head_off(tfname, p);
22936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 if (vim_ispathsep(*p))
22938 {
22939 if (sepcount == 0 || (hasTilde && sepcount == 1))
22940 break;
22941 else
22942 sepcount --;
22943 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945 if (hasTilde)
22946 {
22947 --p;
22948 if (p >= tfname)
22949 *p = '~';
22950 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022951 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 }
22953 else
22954 ++p;
22955
22956 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22957 vim_free(*bufp);
22958 *fnamelen = (int)STRLEN(p);
22959 *bufp = pbuf;
22960 *fnamep = p;
22961
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022962 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963}
22964#endif /* WIN3264 */
22965
22966/*
22967 * Adjust a filename, according to a string of modifiers.
22968 * *fnamep must be NUL terminated when called. When returning, the length is
22969 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022970 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 * When there is an error, *fnamep is set to NULL.
22972 */
22973 int
22974modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22975 char_u *src; /* string with modifiers */
22976 int *usedlen; /* characters after src that are used */
22977 char_u **fnamep; /* file name so far */
22978 char_u **bufp; /* buffer for allocated file name or NULL */
22979 int *fnamelen; /* length of fnamep */
22980{
22981 int valid = 0;
22982 char_u *tail;
22983 char_u *s, *p, *pbuf;
22984 char_u dirname[MAXPATHL];
22985 int c;
22986 int has_fullname = 0;
22987#ifdef WIN3264
22988 int has_shortname = 0;
22989#endif
22990
22991repeat:
22992 /* ":p" - full path/file_name */
22993 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22994 {
22995 has_fullname = 1;
22996
22997 valid |= VALID_PATH;
22998 *usedlen += 2;
22999
23000 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23001 if ((*fnamep)[0] == '~'
23002#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23003 && ((*fnamep)[1] == '/'
23004# ifdef BACKSLASH_IN_FILENAME
23005 || (*fnamep)[1] == '\\'
23006# endif
23007 || (*fnamep)[1] == NUL)
23008
23009#endif
23010 )
23011 {
23012 *fnamep = expand_env_save(*fnamep);
23013 vim_free(*bufp); /* free any allocated file name */
23014 *bufp = *fnamep;
23015 if (*fnamep == NULL)
23016 return -1;
23017 }
23018
23019 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023020 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 {
23022 if (vim_ispathsep(*p)
23023 && p[1] == '.'
23024 && (p[2] == NUL
23025 || vim_ispathsep(p[2])
23026 || (p[2] == '.'
23027 && (p[3] == NUL || vim_ispathsep(p[3])))))
23028 break;
23029 }
23030
23031 /* FullName_save() is slow, don't use it when not needed. */
23032 if (*p != NUL || !vim_isAbsName(*fnamep))
23033 {
23034 *fnamep = FullName_save(*fnamep, *p != NUL);
23035 vim_free(*bufp); /* free any allocated file name */
23036 *bufp = *fnamep;
23037 if (*fnamep == NULL)
23038 return -1;
23039 }
23040
23041 /* Append a path separator to a directory. */
23042 if (mch_isdir(*fnamep))
23043 {
23044 /* Make room for one or two extra characters. */
23045 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23046 vim_free(*bufp); /* free any allocated file name */
23047 *bufp = *fnamep;
23048 if (*fnamep == NULL)
23049 return -1;
23050 add_pathsep(*fnamep);
23051 }
23052 }
23053
23054 /* ":." - path relative to the current directory */
23055 /* ":~" - path relative to the home directory */
23056 /* ":8" - shortname path - postponed till after */
23057 while (src[*usedlen] == ':'
23058 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23059 {
23060 *usedlen += 2;
23061 if (c == '8')
23062 {
23063#ifdef WIN3264
23064 has_shortname = 1; /* Postpone this. */
23065#endif
23066 continue;
23067 }
23068 pbuf = NULL;
23069 /* Need full path first (use expand_env() to remove a "~/") */
23070 if (!has_fullname)
23071 {
23072 if (c == '.' && **fnamep == '~')
23073 p = pbuf = expand_env_save(*fnamep);
23074 else
23075 p = pbuf = FullName_save(*fnamep, FALSE);
23076 }
23077 else
23078 p = *fnamep;
23079
23080 has_fullname = 0;
23081
23082 if (p != NULL)
23083 {
23084 if (c == '.')
23085 {
23086 mch_dirname(dirname, MAXPATHL);
23087 s = shorten_fname(p, dirname);
23088 if (s != NULL)
23089 {
23090 *fnamep = s;
23091 if (pbuf != NULL)
23092 {
23093 vim_free(*bufp); /* free any allocated file name */
23094 *bufp = pbuf;
23095 pbuf = NULL;
23096 }
23097 }
23098 }
23099 else
23100 {
23101 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23102 /* Only replace it when it starts with '~' */
23103 if (*dirname == '~')
23104 {
23105 s = vim_strsave(dirname);
23106 if (s != NULL)
23107 {
23108 *fnamep = s;
23109 vim_free(*bufp);
23110 *bufp = s;
23111 }
23112 }
23113 }
23114 vim_free(pbuf);
23115 }
23116 }
23117
23118 tail = gettail(*fnamep);
23119 *fnamelen = (int)STRLEN(*fnamep);
23120
23121 /* ":h" - head, remove "/file_name", can be repeated */
23122 /* Don't remove the first "/" or "c:\" */
23123 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23124 {
23125 valid |= VALID_HEAD;
23126 *usedlen += 2;
23127 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023128 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023129 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023130 *fnamelen = (int)(tail - *fnamep);
23131#ifdef VMS
23132 if (*fnamelen > 0)
23133 *fnamelen += 1; /* the path separator is part of the path */
23134#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023135 if (*fnamelen == 0)
23136 {
23137 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23138 p = vim_strsave((char_u *)".");
23139 if (p == NULL)
23140 return -1;
23141 vim_free(*bufp);
23142 *bufp = *fnamep = tail = p;
23143 *fnamelen = 1;
23144 }
23145 else
23146 {
23147 while (tail > s && !after_pathsep(s, tail))
23148 mb_ptr_back(*fnamep, tail);
23149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 }
23151
23152 /* ":8" - shortname */
23153 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23154 {
23155 *usedlen += 2;
23156#ifdef WIN3264
23157 has_shortname = 1;
23158#endif
23159 }
23160
23161#ifdef WIN3264
23162 /* Check shortname after we have done 'heads' and before we do 'tails'
23163 */
23164 if (has_shortname)
23165 {
23166 pbuf = NULL;
23167 /* Copy the string if it is shortened by :h */
23168 if (*fnamelen < (int)STRLEN(*fnamep))
23169 {
23170 p = vim_strnsave(*fnamep, *fnamelen);
23171 if (p == 0)
23172 return -1;
23173 vim_free(*bufp);
23174 *bufp = *fnamep = p;
23175 }
23176
23177 /* Split into two implementations - makes it easier. First is where
23178 * there isn't a full name already, second is where there is.
23179 */
23180 if (!has_fullname && !vim_isAbsName(*fnamep))
23181 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023182 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 return -1;
23184 }
23185 else
23186 {
23187 int l;
23188
23189 /* Simple case, already have the full-name
23190 * Nearly always shorter, so try first time. */
23191 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023192 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 return -1;
23194
23195 if (l == 0)
23196 {
23197 /* Couldn't find the filename.. search the paths.
23198 */
23199 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023200 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 return -1;
23202 }
23203 *fnamelen = l;
23204 }
23205 }
23206#endif /* WIN3264 */
23207
23208 /* ":t" - tail, just the basename */
23209 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23210 {
23211 *usedlen += 2;
23212 *fnamelen -= (int)(tail - *fnamep);
23213 *fnamep = tail;
23214 }
23215
23216 /* ":e" - extension, can be repeated */
23217 /* ":r" - root, without extension, can be repeated */
23218 while (src[*usedlen] == ':'
23219 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23220 {
23221 /* find a '.' in the tail:
23222 * - for second :e: before the current fname
23223 * - otherwise: The last '.'
23224 */
23225 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23226 s = *fnamep - 2;
23227 else
23228 s = *fnamep + *fnamelen - 1;
23229 for ( ; s > tail; --s)
23230 if (s[0] == '.')
23231 break;
23232 if (src[*usedlen + 1] == 'e') /* :e */
23233 {
23234 if (s > tail)
23235 {
23236 *fnamelen += (int)(*fnamep - (s + 1));
23237 *fnamep = s + 1;
23238#ifdef VMS
23239 /* cut version from the extension */
23240 s = *fnamep + *fnamelen - 1;
23241 for ( ; s > *fnamep; --s)
23242 if (s[0] == ';')
23243 break;
23244 if (s > *fnamep)
23245 *fnamelen = s - *fnamep;
23246#endif
23247 }
23248 else if (*fnamep <= tail)
23249 *fnamelen = 0;
23250 }
23251 else /* :r */
23252 {
23253 if (s > tail) /* remove one extension */
23254 *fnamelen = (int)(s - *fnamep);
23255 }
23256 *usedlen += 2;
23257 }
23258
23259 /* ":s?pat?foo?" - substitute */
23260 /* ":gs?pat?foo?" - global substitute */
23261 if (src[*usedlen] == ':'
23262 && (src[*usedlen + 1] == 's'
23263 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23264 {
23265 char_u *str;
23266 char_u *pat;
23267 char_u *sub;
23268 int sep;
23269 char_u *flags;
23270 int didit = FALSE;
23271
23272 flags = (char_u *)"";
23273 s = src + *usedlen + 2;
23274 if (src[*usedlen + 1] == 'g')
23275 {
23276 flags = (char_u *)"g";
23277 ++s;
23278 }
23279
23280 sep = *s++;
23281 if (sep)
23282 {
23283 /* find end of pattern */
23284 p = vim_strchr(s, sep);
23285 if (p != NULL)
23286 {
23287 pat = vim_strnsave(s, (int)(p - s));
23288 if (pat != NULL)
23289 {
23290 s = p + 1;
23291 /* find end of substitution */
23292 p = vim_strchr(s, sep);
23293 if (p != NULL)
23294 {
23295 sub = vim_strnsave(s, (int)(p - s));
23296 str = vim_strnsave(*fnamep, *fnamelen);
23297 if (sub != NULL && str != NULL)
23298 {
23299 *usedlen = (int)(p + 1 - src);
23300 s = do_string_sub(str, pat, sub, flags);
23301 if (s != NULL)
23302 {
23303 *fnamep = s;
23304 *fnamelen = (int)STRLEN(s);
23305 vim_free(*bufp);
23306 *bufp = s;
23307 didit = TRUE;
23308 }
23309 }
23310 vim_free(sub);
23311 vim_free(str);
23312 }
23313 vim_free(pat);
23314 }
23315 }
23316 /* after using ":s", repeat all the modifiers */
23317 if (didit)
23318 goto repeat;
23319 }
23320 }
23321
23322 return valid;
23323}
23324
23325/*
23326 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23327 * "flags" can be "g" to do a global substitute.
23328 * Returns an allocated string, NULL for error.
23329 */
23330 char_u *
23331do_string_sub(str, pat, sub, flags)
23332 char_u *str;
23333 char_u *pat;
23334 char_u *sub;
23335 char_u *flags;
23336{
23337 int sublen;
23338 regmatch_T regmatch;
23339 int i;
23340 int do_all;
23341 char_u *tail;
23342 garray_T ga;
23343 char_u *ret;
23344 char_u *save_cpo;
23345
23346 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23347 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023348 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023349
23350 ga_init2(&ga, 1, 200);
23351
23352 do_all = (flags[0] == 'g');
23353
23354 regmatch.rm_ic = p_ic;
23355 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23356 if (regmatch.regprog != NULL)
23357 {
23358 tail = str;
23359 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23360 {
23361 /*
23362 * Get some space for a temporary buffer to do the substitution
23363 * into. It will contain:
23364 * - The text up to where the match is.
23365 * - The substituted text.
23366 * - The text after the match.
23367 */
23368 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23369 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23370 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23371 {
23372 ga_clear(&ga);
23373 break;
23374 }
23375
23376 /* copy the text up to where the match is */
23377 i = (int)(regmatch.startp[0] - tail);
23378 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23379 /* add the substituted text */
23380 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23381 + ga.ga_len + i, TRUE, TRUE, FALSE);
23382 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 /* avoid getting stuck on a match with an empty string */
23384 if (tail == regmatch.endp[0])
23385 {
23386 if (*tail == NUL)
23387 break;
23388 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23389 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 }
23391 else
23392 {
23393 tail = regmatch.endp[0];
23394 if (*tail == NUL)
23395 break;
23396 }
23397 if (!do_all)
23398 break;
23399 }
23400
23401 if (ga.ga_data != NULL)
23402 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23403
23404 vim_free(regmatch.regprog);
23405 }
23406
23407 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23408 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023409 if (p_cpo == empty_option)
23410 p_cpo = save_cpo;
23411 else
23412 /* Darn, evaluating {sub} expression changed the value. */
23413 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414
23415 return ret;
23416}
23417
23418#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */