blob: 295512e34a624f697ba5898438f937c4b0657210 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarc236c162008-07-13 17:41:49 +000013#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019#if defined(FEAT_EVAL) || defined(PROTO)
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef AMIGA
22# include <time.h> /* for strftime() */
23#endif
24
25#ifdef MACOS
26# include <time.h> /* for time_t */
27#endif
28
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
30# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#endif
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000035#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
36 be freed. */
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038/*
Bram Moolenaar33570922005-01-25 22:26:29 +000039 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
40 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000041 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
42 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
43 * HI2DI() converts a hashitem pointer to a dictitem pointer.
44 */
Bram Moolenaar33570922005-01-25 22:26:29 +000045static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000047#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000048#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000049
50/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000051 * Structure returned by get_lval() and used by set_var_lval().
52 * For a plain name:
53 * "name" points to the variable name.
54 * "exp_name" is NULL.
55 * "tv" is NULL
56 * For a magic braces name:
57 * "name" points to the expanded variable name.
58 * "exp_name" is non-NULL, to be freed later.
59 * "tv" is NULL
60 * For an index in a list:
61 * "name" points to the (expanded) variable name.
62 * "exp_name" NULL or non-NULL, to be freed later.
63 * "tv" points to the (first) list item value
64 * "li" points to the (first) list item
65 * "range", "n1", "n2" and "empty2" indicate what items are used.
66 * For an existing Dict item:
67 * "name" points to the (expanded) variable name.
68 * "exp_name" NULL or non-NULL, to be freed later.
69 * "tv" points to the dict item value
70 * "newkey" is NULL
71 * For a non-existing Dict item:
72 * "name" points to the (expanded) variable name.
73 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000074 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000075 * "newkey" is the key for the new item.
76 */
77typedef struct lval_S
78{
79 char_u *ll_name; /* start of variable name (can be NULL) */
80 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000082 isn't NULL it's the Dict to which to add
83 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000084 listitem_T *ll_li; /* The list item or NULL. */
85 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000086 int ll_range; /* TRUE when a [i:j] range was used */
87 long ll_n1; /* First index for list */
88 long ll_n2; /* Second index for list range */
89 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000090 dict_T *ll_dict; /* The Dictionary or NULL */
91 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000092 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000093} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000094
Bram Moolenaar8c711452005-01-14 21:53:12 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000102static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_listreq = N_("E714: List required");
104static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000105static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000106static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
107static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
108static char *e_funcdict = N_("E717: Dictionary entry already exists");
109static char *e_funcref = N_("E718: Funcref required");
110static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
111static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000112static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000113static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000114
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000115/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000116 * All user-defined global variables are stored in dictionary "globvardict".
117 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000119static dict_T globvardict;
120static dictitem_T globvars_var;
121#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124 * Old Vim variables such as "v:version" are also available without the "v:".
125 * Also in functions. We need a special hashtable for them.
126 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000127static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200129/* When using exists() don't auto-load a script. */
130static int no_autoload = FALSE;
131
Bram Moolenaar532c7802005-01-27 14:44:31 +0000132/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133 * When recursively copying lists and dicts we need to remember which ones we
134 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000135 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136 */
137static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000138#define COPYID_INC 2
139#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140
141/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000142 * Array to hold the hashtab with variables local to each sourced script.
143 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000145typedef struct
146{
147 dictitem_T sv_var;
148 dict_T sv_dict;
149} scriptvar_T;
150
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200151static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
152#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
153#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155static int echo_attr = 0; /* attributes used for ":echo" */
156
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000157/* Values for trans_function_name() argument: */
158#define TFN_INT 1 /* internal function name OK */
159#define TFN_QUIET 2 /* no error messages */
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161/*
162 * Structure to hold info for a user function.
163 */
164typedef struct ufunc ufunc_T;
165
166struct ufunc
167{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000168 int uf_varargs; /* variable nr of arguments */
169 int uf_flags;
170 int uf_calls; /* nr of active calls */
171 garray_T uf_args; /* arguments */
172 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000173#ifdef FEAT_PROFILE
174 int uf_profiling; /* TRUE when func is being profiled */
175 /* profiling the function as a whole */
176 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000177 proftime_T uf_tm_total; /* time spent in function + children */
178 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000179 proftime_T uf_tm_children; /* time spent in children this call */
180 /* profiling the function per line */
181 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000182 proftime_T *uf_tml_total; /* time spent in a line + children */
183 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000184 proftime_T uf_tml_start; /* start time for current line */
185 proftime_T uf_tml_children; /* time spent in children for this line */
186 proftime_T uf_tml_wait; /* start wait time for current line */
187 int uf_tml_idx; /* index of line being timed; -1 if none */
188 int uf_tml_execed; /* line being timed was executed */
189#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000190 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000192 int uf_refcount; /* for numbered function: reference count */
193 char_u uf_name[1]; /* name of function (actually longer); can
194 start with <SNR>123_ (<SNR> is K_SPECIAL
195 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196};
197
198/* function flags */
199#define FC_ABORT 1 /* abort function on error */
200#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000201#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
203/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000204 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000206static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000208/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000209static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
210
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000211/* list heads for garbage collection */
212static dict_T *first_dict = NULL; /* list of all dicts */
213static list_T *first_list = NULL; /* list of all lists */
214
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000215/* From user function to hashitem and back. */
216static ufunc_T dumuf;
217#define UF2HIKEY(fp) ((fp)->uf_name)
218#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
219#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
220
221#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
222#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223
Bram Moolenaar33570922005-01-25 22:26:29 +0000224#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
225#define VAR_SHORT_LEN 20 /* short variable name length */
226#define FIXVAR_CNT 12 /* number of fixed variables */
227
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000229typedef struct funccall_S funccall_T;
230
231struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232{
233 ufunc_T *func; /* function being called */
234 int linenr; /* next line to be executed */
235 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000236 struct /* fixed variables for arguments */
237 {
238 dictitem_T var; /* variable (without room for name) */
239 char_u room[VAR_SHORT_LEN]; /* room for the name */
240 } fixvar[FIXVAR_CNT];
241 dict_T l_vars; /* l: local function variables */
242 dictitem_T l_vars_var; /* variable for l: scope */
243 dict_T l_avars; /* a: argument variables */
244 dictitem_T l_avars_var; /* variable for a: scope */
245 list_T l_varlist; /* list for a:000 */
246 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
247 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 linenr_T breakpoint; /* next line with breakpoint or zero */
249 int dbg_tick; /* debug_tick when breakpoint was set */
250 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000251#ifdef FEAT_PROFILE
252 proftime_T prof_child; /* time spent in a child */
253#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000254 funccall_T *caller; /* calling function or NULL */
255};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256
257/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258 * Info used by a ":for" loop.
259 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000260typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261{
262 int fi_semicolon; /* TRUE if ending in '; var]' */
263 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264 listwatch_T fi_lw; /* keep an eye on the item used. */
265 list_T *fi_list; /* list being used */
266} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000269 * Struct used by trans_function_name()
270 */
271typedef struct
272{
Bram Moolenaar33570922005-01-25 22:26:29 +0000273 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000274 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000275 dictitem_T *fd_di; /* Dictionary item used */
276} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000277
Bram Moolenaara7043832005-01-21 11:56:39 +0000278
279/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000280 * Array to hold the value of v: variables.
281 * The value is in a dictitem, so that it can also be used in the v: scope.
282 * The reason to use this table anyway is for very quick access to the
283 * variables with the VV_ defines.
284 */
285#include "version.h"
286
287/* values for vv_flags: */
288#define VV_COMPAT 1 /* compatible, also used without "v:" */
289#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000290#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000291
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000292#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000293
294static struct vimvar
295{
296 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297 dictitem_T vv_di; /* value and name for key */
298 char vv_filler[16]; /* space for LONGEST name below!!! */
299 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
300} vimvars[VV_LEN] =
301{
302 /*
303 * The order here must match the VV_ defines in vim.h!
304 * Initializing a union does not work, leave tv.vval empty to get zero's.
305 */
306 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
307 {VV_NAME("count1", VAR_NUMBER), VV_RO},
308 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
309 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
310 {VV_NAME("warningmsg", VAR_STRING), 0},
311 {VV_NAME("statusmsg", VAR_STRING), 0},
312 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
313 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
314 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
316 {VV_NAME("termresponse", VAR_STRING), VV_RO},
317 {VV_NAME("fname", VAR_STRING), VV_RO},
318 {VV_NAME("lang", VAR_STRING), VV_RO},
319 {VV_NAME("lc_time", VAR_STRING), VV_RO},
320 {VV_NAME("ctype", VAR_STRING), VV_RO},
321 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
323 {VV_NAME("fname_in", VAR_STRING), VV_RO},
324 {VV_NAME("fname_out", VAR_STRING), VV_RO},
325 {VV_NAME("fname_new", VAR_STRING), VV_RO},
326 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
327 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
328 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
331 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("progname", VAR_STRING), VV_RO},
333 {VV_NAME("servername", VAR_STRING), VV_RO},
334 {VV_NAME("dying", VAR_NUMBER), VV_RO},
335 {VV_NAME("exception", VAR_STRING), VV_RO},
336 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
337 {VV_NAME("register", VAR_STRING), VV_RO},
338 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
339 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000340 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
341 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000342 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000343 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
344 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000345 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000350 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000351 {VV_NAME("swapname", VAR_STRING), VV_RO},
352 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000353 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000354 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000355 {VV_NAME("mouse_win", VAR_NUMBER), 0},
356 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
357 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000358 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000359 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000360 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000361};
362
363/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364#define vv_type vv_di.di_tv.v_type
365#define vv_nr vv_di.di_tv.vval.v_number
366#define vv_float vv_di.di_tv.vval.v_float
367#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000368#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000370
371/*
372 * The v: variables are stored in dictionary "vimvardict".
373 * "vimvars_var" is the variable that is used for the "l:" scope.
374 */
375static dict_T vimvardict;
376static dictitem_T vimvars_var;
377#define vimvarht vimvardict.dv_hashtab
378
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
380static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
381#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
382static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
383#endif
384static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
385static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
386static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000387static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
388static void list_glob_vars __ARGS((int *first));
389static void list_buf_vars __ARGS((int *first));
390static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000392static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000393#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000394static void list_vim_vars __ARGS((int *first));
395static void list_script_vars __ARGS((int *first));
396static void list_func_vars __ARGS((int *first));
397static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000398static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
399static int check_changedtick __ARGS((char_u *arg));
400static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
401static void clear_lval __ARGS((lval_T *lp));
402static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
403static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
404static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
405static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
406static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
407static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
408static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
409static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
410static void item_lock __ARGS((typval_T *tv, int deep, int lock));
411static int tv_islocked __ARGS((typval_T *tv));
412
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
414static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000419static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
420static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000421
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000422static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
424static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
425static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
426static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000427static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static listitem_T *listitem_alloc __ARGS((void));
429static void listitem_free __ARGS((listitem_T *item));
430static void listitem_remove __ARGS((list_T *l, listitem_T *item));
431static long list_len __ARGS((list_T *l));
432static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
433static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
434static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000436static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000439static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
441static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
442static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000445static char_u *list2string __ARGS((typval_T *tv, int copyID));
446static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000447static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000448static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
449static void set_ref_in_list __ARGS((list_T *l, int copyID));
450static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200451static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000453static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
455static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000456static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000458static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000460static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
461static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#ifdef FEAT_FLOAT
464static int string2float __ARGS((char_u *text, float_T *value));
465#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000466static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
467static int find_internal_func __ARGS((char_u *name));
468static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
469static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200470static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000471static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000472static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000473
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#ifdef FEAT_FLOAT
475static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200476static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000478static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200484static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200486static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000502static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000505static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000506static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000508static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000509static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
515static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200516static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
521static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200532#ifdef FEAT_FLOAT
533static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
534#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000537static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
544static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000548static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000557static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000559static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000565static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000573static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000574static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000575static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000576static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200579static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000580static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000588static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000602static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000608static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000620#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200621static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000622static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000629static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000630static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000632static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
634static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000636#ifdef vim_mkdir
637static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100640#ifdef FEAT_MZSCHEME
641static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
644static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000645static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000646#ifdef FEAT_FLOAT
647static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
648#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000650static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000651static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000653static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000654static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000666#ifdef FEAT_FLOAT
667static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
668#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000670static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000672static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000679static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000680static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000681static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000682static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200684static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000685static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000687static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
690static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200691static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000694static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000695static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#ifdef FEAT_FLOAT
699static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
701#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000702static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703#ifdef HAVE_STRFTIME
704static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
706static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000717static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000718static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000719static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000720static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000721static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000722static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000723static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000725static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200726#ifdef FEAT_FLOAT
727static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
729#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#ifdef FEAT_FLOAT
734static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200737static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200738static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000748static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000750static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000751static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000753static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000754static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static int get_env_len __ARGS((char_u **arg));
756static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000758static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
759#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
760#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
761 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000762static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000764static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000765static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
766static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static typval_T *alloc_tv __ARGS((void));
768static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void init_tv __ARGS((typval_T *varp));
770static long get_tv_number __ARGS((typval_T *varp));
771static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000772static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static char_u *get_tv_string __ARGS((typval_T *varp));
774static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000775static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000777static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
779static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
780static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000781static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
782static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
784static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000785static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000786static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000787static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
789static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
790static int eval_fname_script __ARGS((char_u *p));
791static int eval_fname_sid __ARGS((char_u *p));
792static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000793static ufunc_T *find_func __ARGS((char_u *name));
794static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000795static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796#ifdef FEAT_PROFILE
797static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000798static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
799static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
800static int
801# ifdef __BORLANDC__
802 _RTLENTRYF
803# endif
804 prof_total_cmp __ARGS((const void *s1, const void *s2));
805static int
806# ifdef __BORLANDC__
807 _RTLENTRYF
808# endif
809 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000811static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000812static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000813static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000814static void func_free __ARGS((ufunc_T *fp));
815static void func_unref __ARGS((char_u *name));
816static void func_ref __ARGS((char_u *name));
817static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000818static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
819static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000820static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000821static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
822static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000823static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000824static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000825static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200827
828#ifdef EBCDIC
829static int compare_func_name __ARGS((const void *s1, const void *s2));
830static void sortFunctions __ARGS(());
831#endif
832
833
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000834/* Character used as separated in autoload function/variable names. */
835#define AUTOLOAD_CHAR '#'
836
Bram Moolenaar33570922005-01-25 22:26:29 +0000837/*
838 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000839 */
840 void
841eval_init()
842{
Bram Moolenaar33570922005-01-25 22:26:29 +0000843 int i;
844 struct vimvar *p;
845
846 init_var_dict(&globvardict, &globvars_var);
847 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000848 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000849 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000850
851 for (i = 0; i < VV_LEN; ++i)
852 {
853 p = &vimvars[i];
854 STRCPY(p->vv_di.di_key, p->vv_name);
855 if (p->vv_flags & VV_RO)
856 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
857 else if (p->vv_flags & VV_RO_SBX)
858 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
859 else
860 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000861
862 /* add to v: scope dict, unless the value is not always available */
863 if (p->vv_type != VAR_UNKNOWN)
864 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000865 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000866 /* add to compat scope dict */
867 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000868 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000869 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200870
871#ifdef EBCDIC
872 /*
873 * Sort the function table, to enable binary sort.
874 */
875 sortFunctions();
876#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000877}
878
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000879#if defined(EXITFREE) || defined(PROTO)
880 void
881eval_clear()
882{
883 int i;
884 struct vimvar *p;
885
886 for (i = 0; i < VV_LEN; ++i)
887 {
888 p = &vimvars[i];
889 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000890 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000891 vim_free(p->vv_str);
892 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000893 }
894 else if (p->vv_di.di_tv.v_type == VAR_LIST)
895 {
896 list_unref(p->vv_list);
897 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000898 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000899 }
900 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000901 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000902 hash_clear(&compat_hashtab);
903
Bram Moolenaard9fba312005-06-26 22:34:35 +0000904 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905
906 /* global variables */
907 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000908
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000909 /* autoloaded script names */
910 ga_clear_strings(&ga_loaded);
911
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200912 /* script-local variables */
913 for (i = 1; i <= ga_scripts.ga_len; ++i)
914 {
915 vars_clear(&SCRIPT_VARS(i));
916 vim_free(SCRIPT_SV(i));
917 }
918 ga_clear(&ga_scripts);
919
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000920 /* unreferenced lists and dicts */
921 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000922
923 /* functions */
924 free_all_functions();
925 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000926}
927#endif
928
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 * Return the name of the executed function.
931 */
932 char_u *
933func_name(cookie)
934 void *cookie;
935{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000936 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937}
938
939/*
940 * Return the address holding the next breakpoint line for a funccall cookie.
941 */
942 linenr_T *
943func_breakpoint(cookie)
944 void *cookie;
945{
Bram Moolenaar33570922005-01-25 22:26:29 +0000946 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947}
948
949/*
950 * Return the address holding the debug tick for a funccall cookie.
951 */
952 int *
953func_dbg_tick(cookie)
954 void *cookie;
955{
Bram Moolenaar33570922005-01-25 22:26:29 +0000956 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959/*
960 * Return the nesting level for a funccall cookie.
961 */
962 int
963func_level(cookie)
964 void *cookie;
965{
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
969/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000970funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000972/* pointer to list of previously used funccal, still around because some
973 * item in it is still being used. */
974funccall_T *previous_funccal = NULL;
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976/*
977 * Return TRUE when a function was ended by a ":return" command.
978 */
979 int
980current_func_returned()
981{
982 return current_funccal->returned;
983}
984
985
986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 * Set an internal variable to a string value. Creates the variable if it does
988 * not already exist.
989 */
990 void
991set_internal_string_var(name, value)
992 char_u *name;
993 char_u *value;
994{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000995 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000996 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
998 val = vim_strsave(value);
999 if (val != NULL)
1000 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001001 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001002 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001004 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001005 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007 }
1008}
1009
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001010static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001011static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001012static char_u *redir_endp = NULL;
1013static char_u *redir_varname = NULL;
1014
1015/*
1016 * Start recording command output to a variable
1017 * Returns OK if successfully completed the setup. FAIL otherwise.
1018 */
1019 int
1020var_redir_start(name, append)
1021 char_u *name;
1022 int append; /* append to an existing variable */
1023{
1024 int save_emsg;
1025 int err;
1026 typval_T tv;
1027
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001028 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001029 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001030 {
1031 EMSG(_(e_invarg));
1032 return FAIL;
1033 }
1034
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001035 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 redir_varname = vim_strsave(name);
1037 if (redir_varname == NULL)
1038 return FAIL;
1039
1040 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1041 if (redir_lval == NULL)
1042 {
1043 var_redir_stop();
1044 return FAIL;
1045 }
1046
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001047 /* The output is stored in growarray "redir_ga" until redirection ends. */
1048 ga_init2(&redir_ga, (int)sizeof(char), 500);
1049
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001051 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1052 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1054 {
1055 if (redir_endp != NULL && *redir_endp != NUL)
1056 /* Trailing characters are present after the variable name */
1057 EMSG(_(e_trailing));
1058 else
1059 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001060 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 var_redir_stop();
1062 return FAIL;
1063 }
1064
1065 /* check if we can write to the variable: set it to or append an empty
1066 * string */
1067 save_emsg = did_emsg;
1068 did_emsg = FALSE;
1069 tv.v_type = VAR_STRING;
1070 tv.vval.v_string = (char_u *)"";
1071 if (append)
1072 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1073 else
1074 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
1075 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001076 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001077 if (err)
1078 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001079 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 var_redir_stop();
1081 return FAIL;
1082 }
1083 if (redir_lval->ll_newkey != NULL)
1084 {
1085 /* Dictionary item was created, don't do it again. */
1086 vim_free(redir_lval->ll_newkey);
1087 redir_lval->ll_newkey = NULL;
1088 }
1089
1090 return OK;
1091}
1092
1093/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001094 * Append "value[value_len]" to the variable set by var_redir_start().
1095 * The actual appending is postponed until redirection ends, because the value
1096 * appended may in fact be the string we write to, changing it may cause freed
1097 * memory to be used:
1098 * :redir => foo
1099 * :let foo
1100 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 */
1102 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001107 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108
1109 if (redir_lval == NULL)
1110 return;
1111
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001113 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 {
1119 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 }
1122 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124}
1125
1126/*
1127 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001128 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 */
1130 void
1131var_redir_stop()
1132{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 typval_T tv;
1134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 if (redir_lval != NULL)
1136 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 /* If there was no error: assign the text to the variable. */
1138 if (redir_endp != NULL)
1139 {
1140 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1141 tv.v_type = VAR_STRING;
1142 tv.vval.v_string = redir_ga.ga_data;
1143 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1144 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001145
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 /* free the collected output */
1147 vim_free(redir_ga.ga_data);
1148 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 clear_lval(redir_lval);
1151 vim_free(redir_lval);
1152 redir_lval = NULL;
1153 }
1154 vim_free(redir_varname);
1155 redir_varname = NULL;
1156}
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158# if defined(FEAT_MBYTE) || defined(PROTO)
1159 int
1160eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1161 char_u *enc_from;
1162 char_u *enc_to;
1163 char_u *fname_from;
1164 char_u *fname_to;
1165{
1166 int err = FALSE;
1167
1168 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1169 set_vim_var_string(VV_CC_TO, enc_to, -1);
1170 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1171 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1172 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1173 err = TRUE;
1174 set_vim_var_string(VV_CC_FROM, NULL, -1);
1175 set_vim_var_string(VV_CC_TO, NULL, -1);
1176 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1177 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1178
1179 if (err)
1180 return FAIL;
1181 return OK;
1182}
1183# endif
1184
1185# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1186 int
1187eval_printexpr(fname, args)
1188 char_u *fname;
1189 char_u *args;
1190{
1191 int err = FALSE;
1192
1193 set_vim_var_string(VV_FNAME_IN, fname, -1);
1194 set_vim_var_string(VV_CMDARG, args, -1);
1195 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1196 err = TRUE;
1197 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1198 set_vim_var_string(VV_CMDARG, NULL, -1);
1199
1200 if (err)
1201 {
1202 mch_remove(fname);
1203 return FAIL;
1204 }
1205 return OK;
1206}
1207# endif
1208
1209# if defined(FEAT_DIFF) || defined(PROTO)
1210 void
1211eval_diff(origfile, newfile, outfile)
1212 char_u *origfile;
1213 char_u *newfile;
1214 char_u *outfile;
1215{
1216 int err = FALSE;
1217
1218 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1219 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1220 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1221 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1222 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1223 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1224 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1225}
1226
1227 void
1228eval_patch(origfile, difffile, outfile)
1229 char_u *origfile;
1230 char_u *difffile;
1231 char_u *outfile;
1232{
1233 int err;
1234
1235 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1236 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1237 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1238 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1239 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1240 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1241 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1242}
1243# endif
1244
1245/*
1246 * Top level evaluation function, returning a boolean.
1247 * Sets "error" to TRUE if there was an error.
1248 * Return TRUE or FALSE.
1249 */
1250 int
1251eval_to_bool(arg, error, nextcmd, skip)
1252 char_u *arg;
1253 int *error;
1254 char_u **nextcmd;
1255 int skip; /* only parse, don't execute */
1256{
Bram Moolenaar33570922005-01-25 22:26:29 +00001257 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 int retval = FALSE;
1259
1260 if (skip)
1261 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001262 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 else
1265 {
1266 *error = FALSE;
1267 if (!skip)
1268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001269 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272 }
1273 if (skip)
1274 --emsg_skip;
1275
1276 return retval;
1277}
1278
1279/*
1280 * Top level evaluation function, returning a string. If "skip" is TRUE,
1281 * only parsing to "nextcmd" is done, without reporting errors. Return
1282 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1283 */
1284 char_u *
1285eval_to_string_skip(arg, nextcmd, skip)
1286 char_u *arg;
1287 char_u **nextcmd;
1288 int skip; /* only parse, don't execute */
1289{
Bram Moolenaar33570922005-01-25 22:26:29 +00001290 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 char_u *retval;
1292
1293 if (skip)
1294 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001295 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 retval = NULL;
1297 else
1298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001299 retval = vim_strsave(get_tv_string(&tv));
1300 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
1302 if (skip)
1303 --emsg_skip;
1304
1305 return retval;
1306}
1307
1308/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001309 * Skip over an expression at "*pp".
1310 * Return FAIL for an error, OK otherwise.
1311 */
1312 int
1313skip_expr(pp)
1314 char_u **pp;
1315{
Bram Moolenaar33570922005-01-25 22:26:29 +00001316 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001317
1318 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001320}
1321
1322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001324 * When "convert" is TRUE convert a List into a sequence of lines and convert
1325 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 * Return pointer to allocated memory, or NULL for failure.
1327 */
1328 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001329eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 char_u *arg;
1331 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001332 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
Bram Moolenaar33570922005-01-25 22:26:29 +00001334 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001336 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001337#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 retval = NULL;
1343 else
1344 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001346 {
1347 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001348 if (tv.vval.v_list != NULL)
1349 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001350 ga_append(&ga, NUL);
1351 retval = (char_u *)ga.ga_data;
1352 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001353#ifdef FEAT_FLOAT
1354 else if (convert && tv.v_type == VAR_FLOAT)
1355 {
1356 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1357 retval = vim_strsave(numbuf);
1358 }
1359#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 else
1361 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001362 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364
1365 return retval;
1366}
1367
1368/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001369 * Call eval_to_string() without using current local variables and using
1370 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 */
1372 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001373eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 char_u *arg;
1375 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001376 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 char_u *retval;
1379 void *save_funccalp;
1380
1381 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001382 if (use_sandbox)
1383 ++sandbox;
1384 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001385 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001386 if (use_sandbox)
1387 --sandbox;
1388 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 restore_funccal(save_funccalp);
1390 return retval;
1391}
1392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393/*
1394 * Top level evaluation function, returning a number.
1395 * Evaluates "expr" silently.
1396 * Returns -1 for an error.
1397 */
1398 int
1399eval_to_number(expr)
1400 char_u *expr;
1401{
Bram Moolenaar33570922005-01-25 22:26:29 +00001402 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001404 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
1406 ++emsg_off;
1407
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001408 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 retval = -1;
1410 else
1411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001412 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001413 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
1415 --emsg_off;
1416
1417 return retval;
1418}
1419
Bram Moolenaara40058a2005-07-11 22:42:07 +00001420/*
1421 * Prepare v: variable "idx" to be used.
1422 * Save the current typeval in "save_tv".
1423 * When not used yet add the variable to the v: hashtable.
1424 */
1425 static void
1426prepare_vimvar(idx, save_tv)
1427 int idx;
1428 typval_T *save_tv;
1429{
1430 *save_tv = vimvars[idx].vv_tv;
1431 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1432 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1433}
1434
1435/*
1436 * Restore v: variable "idx" to typeval "save_tv".
1437 * When no longer defined, remove the variable from the v: hashtable.
1438 */
1439 static void
1440restore_vimvar(idx, save_tv)
1441 int idx;
1442 typval_T *save_tv;
1443{
1444 hashitem_T *hi;
1445
Bram Moolenaara40058a2005-07-11 22:42:07 +00001446 vimvars[idx].vv_tv = *save_tv;
1447 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1448 {
1449 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1450 if (HASHITEM_EMPTY(hi))
1451 EMSG2(_(e_intern2), "restore_vimvar()");
1452 else
1453 hash_remove(&vimvarht, hi);
1454 }
1455}
1456
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001457#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001458/*
1459 * Evaluate an expression to a list with suggestions.
1460 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001461 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001462 */
1463 list_T *
1464eval_spell_expr(badword, expr)
1465 char_u *badword;
1466 char_u *expr;
1467{
1468 typval_T save_val;
1469 typval_T rettv;
1470 list_T *list = NULL;
1471 char_u *p = skipwhite(expr);
1472
1473 /* Set "v:val" to the bad word. */
1474 prepare_vimvar(VV_VAL, &save_val);
1475 vimvars[VV_VAL].vv_type = VAR_STRING;
1476 vimvars[VV_VAL].vv_str = badword;
1477 if (p_verbose == 0)
1478 ++emsg_off;
1479
1480 if (eval1(&p, &rettv, TRUE) == OK)
1481 {
1482 if (rettv.v_type != VAR_LIST)
1483 clear_tv(&rettv);
1484 else
1485 list = rettv.vval.v_list;
1486 }
1487
1488 if (p_verbose == 0)
1489 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001490 restore_vimvar(VV_VAL, &save_val);
1491
1492 return list;
1493}
1494
1495/*
1496 * "list" is supposed to contain two items: a word and a number. Return the
1497 * word in "pp" and the number as the return value.
1498 * Return -1 if anything isn't right.
1499 * Used to get the good word and score from the eval_spell_expr() result.
1500 */
1501 int
1502get_spellword(list, pp)
1503 list_T *list;
1504 char_u **pp;
1505{
1506 listitem_T *li;
1507
1508 li = list->lv_first;
1509 if (li == NULL)
1510 return -1;
1511 *pp = get_tv_string(&li->li_tv);
1512
1513 li = li->li_next;
1514 if (li == NULL)
1515 return -1;
1516 return get_tv_number(&li->li_tv);
1517}
1518#endif
1519
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001520/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001521 * Top level evaluation function.
1522 * Returns an allocated typval_T with the result.
1523 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001524 */
1525 typval_T *
1526eval_expr(arg, nextcmd)
1527 char_u *arg;
1528 char_u **nextcmd;
1529{
1530 typval_T *tv;
1531
1532 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001533 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001534 {
1535 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001536 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001537 }
1538
1539 return tv;
1540}
1541
1542
Bram Moolenaar4f688582007-07-24 12:34:30 +00001543#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1544 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001546 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001547 * Uses argv[argc] for the function arguments. Only Number and String
1548 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001549 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001551 static int
1552call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 char_u *func;
1554 int argc;
1555 char_u **argv;
1556 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
Bram Moolenaar33570922005-01-25 22:26:29 +00001559 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 long n;
1561 int len;
1562 int i;
1563 int doesrange;
1564 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001567 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001569 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
1571 for (i = 0; i < argc; i++)
1572 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001573 /* Pass a NULL or empty argument as an empty string */
1574 if (argv[i] == NULL || *argv[i] == NUL)
1575 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001576 argvars[i].v_type = VAR_STRING;
1577 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001578 continue;
1579 }
1580
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 /* Recognize a number argument, the others must be strings. */
1582 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1583 if (len != 0 && len == (int)STRLEN(argv[i]))
1584 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001585 argvars[i].v_type = VAR_NUMBER;
1586 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588 else
1589 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001590 argvars[i].v_type = VAR_STRING;
1591 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593 }
1594
1595 if (safe)
1596 {
1597 save_funccalp = save_funccal();
1598 ++sandbox;
1599 }
1600
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001601 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1602 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001604 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (safe)
1606 {
1607 --sandbox;
1608 restore_funccal(save_funccalp);
1609 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 vim_free(argvars);
1611
1612 if (ret == FAIL)
1613 clear_tv(rettv);
1614
1615 return ret;
1616}
1617
Bram Moolenaar4f688582007-07-24 12:34:30 +00001618# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001620 * Call vimL function "func" and return the result as a string.
1621 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001622 * Uses argv[argc] for the function arguments.
1623 */
1624 void *
1625call_func_retstr(func, argc, argv, safe)
1626 char_u *func;
1627 int argc;
1628 char_u **argv;
1629 int safe; /* use the sandbox */
1630{
1631 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001632 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633
1634 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1635 return NULL;
1636
1637 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 return retval;
1640}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001641# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642
Bram Moolenaar4f688582007-07-24 12:34:30 +00001643# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001645 * Call vimL function "func" and return the result as a number.
1646 * Returns -1 when calling the function fails.
1647 * Uses argv[argc] for the function arguments.
1648 */
1649 long
1650call_func_retnr(func, argc, argv, safe)
1651 char_u *func;
1652 int argc;
1653 char_u **argv;
1654 int safe; /* use the sandbox */
1655{
1656 typval_T rettv;
1657 long retval;
1658
1659 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1660 return -1;
1661
1662 retval = get_tv_number_chk(&rettv, NULL);
1663 clear_tv(&rettv);
1664 return retval;
1665}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001666# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001667
1668/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001669 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001670 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001671 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 */
1673 void *
1674call_func_retlist(func, argc, argv, safe)
1675 char_u *func;
1676 int argc;
1677 char_u **argv;
1678 int safe; /* use the sandbox */
1679{
1680 typval_T rettv;
1681
1682 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1683 return NULL;
1684
1685 if (rettv.v_type != VAR_LIST)
1686 {
1687 clear_tv(&rettv);
1688 return NULL;
1689 }
1690
1691 return rettv.vval.v_list;
1692}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#endif
1694
Bram Moolenaar4f688582007-07-24 12:34:30 +00001695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696/*
1697 * Save the current function call pointer, and set it to NULL.
1698 * Used when executing autocommands and for ":source".
1699 */
1700 void *
1701save_funccal()
1702{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001703 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 current_funccal = NULL;
1706 return (void *)fc;
1707}
1708
1709 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001710restore_funccal(vfc)
1711 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001713 funccall_T *fc = (funccall_T *)vfc;
1714
1715 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716}
1717
Bram Moolenaar05159a02005-02-26 23:04:13 +00001718#if defined(FEAT_PROFILE) || defined(PROTO)
1719/*
1720 * Prepare profiling for entering a child or something else that is not
1721 * counted for the script/function itself.
1722 * Should always be called in pair with prof_child_exit().
1723 */
1724 void
1725prof_child_enter(tm)
1726 proftime_T *tm; /* place to store waittime */
1727{
1728 funccall_T *fc = current_funccal;
1729
1730 if (fc != NULL && fc->func->uf_profiling)
1731 profile_start(&fc->prof_child);
1732 script_prof_save(tm);
1733}
1734
1735/*
1736 * Take care of time spent in a child.
1737 * Should always be called after prof_child_enter().
1738 */
1739 void
1740prof_child_exit(tm)
1741 proftime_T *tm; /* where waittime was stored */
1742{
1743 funccall_T *fc = current_funccal;
1744
1745 if (fc != NULL && fc->func->uf_profiling)
1746 {
1747 profile_end(&fc->prof_child);
1748 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1749 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1750 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1751 }
1752 script_prof_restore(tm);
1753}
1754#endif
1755
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757#ifdef FEAT_FOLDING
1758/*
1759 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1760 * it in "*cp". Doesn't give error messages.
1761 */
1762 int
1763eval_foldexpr(arg, cp)
1764 char_u *arg;
1765 int *cp;
1766{
Bram Moolenaar33570922005-01-25 22:26:29 +00001767 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 int retval;
1769 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001770 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1771 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001774 if (use_sandbox)
1775 ++sandbox;
1776 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001778 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 retval = 0;
1780 else
1781 {
1782 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 if (tv.v_type == VAR_NUMBER)
1784 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001785 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 retval = 0;
1787 else
1788 {
1789 /* If the result is a string, check if there is a non-digit before
1790 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (!VIM_ISDIGIT(*s) && *s != '-')
1793 *cp = *s++;
1794 retval = atol((char *)s);
1795 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
1798 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001799 if (use_sandbox)
1800 --sandbox;
1801 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
1803 return retval;
1804}
1805#endif
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 * ":let" list all variable values
1809 * ":let var1 var2" list variable values
1810 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001811 * ":let var += expr" assignment command.
1812 * ":let var -= expr" assignment command.
1813 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 */
1816 void
1817ex_let(eap)
1818 exarg_T *eap;
1819{
1820 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001822 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 int var_count = 0;
1825 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001826 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001827 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001828 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaardb552d602006-03-23 22:59:57 +00001830 argend = skip_var_list(arg, &var_count, &semicolon);
1831 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001833 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1834 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001835 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001838 /*
1839 * ":let" without "=": list variables
1840 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 if (*arg == '[')
1842 EMSG(_(e_invarg));
1843 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001845 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001846 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001849 list_glob_vars(&first);
1850 list_buf_vars(&first);
1851 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001852#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001853 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001854#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001855 list_script_vars(&first);
1856 list_func_vars(&first);
1857 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 eap->nextcmd = check_nextcmd(arg);
1860 }
1861 else
1862 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001863 op[0] = '=';
1864 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001865 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001866 {
1867 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1868 op[0] = expr[-1]; /* +=, -= or .= */
1869 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (eap->skip)
1873 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (eap->skip)
1876 {
1877 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 --emsg_skip;
1880 }
1881 else if (i != FAIL)
1882 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001884 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 }
1887 }
1888}
1889
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890/*
1891 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1892 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 * When "nextchars" is not NULL it points to a string with characters that
1894 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1895 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 * Returns OK or FAIL;
1897 */
1898 static int
1899ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1900 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001901 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 int copy; /* copy values from "tv", don't move */
1903 int semicolon; /* from skip_var_list() */
1904 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001905 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906{
1907 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001908 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001910 listitem_T *item;
1911 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912
1913 if (*arg != '[')
1914 {
1915 /*
1916 * ":let var = expr" or ":for var in list"
1917 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001918 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 return FAIL;
1920 return OK;
1921 }
1922
1923 /*
1924 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1925 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001926 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927 {
1928 EMSG(_(e_listreq));
1929 return FAIL;
1930 }
1931
1932 i = list_len(l);
1933 if (semicolon == 0 && var_count < i)
1934 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001935 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 return FAIL;
1937 }
1938 if (var_count - semicolon > i)
1939 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001940 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 return FAIL;
1942 }
1943
1944 item = l->lv_first;
1945 while (*arg != ']')
1946 {
1947 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 item = item->li_next;
1950 if (arg == NULL)
1951 return FAIL;
1952
1953 arg = skipwhite(arg);
1954 if (*arg == ';')
1955 {
1956 /* Put the rest of the list (may be empty) in the var after ';'.
1957 * Create a new list for this. */
1958 l = list_alloc();
1959 if (l == NULL)
1960 return FAIL;
1961 while (item != NULL)
1962 {
1963 list_append_tv(l, &item->li_tv);
1964 item = item->li_next;
1965 }
1966
1967 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001968 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 ltv.vval.v_list = l;
1970 l->lv_refcount = 1;
1971
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001972 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1973 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 clear_tv(&ltv);
1975 if (arg == NULL)
1976 return FAIL;
1977 break;
1978 }
1979 else if (*arg != ',' && *arg != ']')
1980 {
1981 EMSG2(_(e_intern2), "ex_let_vars()");
1982 return FAIL;
1983 }
1984 }
1985
1986 return OK;
1987}
1988
1989/*
1990 * Skip over assignable variable "var" or list of variables "[var, var]".
1991 * Used for ":let varvar = expr" and ":for varvar in expr".
1992 * For "[var, var]" increment "*var_count" for each variable.
1993 * for "[var, var; var]" set "semicolon".
1994 * Return NULL for an error.
1995 */
1996 static char_u *
1997skip_var_list(arg, var_count, semicolon)
1998 char_u *arg;
1999 int *var_count;
2000 int *semicolon;
2001{
2002 char_u *p, *s;
2003
2004 if (*arg == '[')
2005 {
2006 /* "[var, var]": find the matching ']'. */
2007 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002008 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009 {
2010 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2011 s = skip_var_one(p);
2012 if (s == p)
2013 {
2014 EMSG2(_(e_invarg2), p);
2015 return NULL;
2016 }
2017 ++*var_count;
2018
2019 p = skipwhite(s);
2020 if (*p == ']')
2021 break;
2022 else if (*p == ';')
2023 {
2024 if (*semicolon == 1)
2025 {
2026 EMSG(_("Double ; in list of variables"));
2027 return NULL;
2028 }
2029 *semicolon = 1;
2030 }
2031 else if (*p != ',')
2032 {
2033 EMSG2(_(e_invarg2), p);
2034 return NULL;
2035 }
2036 }
2037 return p + 1;
2038 }
2039 else
2040 return skip_var_one(arg);
2041}
2042
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002043/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002044 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002045 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002046 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047 static char_u *
2048skip_var_one(arg)
2049 char_u *arg;
2050{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002051 if (*arg == '@' && arg[1] != NUL)
2052 return arg + 2;
2053 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2054 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002055}
2056
Bram Moolenaara7043832005-01-21 11:56:39 +00002057/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002058 * List variables for hashtab "ht" with prefix "prefix".
2059 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002060 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002062list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002063 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002064 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002065 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002066 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002067{
Bram Moolenaar33570922005-01-25 22:26:29 +00002068 hashitem_T *hi;
2069 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 int todo;
2071
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002072 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2074 {
2075 if (!HASHITEM_EMPTY(hi))
2076 {
2077 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 di = HI2DI(hi);
2079 if (empty || di->di_tv.v_type != VAR_STRING
2080 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002082 }
2083 }
2084}
2085
2086/*
2087 * List global variables.
2088 */
2089 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002090list_glob_vars(first)
2091 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002092{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002093 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002094}
2095
2096/*
2097 * List buffer variables.
2098 */
2099 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100list_buf_vars(first)
2101 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002103 char_u numbuf[NUMBUFLEN];
2104
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2106 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002107
2108 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2110 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002111}
2112
2113/*
2114 * List window variables.
2115 */
2116 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117list_win_vars(first)
2118 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2121 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002122}
2123
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002124#ifdef FEAT_WINDOWS
2125/*
2126 * List tab page variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_tab_vars(first)
2130 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002131{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2133 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002134}
2135#endif
2136
Bram Moolenaara7043832005-01-21 11:56:39 +00002137/*
2138 * List Vim variables.
2139 */
2140 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141list_vim_vars(first)
2142 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145}
2146
2147/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002148 * List script-local variables, if there is a script.
2149 */
2150 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151list_script_vars(first)
2152 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002153{
2154 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2156 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002157}
2158
2159/*
2160 * List function variables, if there is a function.
2161 */
2162 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163list_func_vars(first)
2164 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002165{
2166 if (current_funccal != NULL)
2167 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002169}
2170
2171/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 * List variables in "arg".
2173 */
2174 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176 exarg_T *eap;
2177 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179{
2180 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002181 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002183 char_u *name_start;
2184 char_u *arg_subsc;
2185 char_u *tofree;
2186 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187
2188 while (!ends_excmd(*arg) && !got_int)
2189 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002192 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2194 {
2195 emsg_severe = TRUE;
2196 EMSG(_(e_trailing));
2197 break;
2198 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002202 /* get_name_len() takes care of expanding curly braces */
2203 name_start = name = arg;
2204 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2205 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 /* This is mainly to keep test 49 working: when expanding
2208 * curly braces fails overrule the exception error message. */
2209 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 emsg_severe = TRUE;
2212 EMSG2(_(e_invarg2), arg);
2213 break;
2214 }
2215 error = TRUE;
2216 }
2217 else
2218 {
2219 if (tofree != NULL)
2220 name = tofree;
2221 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 else
2224 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 /* handle d.key, l[idx], f(expr) */
2226 arg_subsc = arg;
2227 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002228 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002230 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002232 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002234 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002235 case 'g': list_glob_vars(first); break;
2236 case 'b': list_buf_vars(first); break;
2237 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002238#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002239 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002240#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 case 'v': list_vim_vars(first); break;
2242 case 's': list_script_vars(first); break;
2243 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 default:
2245 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002246 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 }
2248 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 {
2250 char_u numbuf[NUMBUFLEN];
2251 char_u *tf;
2252 int c;
2253 char_u *s;
2254
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002255 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 c = *arg;
2257 *arg = NUL;
2258 list_one_var_a((char_u *)"",
2259 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 tv.v_type,
2261 s == NULL ? (char_u *)"" : s,
2262 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 *arg = c;
2264 vim_free(tf);
2265 }
2266 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 }
2269 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270
2271 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273
2274 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 }
2276
2277 return arg;
2278}
2279
2280/*
2281 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2282 * Returns a pointer to the char just after the var name.
2283 * Returns NULL if there is an error.
2284 */
2285 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002286ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002288 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002290 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002291 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292{
2293 int c1;
2294 char_u *name;
2295 char_u *p;
2296 char_u *arg_end = NULL;
2297 int len;
2298 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002299 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300
2301 /*
2302 * ":let $VAR = expr": Set environment variable.
2303 */
2304 if (*arg == '$')
2305 {
2306 /* Find the end of the name. */
2307 ++arg;
2308 name = arg;
2309 len = get_env_len(&arg);
2310 if (len == 0)
2311 EMSG2(_(e_invarg2), name - 1);
2312 else
2313 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 if (op != NULL && (*op == '+' || *op == '-'))
2315 EMSG2(_(e_letwrong), op);
2316 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002317 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 EMSG(_(e_letunexp));
2319 else
2320 {
2321 c1 = name[len];
2322 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002323 p = get_tv_string_chk(tv);
2324 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325 {
2326 int mustfree = FALSE;
2327 char_u *s = vim_getenv(name, &mustfree);
2328
2329 if (s != NULL)
2330 {
2331 p = tofree = concat_str(s, p);
2332 if (mustfree)
2333 vim_free(s);
2334 }
2335 }
2336 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002337 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002339 if (STRICMP(name, "HOME") == 0)
2340 init_homedir();
2341 else if (didset_vim && STRICMP(name, "VIM") == 0)
2342 didset_vim = FALSE;
2343 else if (didset_vimruntime
2344 && STRICMP(name, "VIMRUNTIME") == 0)
2345 didset_vimruntime = FALSE;
2346 arg_end = arg;
2347 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002348 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 }
2351 }
2352 }
2353
2354 /*
2355 * ":let &option = expr": Set option value.
2356 * ":let &l:option = expr": Set local option value.
2357 * ":let &g:option = expr": Set global option value.
2358 */
2359 else if (*arg == '&')
2360 {
2361 /* Find the end of the name. */
2362 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002363 if (p == NULL || (endchars != NULL
2364 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 EMSG(_(e_letunexp));
2366 else
2367 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 long n;
2369 int opt_type;
2370 long numval;
2371 char_u *stringval = NULL;
2372 char_u *s;
2373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 c1 = *p;
2375 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002376
2377 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 s = get_tv_string_chk(tv); /* != NULL if number or string */
2379 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 {
2381 opt_type = get_option_value(arg, &numval,
2382 &stringval, opt_flags);
2383 if ((opt_type == 1 && *op == '.')
2384 || (opt_type == 0 && *op != '.'))
2385 EMSG2(_(e_letwrong), op);
2386 else
2387 {
2388 if (opt_type == 1) /* number */
2389 {
2390 if (*op == '+')
2391 n = numval + n;
2392 else
2393 n = numval - n;
2394 }
2395 else if (opt_type == 0 && stringval != NULL) /* string */
2396 {
2397 s = concat_str(stringval, s);
2398 vim_free(stringval);
2399 stringval = s;
2400 }
2401 }
2402 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 if (s != NULL)
2404 {
2405 set_option_value(arg, n, s, opt_flags);
2406 arg_end = p;
2407 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002408 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 }
2411 }
2412
2413 /*
2414 * ":let @r = expr": Set register contents.
2415 */
2416 else if (*arg == '@')
2417 {
2418 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 if (op != NULL && (*op == '+' || *op == '-'))
2420 EMSG2(_(e_letwrong), op);
2421 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002422 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 EMSG(_(e_letunexp));
2424 else
2425 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002426 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 char_u *s;
2428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 p = get_tv_string_chk(tv);
2430 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002432 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002433 if (s != NULL)
2434 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002435 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 vim_free(s);
2437 }
2438 }
2439 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 arg_end = arg + 1;
2443 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002444 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002445 }
2446 }
2447
2448 /*
2449 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002452 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002454 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002456 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002457 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2460 EMSG(_(e_letunexp));
2461 else
2462 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 arg_end = p;
2465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
2469
2470 else
2471 EMSG2(_(e_invarg2), arg);
2472
2473 return arg_end;
2474}
2475
2476/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002477 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2478 */
2479 static int
2480check_changedtick(arg)
2481 char_u *arg;
2482{
2483 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2484 {
2485 EMSG2(_(e_readonlyvar), arg);
2486 return TRUE;
2487 }
2488 return FALSE;
2489}
2490
2491/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 * Get an lval: variable, Dict item or List item that can be assigned a value
2493 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2494 * "name.key", "name.key[expr]" etc.
2495 * Indexing only works if "name" is an existing List or Dictionary.
2496 * "name" points to the start of the name.
2497 * If "rettv" is not NULL it points to the value to be assigned.
2498 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2499 * wrong; must end in space or cmd separator.
2500 *
2501 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002502 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 */
2505 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002506get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002508 typval_T *rettv;
2509 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 int unlet;
2511 int skip;
2512 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002513 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 char_u *expr_start, *expr_end;
2517 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 dictitem_T *v;
2519 typval_T var1;
2520 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002522 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002523 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002524 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529
2530 if (skip)
2531 {
2532 /* When skipping just find the end of the name. */
2533 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002534 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 }
2536
2537 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002538 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 if (expr_start != NULL)
2540 {
2541 /* Don't expand the name when we already know there is an error. */
2542 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2543 && *p != '[' && *p != '.')
2544 {
2545 EMSG(_(e_trailing));
2546 return NULL;
2547 }
2548
2549 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2550 if (lp->ll_exp_name == NULL)
2551 {
2552 /* Report an invalid expression in braces, unless the
2553 * expression evaluation has been cancelled due to an
2554 * aborting error, an interrupt, or an exception. */
2555 if (!aborting() && !quiet)
2556 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002557 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 EMSG2(_(e_invarg2), name);
2559 return NULL;
2560 }
2561 }
2562 lp->ll_name = lp->ll_exp_name;
2563 }
2564 else
2565 lp->ll_name = name;
2566
2567 /* Without [idx] or .key we are done. */
2568 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2569 return p;
2570
2571 cc = *p;
2572 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002573 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 if (v == NULL && !quiet)
2575 EMSG2(_(e_undefvar), lp->ll_name);
2576 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 if (v == NULL)
2578 return NULL;
2579
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 /*
2581 * Loop until no more [idx] or .key is following.
2582 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002583 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2587 && !(lp->ll_tv->v_type == VAR_DICT
2588 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (!quiet)
2591 EMSG(_("E689: Can only index a List or Dictionary"));
2592 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (!quiet)
2597 EMSG(_("E708: [:] must come last"));
2598 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002600
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 len = -1;
2602 if (*p == '.')
2603 {
2604 key = p + 1;
2605 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2606 ;
2607 if (len == 0)
2608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (!quiet)
2610 EMSG(_(e_emptykey));
2611 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 }
2613 p = key + len;
2614 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615 else
2616 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002618 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 if (*p == ':')
2620 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002621 else
2622 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 empty1 = FALSE;
2624 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002626 if (get_tv_string_chk(&var1) == NULL)
2627 {
2628 /* not a number or string */
2629 clear_tv(&var1);
2630 return NULL;
2631 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 }
2633
2634 /* Optionally get the second index [ :expr]. */
2635 if (*p == ':')
2636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002640 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002641 if (!empty1)
2642 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (rettv != NULL && (rettv->v_type != VAR_LIST
2646 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (!quiet)
2649 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 if (!empty1)
2651 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 }
2654 p = skipwhite(p + 1);
2655 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 else
2658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2661 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 if (!empty1)
2663 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002665 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002666 if (get_tv_string_chk(&var2) == NULL)
2667 {
2668 /* not a number or string */
2669 if (!empty1)
2670 clear_tv(&var1);
2671 clear_tv(&var2);
2672 return NULL;
2673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
2683 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 if (!empty1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
2690
2691 /* Skip to past ']'. */
2692 ++p;
2693 }
2694
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 {
2697 if (len == -1)
2698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002700 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 if (*key == NUL)
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (!quiet)
2704 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 }
2708 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002709 lp->ll_list = NULL;
2710 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002711 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002714 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 if (len == -1)
2720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (len == -1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 p = NULL;
2731 break;
2732 }
2733 if (len == -1)
2734 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 }
2737 else
2738 {
2739 /*
2740 * Get the number and item for the only or first index of the List.
2741 */
2742 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 else
2745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002746 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 clear_tv(&var1);
2748 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002749 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 lp->ll_list = lp->ll_tv->vval.v_list;
2751 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2752 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002754 if (lp->ll_n1 < 0)
2755 {
2756 lp->ll_n1 = 0;
2757 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2758 }
2759 }
2760 if (lp->ll_li == NULL)
2761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 }
2766
2767 /*
2768 * May need to find the item or absolute index for the second
2769 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 * When no index given: "lp->ll_empty2" is TRUE.
2771 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002775 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
2784
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2786 if (lp->ll_n1 < 0)
2787 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2788 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002790 }
2791
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002793 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002794 }
2795
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 return p;
2797}
2798
2799/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 */
2802 static void
2803clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002804 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805{
2806 vim_free(lp->ll_exp_name);
2807 vim_free(lp->ll_newkey);
2808}
2809
2810/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002811 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 */
2815 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002816set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002817 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002819 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822{
2823 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002824 listitem_T *ri;
2825 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826
2827 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002828 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002830 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 cc = *endp;
2832 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002833 if (op != NULL && *op != '=')
2834 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002835 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002836
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002837 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002838 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002839 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002840 {
2841 if (tv_op(&tv, rettv, op) == OK)
2842 set_var(lp->ll_name, &tv, FALSE);
2843 clear_tv(&tv);
2844 }
2845 }
2846 else
2847 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002849 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002851 else if (tv_check_lock(lp->ll_newkey == NULL
2852 ? lp->ll_tv->v_lock
2853 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2854 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 else if (lp->ll_range)
2856 {
2857 /*
2858 * Assign the List values to the list items.
2859 */
2860 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002861 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002862 if (op != NULL && *op != '=')
2863 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2864 else
2865 {
2866 clear_tv(&lp->ll_li->li_tv);
2867 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2868 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 ri = ri->li_next;
2870 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2871 break;
2872 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002875 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 ri = NULL;
2878 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002879 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002880 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 lp->ll_li = lp->ll_li->li_next;
2882 ++lp->ll_n1;
2883 }
2884 if (ri != NULL)
2885 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002886 else if (lp->ll_empty2
2887 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 : lp->ll_n1 != lp->ll_n2)
2889 EMSG(_("E711: List value has not enough items"));
2890 }
2891 else
2892 {
2893 /*
2894 * Assign to a List or Dictionary item.
2895 */
2896 if (lp->ll_newkey != NULL)
2897 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002898 if (op != NULL && *op != '=')
2899 {
2900 EMSG2(_(e_letwrong), op);
2901 return;
2902 }
2903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002905 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 if (di == NULL)
2907 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002908 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2909 {
2910 vim_free(di);
2911 return;
2912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002914 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915 else if (op != NULL && *op != '=')
2916 {
2917 tv_op(lp->ll_tv, rettv, op);
2918 return;
2919 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002920 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002922
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 /*
2924 * Assign the value to the variable or list item.
2925 */
2926 if (copy)
2927 copy_tv(rettv, lp->ll_tv);
2928 else
2929 {
2930 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002931 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 }
2934 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935}
2936
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002937/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002938 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2939 * Returns OK or FAIL.
2940 */
2941 static int
2942tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002943 typval_T *tv1;
2944 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 char_u *op;
2946{
2947 long n;
2948 char_u numbuf[NUMBUFLEN];
2949 char_u *s;
2950
2951 /* Can't do anything with a Funcref or a Dict on the right. */
2952 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2953 {
2954 switch (tv1->v_type)
2955 {
2956 case VAR_DICT:
2957 case VAR_FUNC:
2958 break;
2959
2960 case VAR_LIST:
2961 if (*op != '+' || tv2->v_type != VAR_LIST)
2962 break;
2963 /* List += List */
2964 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2965 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2966 return OK;
2967
2968 case VAR_NUMBER:
2969 case VAR_STRING:
2970 if (tv2->v_type == VAR_LIST)
2971 break;
2972 if (*op == '+' || *op == '-')
2973 {
2974 /* nr += nr or nr -= nr*/
2975 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002976#ifdef FEAT_FLOAT
2977 if (tv2->v_type == VAR_FLOAT)
2978 {
2979 float_T f = n;
2980
2981 if (*op == '+')
2982 f += tv2->vval.v_float;
2983 else
2984 f -= tv2->vval.v_float;
2985 clear_tv(tv1);
2986 tv1->v_type = VAR_FLOAT;
2987 tv1->vval.v_float = f;
2988 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002990#endif
2991 {
2992 if (*op == '+')
2993 n += get_tv_number(tv2);
2994 else
2995 n -= get_tv_number(tv2);
2996 clear_tv(tv1);
2997 tv1->v_type = VAR_NUMBER;
2998 tv1->vval.v_number = n;
2999 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 }
3001 else
3002 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003003 if (tv2->v_type == VAR_FLOAT)
3004 break;
3005
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 /* str .= str */
3007 s = get_tv_string(tv1);
3008 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3009 clear_tv(tv1);
3010 tv1->v_type = VAR_STRING;
3011 tv1->vval.v_string = s;
3012 }
3013 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003014
3015#ifdef FEAT_FLOAT
3016 case VAR_FLOAT:
3017 {
3018 float_T f;
3019
3020 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3021 && tv2->v_type != VAR_NUMBER
3022 && tv2->v_type != VAR_STRING))
3023 break;
3024 if (tv2->v_type == VAR_FLOAT)
3025 f = tv2->vval.v_float;
3026 else
3027 f = get_tv_number(tv2);
3028 if (*op == '+')
3029 tv1->vval.v_float += f;
3030 else
3031 tv1->vval.v_float -= f;
3032 }
3033 return OK;
3034#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003035 }
3036 }
3037
3038 EMSG2(_(e_letwrong), op);
3039 return FAIL;
3040}
3041
3042/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003043 * Add a watcher to a list.
3044 */
3045 static void
3046list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003047 list_T *l;
3048 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003049{
3050 lw->lw_next = l->lv_watch;
3051 l->lv_watch = lw;
3052}
3053
3054/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003055 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003056 * No warning when it isn't found...
3057 */
3058 static void
3059list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003060 list_T *l;
3061 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003062{
Bram Moolenaar33570922005-01-25 22:26:29 +00003063 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003064
3065 lwp = &l->lv_watch;
3066 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3067 {
3068 if (lw == lwrem)
3069 {
3070 *lwp = lw->lw_next;
3071 break;
3072 }
3073 lwp = &lw->lw_next;
3074 }
3075}
3076
3077/*
3078 * Just before removing an item from a list: advance watchers to the next
3079 * item.
3080 */
3081 static void
3082list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003083 list_T *l;
3084 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003085{
Bram Moolenaar33570922005-01-25 22:26:29 +00003086 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003087
3088 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3089 if (lw->lw_item == item)
3090 lw->lw_item = item->li_next;
3091}
3092
3093/*
3094 * Evaluate the expression used in a ":for var in expr" command.
3095 * "arg" points to "var".
3096 * Set "*errp" to TRUE for an error, FALSE otherwise;
3097 * Return a pointer that holds the info. Null when there is an error.
3098 */
3099 void *
3100eval_for_line(arg, errp, nextcmdp, skip)
3101 char_u *arg;
3102 int *errp;
3103 char_u **nextcmdp;
3104 int skip;
3105{
Bram Moolenaar33570922005-01-25 22:26:29 +00003106 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003108 typval_T tv;
3109 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110
3111 *errp = TRUE; /* default: there is an error */
3112
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114 if (fi == NULL)
3115 return NULL;
3116
3117 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3118 if (expr == NULL)
3119 return fi;
3120
3121 expr = skipwhite(expr);
3122 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3123 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003124 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003125 return fi;
3126 }
3127
3128 if (skip)
3129 ++emsg_skip;
3130 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3131 {
3132 *errp = FALSE;
3133 if (!skip)
3134 {
3135 l = tv.vval.v_list;
3136 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003137 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003138 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003139 clear_tv(&tv);
3140 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003141 else
3142 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003143 /* No need to increment the refcount, it's already set for the
3144 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145 fi->fi_list = l;
3146 list_add_watch(l, &fi->fi_lw);
3147 fi->fi_lw.lw_item = l->lv_first;
3148 }
3149 }
3150 }
3151 if (skip)
3152 --emsg_skip;
3153
3154 return fi;
3155}
3156
3157/*
3158 * Use the first item in a ":for" list. Advance to the next.
3159 * Assign the values to the variable (list). "arg" points to the first one.
3160 * Return TRUE when a valid item was found, FALSE when at end of list or
3161 * something wrong.
3162 */
3163 int
3164next_for_item(fi_void, arg)
3165 void *fi_void;
3166 char_u *arg;
3167{
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171
3172 item = fi->fi_lw.lw_item;
3173 if (item == NULL)
3174 result = FALSE;
3175 else
3176 {
3177 fi->fi_lw.lw_item = item->li_next;
3178 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3179 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3180 }
3181 return result;
3182}
3183
3184/*
3185 * Free the structure used to store info used by ":for".
3186 */
3187 void
3188free_for_info(fi_void)
3189 void *fi_void;
3190{
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003193 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003194 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003196 list_unref(fi->fi_list);
3197 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 vim_free(fi);
3199}
3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3202
3203 void
3204set_context_for_expression(xp, arg, cmdidx)
3205 expand_T *xp;
3206 char_u *arg;
3207 cmdidx_T cmdidx;
3208{
3209 int got_eq = FALSE;
3210 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 if (cmdidx == CMD_let)
3214 {
3215 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003216 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 {
3218 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003219 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 {
3221 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223 if (vim_iswhite(*p))
3224 break;
3225 }
3226 return;
3227 }
3228 }
3229 else
3230 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3231 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 while ((xp->xp_pattern = vim_strpbrk(arg,
3233 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3234 {
3235 c = *xp->xp_pattern;
3236 if (c == '&')
3237 {
3238 c = xp->xp_pattern[1];
3239 if (c == '&')
3240 {
3241 ++xp->xp_pattern;
3242 xp->xp_context = cmdidx != CMD_let || got_eq
3243 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3244 }
3245 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003248 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3249 xp->xp_pattern += 2;
3250
3251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 }
3253 else if (c == '$')
3254 {
3255 /* environment variable */
3256 xp->xp_context = EXPAND_ENV_VARS;
3257 }
3258 else if (c == '=')
3259 {
3260 got_eq = TRUE;
3261 xp->xp_context = EXPAND_EXPRESSION;
3262 }
3263 else if (c == '<'
3264 && xp->xp_context == EXPAND_FUNCTIONS
3265 && vim_strchr(xp->xp_pattern, '(') == NULL)
3266 {
3267 /* Function name can start with "<SNR>" */
3268 break;
3269 }
3270 else if (cmdidx != CMD_let || got_eq)
3271 {
3272 if (c == '"') /* string */
3273 {
3274 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3275 if (c == '\\' && xp->xp_pattern[1] != NUL)
3276 ++xp->xp_pattern;
3277 xp->xp_context = EXPAND_NOTHING;
3278 }
3279 else if (c == '\'') /* literal string */
3280 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003281 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3283 /* skip */ ;
3284 xp->xp_context = EXPAND_NOTHING;
3285 }
3286 else if (c == '|')
3287 {
3288 if (xp->xp_pattern[1] == '|')
3289 {
3290 ++xp->xp_pattern;
3291 xp->xp_context = EXPAND_EXPRESSION;
3292 }
3293 else
3294 xp->xp_context = EXPAND_COMMANDS;
3295 }
3296 else
3297 xp->xp_context = EXPAND_EXPRESSION;
3298 }
3299 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300 /* Doesn't look like something valid, expand as an expression
3301 * anyway. */
3302 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 arg = xp->xp_pattern;
3304 if (*arg != NUL)
3305 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3306 /* skip */ ;
3307 }
3308 xp->xp_pattern = arg;
3309}
3310
3311#endif /* FEAT_CMDL_COMPL */
3312
3313/*
3314 * ":1,25call func(arg1, arg2)" function call.
3315 */
3316 void
3317ex_call(eap)
3318 exarg_T *eap;
3319{
3320 char_u *arg = eap->arg;
3321 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003323 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003325 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 linenr_T lnum;
3327 int doesrange;
3328 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003329 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003331 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003332 if (fudi.fd_newkey != NULL)
3333 {
3334 /* Still need to give an error message for missing key. */
3335 EMSG2(_(e_dictkey), fudi.fd_newkey);
3336 vim_free(fudi.fd_newkey);
3337 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003338 if (tofree == NULL)
3339 return;
3340
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003341 /* Increase refcount on dictionary, it could get deleted when evaluating
3342 * the arguments. */
3343 if (fudi.fd_dict != NULL)
3344 ++fudi.fd_dict->dv_refcount;
3345
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003346 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003347 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003348 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
Bram Moolenaar532c7802005-01-27 14:44:31 +00003350 /* Skip white space to allow ":call func ()". Not good, but required for
3351 * backward compatibility. */
3352 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003353 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
3355 if (*startarg != '(')
3356 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003357 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 goto end;
3359 }
3360
3361 /*
3362 * When skipping, evaluate the function once, to find the end of the
3363 * arguments.
3364 * When the function takes a range, this is discovered after the first
3365 * call, and the loop is broken.
3366 */
3367 if (eap->skip)
3368 {
3369 ++emsg_skip;
3370 lnum = eap->line2; /* do it once, also with an invalid range */
3371 }
3372 else
3373 lnum = eap->line1;
3374 for ( ; lnum <= eap->line2; ++lnum)
3375 {
3376 if (!eap->skip && eap->addr_count > 0)
3377 {
3378 curwin->w_cursor.lnum = lnum;
3379 curwin->w_cursor.col = 0;
3380 }
3381 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003382 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003383 eap->line1, eap->line2, &doesrange,
3384 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
3386 failed = TRUE;
3387 break;
3388 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003389
3390 /* Handle a function returning a Funcref, Dictionary or List. */
3391 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3392 {
3393 failed = TRUE;
3394 break;
3395 }
3396
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003397 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (doesrange || eap->skip)
3399 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003402 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003403 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003404 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 if (aborting())
3406 break;
3407 }
3408 if (eap->skip)
3409 --emsg_skip;
3410
3411 if (!failed)
3412 {
3413 /* Check for trailing illegal characters and a following command. */
3414 if (!ends_excmd(*arg))
3415 {
3416 emsg_severe = TRUE;
3417 EMSG(_(e_trailing));
3418 }
3419 else
3420 eap->nextcmd = check_nextcmd(arg);
3421 }
3422
3423end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003424 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003425 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426}
3427
3428/*
3429 * ":unlet[!] var1 ... " command.
3430 */
3431 void
3432ex_unlet(eap)
3433 exarg_T *eap;
3434{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003435 ex_unletlock(eap, eap->arg, 0);
3436}
3437
3438/*
3439 * ":lockvar" and ":unlockvar" commands
3440 */
3441 void
3442ex_lockvar(eap)
3443 exarg_T *eap;
3444{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003446 int deep = 2;
3447
3448 if (eap->forceit)
3449 deep = -1;
3450 else if (vim_isdigit(*arg))
3451 {
3452 deep = getdigits(&arg);
3453 arg = skipwhite(arg);
3454 }
3455
3456 ex_unletlock(eap, arg, deep);
3457}
3458
3459/*
3460 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3461 */
3462 static void
3463ex_unletlock(eap, argstart, deep)
3464 exarg_T *eap;
3465 char_u *argstart;
3466 int deep;
3467{
3468 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003471 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
3473 do
3474 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003475 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003476 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3477 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003478 if (lv.ll_name == NULL)
3479 error = TRUE; /* error but continue parsing */
3480 if (name_end == NULL || (!vim_iswhite(*name_end)
3481 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003483 if (name_end != NULL)
3484 {
3485 emsg_severe = TRUE;
3486 EMSG(_(e_trailing));
3487 }
3488 if (!(eap->skip || error))
3489 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 break;
3491 }
3492
3493 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003494 {
3495 if (eap->cmdidx == CMD_unlet)
3496 {
3497 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3498 error = TRUE;
3499 }
3500 else
3501 {
3502 if (do_lock_var(&lv, name_end, deep,
3503 eap->cmdidx == CMD_lockvar) == FAIL)
3504 error = TRUE;
3505 }
3506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003508 if (!eap->skip)
3509 clear_lval(&lv);
3510
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 arg = skipwhite(name_end);
3512 } while (!ends_excmd(*arg));
3513
3514 eap->nextcmd = check_nextcmd(arg);
3515}
3516
Bram Moolenaar8c711452005-01-14 21:53:12 +00003517 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003518do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003520 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003521 int forceit;
3522{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003523 int ret = OK;
3524 int cc;
3525
3526 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003528 cc = *name_end;
3529 *name_end = NUL;
3530
3531 /* Normal name or expanded name. */
3532 if (check_changedtick(lp->ll_name))
3533 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003535 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003536 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003538 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3539 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003540 else if (lp->ll_range)
3541 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003542 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003543
3544 /* Delete a range of List items. */
3545 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3546 {
3547 li = lp->ll_li->li_next;
3548 listitem_remove(lp->ll_list, lp->ll_li);
3549 lp->ll_li = li;
3550 ++lp->ll_n1;
3551 }
3552 }
3553 else
3554 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003555 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 /* unlet a List item. */
3557 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003558 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003559 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003560 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 }
3562
3563 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003564}
3565
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566/*
3567 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003568 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 */
3570 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003571do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003573 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
Bram Moolenaar33570922005-01-25 22:26:29 +00003575 hashtab_T *ht;
3576 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003577 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003578 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
Bram Moolenaar33570922005-01-25 22:26:29 +00003580 ht = find_var_ht(name, &varname);
3581 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003583 hi = hash_find(ht, varname);
3584 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003585 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003586 di = HI2DI(hi);
3587 if (var_check_fixed(di->di_flags, name)
3588 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 return FAIL;
3590 delete_var(ht, hi);
3591 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 if (forceit)
3595 return OK;
3596 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 return FAIL;
3598}
3599
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003600/*
3601 * Lock or unlock variable indicated by "lp".
3602 * "deep" is the levels to go (-1 for unlimited);
3603 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3604 */
3605 static int
3606do_lock_var(lp, name_end, deep, lock)
3607 lval_T *lp;
3608 char_u *name_end;
3609 int deep;
3610 int lock;
3611{
3612 int ret = OK;
3613 int cc;
3614 dictitem_T *di;
3615
3616 if (deep == 0) /* nothing to do */
3617 return OK;
3618
3619 if (lp->ll_tv == NULL)
3620 {
3621 cc = *name_end;
3622 *name_end = NUL;
3623
3624 /* Normal name or expanded name. */
3625 if (check_changedtick(lp->ll_name))
3626 ret = FAIL;
3627 else
3628 {
3629 di = find_var(lp->ll_name, NULL);
3630 if (di == NULL)
3631 ret = FAIL;
3632 else
3633 {
3634 if (lock)
3635 di->di_flags |= DI_FLAGS_LOCK;
3636 else
3637 di->di_flags &= ~DI_FLAGS_LOCK;
3638 item_lock(&di->di_tv, deep, lock);
3639 }
3640 }
3641 *name_end = cc;
3642 }
3643 else if (lp->ll_range)
3644 {
3645 listitem_T *li = lp->ll_li;
3646
3647 /* (un)lock a range of List items. */
3648 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3649 {
3650 item_lock(&li->li_tv, deep, lock);
3651 li = li->li_next;
3652 ++lp->ll_n1;
3653 }
3654 }
3655 else if (lp->ll_list != NULL)
3656 /* (un)lock a List item. */
3657 item_lock(&lp->ll_li->li_tv, deep, lock);
3658 else
3659 /* un(lock) a Dictionary item. */
3660 item_lock(&lp->ll_di->di_tv, deep, lock);
3661
3662 return ret;
3663}
3664
3665/*
3666 * Lock or unlock an item. "deep" is nr of levels to go.
3667 */
3668 static void
3669item_lock(tv, deep, lock)
3670 typval_T *tv;
3671 int deep;
3672 int lock;
3673{
3674 static int recurse = 0;
3675 list_T *l;
3676 listitem_T *li;
3677 dict_T *d;
3678 hashitem_T *hi;
3679 int todo;
3680
3681 if (recurse >= DICT_MAXNEST)
3682 {
3683 EMSG(_("E743: variable nested too deep for (un)lock"));
3684 return;
3685 }
3686 if (deep == 0)
3687 return;
3688 ++recurse;
3689
3690 /* lock/unlock the item itself */
3691 if (lock)
3692 tv->v_lock |= VAR_LOCKED;
3693 else
3694 tv->v_lock &= ~VAR_LOCKED;
3695
3696 switch (tv->v_type)
3697 {
3698 case VAR_LIST:
3699 if ((l = tv->vval.v_list) != NULL)
3700 {
3701 if (lock)
3702 l->lv_lock |= VAR_LOCKED;
3703 else
3704 l->lv_lock &= ~VAR_LOCKED;
3705 if (deep < 0 || deep > 1)
3706 /* recursive: lock/unlock the items the List contains */
3707 for (li = l->lv_first; li != NULL; li = li->li_next)
3708 item_lock(&li->li_tv, deep - 1, lock);
3709 }
3710 break;
3711 case VAR_DICT:
3712 if ((d = tv->vval.v_dict) != NULL)
3713 {
3714 if (lock)
3715 d->dv_lock |= VAR_LOCKED;
3716 else
3717 d->dv_lock &= ~VAR_LOCKED;
3718 if (deep < 0 || deep > 1)
3719 {
3720 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003721 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003722 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3723 {
3724 if (!HASHITEM_EMPTY(hi))
3725 {
3726 --todo;
3727 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3728 }
3729 }
3730 }
3731 }
3732 }
3733 --recurse;
3734}
3735
Bram Moolenaara40058a2005-07-11 22:42:07 +00003736/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003737 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3738 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003739 */
3740 static int
3741tv_islocked(tv)
3742 typval_T *tv;
3743{
3744 return (tv->v_lock & VAR_LOCKED)
3745 || (tv->v_type == VAR_LIST
3746 && tv->vval.v_list != NULL
3747 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3748 || (tv->v_type == VAR_DICT
3749 && tv->vval.v_dict != NULL
3750 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3751}
3752
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3754/*
3755 * Delete all "menutrans_" variables.
3756 */
3757 void
3758del_menutrans_vars()
3759{
Bram Moolenaar33570922005-01-25 22:26:29 +00003760 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003761 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
Bram Moolenaar33570922005-01-25 22:26:29 +00003763 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003764 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003765 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003766 {
3767 if (!HASHITEM_EMPTY(hi))
3768 {
3769 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3771 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003772 }
3773 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003774 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775}
3776#endif
3777
3778#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3779
3780/*
3781 * Local string buffer for the next two functions to store a variable name
3782 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3783 * get_user_var_name().
3784 */
3785
3786static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3787
3788static char_u *varnamebuf = NULL;
3789static int varnamebuflen = 0;
3790
3791/*
3792 * Function to concatenate a prefix and a variable name.
3793 */
3794 static char_u *
3795cat_prefix_varname(prefix, name)
3796 int prefix;
3797 char_u *name;
3798{
3799 int len;
3800
3801 len = (int)STRLEN(name) + 3;
3802 if (len > varnamebuflen)
3803 {
3804 vim_free(varnamebuf);
3805 len += 10; /* some additional space */
3806 varnamebuf = alloc(len);
3807 if (varnamebuf == NULL)
3808 {
3809 varnamebuflen = 0;
3810 return NULL;
3811 }
3812 varnamebuflen = len;
3813 }
3814 *varnamebuf = prefix;
3815 varnamebuf[1] = ':';
3816 STRCPY(varnamebuf + 2, name);
3817 return varnamebuf;
3818}
3819
3820/*
3821 * Function given to ExpandGeneric() to obtain the list of user defined
3822 * (global/buffer/window/built-in) variable names.
3823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 char_u *
3825get_user_var_name(xp, idx)
3826 expand_T *xp;
3827 int idx;
3828{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003829 static long_u gdone;
3830 static long_u bdone;
3831 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003832#ifdef FEAT_WINDOWS
3833 static long_u tdone;
3834#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003835 static int vidx;
3836 static hashitem_T *hi;
3837 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
3839 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003840 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003841 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003842#ifdef FEAT_WINDOWS
3843 tdone = 0;
3844#endif
3845 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003846
3847 /* Global variables */
3848 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003851 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003852 else
3853 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003854 while (HASHITEM_EMPTY(hi))
3855 ++hi;
3856 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3857 return cat_prefix_varname('g', hi->hi_key);
3858 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003860
3861 /* b: variables */
3862 ht = &curbuf->b_vars.dv_hashtab;
3863 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003865 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003866 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003867 else
3868 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003869 while (HASHITEM_EMPTY(hi))
3870 ++hi;
3871 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003873 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003875 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 return (char_u *)"b:changedtick";
3877 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003878
3879 /* w: variables */
3880 ht = &curwin->w_vars.dv_hashtab;
3881 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003883 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003884 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003885 else
3886 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003887 while (HASHITEM_EMPTY(hi))
3888 ++hi;
3889 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003891
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003892#ifdef FEAT_WINDOWS
3893 /* t: variables */
3894 ht = &curtab->tp_vars.dv_hashtab;
3895 if (tdone < ht->ht_used)
3896 {
3897 if (tdone++ == 0)
3898 hi = ht->ht_array;
3899 else
3900 ++hi;
3901 while (HASHITEM_EMPTY(hi))
3902 ++hi;
3903 return cat_prefix_varname('t', hi->hi_key);
3904 }
3905#endif
3906
Bram Moolenaar33570922005-01-25 22:26:29 +00003907 /* v: variables */
3908 if (vidx < VV_LEN)
3909 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
3911 vim_free(varnamebuf);
3912 varnamebuf = NULL;
3913 varnamebuflen = 0;
3914 return NULL;
3915}
3916
3917#endif /* FEAT_CMDL_COMPL */
3918
3919/*
3920 * types for expressions.
3921 */
3922typedef enum
3923{
3924 TYPE_UNKNOWN = 0
3925 , TYPE_EQUAL /* == */
3926 , TYPE_NEQUAL /* != */
3927 , TYPE_GREATER /* > */
3928 , TYPE_GEQUAL /* >= */
3929 , TYPE_SMALLER /* < */
3930 , TYPE_SEQUAL /* <= */
3931 , TYPE_MATCH /* =~ */
3932 , TYPE_NOMATCH /* !~ */
3933} exptype_T;
3934
3935/*
3936 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003937 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3939 */
3940
3941/*
3942 * Handle zero level expression.
3943 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003944 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003945 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 * Return OK or FAIL.
3947 */
3948 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003949eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 char_u **nextcmd;
3953 int evaluate;
3954{
3955 int ret;
3956 char_u *p;
3957
3958 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003959 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (ret == FAIL || !ends_excmd(*p))
3961 {
3962 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003963 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 /*
3965 * Report the invalid expression unless the expression evaluation has
3966 * been cancelled due to an aborting error, an interrupt, or an
3967 * exception.
3968 */
3969 if (!aborting())
3970 EMSG2(_(e_invexpr2), arg);
3971 ret = FAIL;
3972 }
3973 if (nextcmd != NULL)
3974 *nextcmd = check_nextcmd(p);
3975
3976 return ret;
3977}
3978
3979/*
3980 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003981 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 *
3983 * "arg" must point to the first non-white of the expression.
3984 * "arg" is advanced to the next non-white after the recognized expression.
3985 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003986 * Note: "rettv.v_lock" is not set.
3987 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 * Return OK or FAIL.
3989 */
3990 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003991eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 int evaluate;
3995{
3996 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999 /*
4000 * Get the first variable.
4001 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004002 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 return FAIL;
4004
4005 if ((*arg)[0] == '?')
4006 {
4007 result = FALSE;
4008 if (evaluate)
4009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004010 int error = FALSE;
4011
4012 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004014 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 if (error)
4016 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018
4019 /*
4020 * Get the second variable.
4021 */
4022 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 return FAIL;
4025
4026 /*
4027 * Check for the ":".
4028 */
4029 if ((*arg)[0] != ':')
4030 {
4031 EMSG(_("E109: Missing ':' after '?'"));
4032 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return FAIL;
4035 }
4036
4037 /*
4038 * Get the third variable.
4039 */
4040 *arg = skipwhite(*arg + 1);
4041 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4042 {
4043 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 return FAIL;
4046 }
4047 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050
4051 return OK;
4052}
4053
4054/*
4055 * Handle first level expression:
4056 * expr2 || expr2 || expr2 logical OR
4057 *
4058 * "arg" must point to the first non-white of the expression.
4059 * "arg" is advanced to the next non-white after the recognized expression.
4060 *
4061 * Return OK or FAIL.
4062 */
4063 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 int evaluate;
4068{
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 long result;
4071 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004072 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
4074 /*
4075 * Get the first variable.
4076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return FAIL;
4079
4080 /*
4081 * Repeat until there is no following "||".
4082 */
4083 first = TRUE;
4084 result = FALSE;
4085 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4086 {
4087 if (evaluate && first)
4088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004089 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004092 if (error)
4093 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 first = FALSE;
4095 }
4096
4097 /*
4098 * Get the second variable.
4099 */
4100 *arg = skipwhite(*arg + 2);
4101 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4102 return FAIL;
4103
4104 /*
4105 * Compute the result.
4106 */
4107 if (evaluate && !result)
4108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004112 if (error)
4113 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115 if (evaluate)
4116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 rettv->v_type = VAR_NUMBER;
4118 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120 }
4121
4122 return OK;
4123}
4124
4125/*
4126 * Handle second level expression:
4127 * expr3 && expr3 && expr3 logical AND
4128 *
4129 * "arg" must point to the first non-white of the expression.
4130 * "arg" is advanced to the next non-white after the recognized expression.
4131 *
4132 * Return OK or FAIL.
4133 */
4134 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 int evaluate;
4139{
Bram Moolenaar33570922005-01-25 22:26:29 +00004140 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 long result;
4142 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004143 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 /*
4146 * Get the first variable.
4147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 return FAIL;
4150
4151 /*
4152 * Repeat until there is no following "&&".
4153 */
4154 first = TRUE;
4155 result = TRUE;
4156 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4157 {
4158 if (evaluate && first)
4159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004160 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004162 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004163 if (error)
4164 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 first = FALSE;
4166 }
4167
4168 /*
4169 * Get the second variable.
4170 */
4171 *arg = skipwhite(*arg + 2);
4172 if (eval4(arg, &var2, evaluate && result) == FAIL)
4173 return FAIL;
4174
4175 /*
4176 * Compute the result.
4177 */
4178 if (evaluate && result)
4179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004180 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004183 if (error)
4184 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186 if (evaluate)
4187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 rettv->v_type = VAR_NUMBER;
4189 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 }
4192
4193 return OK;
4194}
4195
4196/*
4197 * Handle third level expression:
4198 * var1 == var2
4199 * var1 =~ var2
4200 * var1 != var2
4201 * var1 !~ var2
4202 * var1 > var2
4203 * var1 >= var2
4204 * var1 < var2
4205 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004206 * var1 is var2
4207 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 *
4209 * "arg" must point to the first non-white of the expression.
4210 * "arg" is advanced to the next non-white after the recognized expression.
4211 *
4212 * Return OK or FAIL.
4213 */
4214 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 int evaluate;
4219{
Bram Moolenaar33570922005-01-25 22:26:29 +00004220 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 char_u *p;
4222 int i;
4223 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004224 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 int len = 2;
4226 long n1, n2;
4227 char_u *s1, *s2;
4228 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4229 regmatch_T regmatch;
4230 int ic;
4231 char_u *save_cpo;
4232
4233 /*
4234 * Get the first variable.
4235 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004236 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 return FAIL;
4238
4239 p = *arg;
4240 switch (p[0])
4241 {
4242 case '=': if (p[1] == '=')
4243 type = TYPE_EQUAL;
4244 else if (p[1] == '~')
4245 type = TYPE_MATCH;
4246 break;
4247 case '!': if (p[1] == '=')
4248 type = TYPE_NEQUAL;
4249 else if (p[1] == '~')
4250 type = TYPE_NOMATCH;
4251 break;
4252 case '>': if (p[1] != '=')
4253 {
4254 type = TYPE_GREATER;
4255 len = 1;
4256 }
4257 else
4258 type = TYPE_GEQUAL;
4259 break;
4260 case '<': if (p[1] != '=')
4261 {
4262 type = TYPE_SMALLER;
4263 len = 1;
4264 }
4265 else
4266 type = TYPE_SEQUAL;
4267 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004268 case 'i': if (p[1] == 's')
4269 {
4270 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4271 len = 5;
4272 if (!vim_isIDc(p[len]))
4273 {
4274 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4275 type_is = TRUE;
4276 }
4277 }
4278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280
4281 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004282 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 */
4284 if (type != TYPE_UNKNOWN)
4285 {
4286 /* extra question mark appended: ignore case */
4287 if (p[len] == '?')
4288 {
4289 ic = TRUE;
4290 ++len;
4291 }
4292 /* extra '#' appended: match case */
4293 else if (p[len] == '#')
4294 {
4295 ic = FALSE;
4296 ++len;
4297 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004298 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 else
4300 ic = p_ic;
4301
4302 /*
4303 * Get the second variable.
4304 */
4305 *arg = skipwhite(p + len);
4306 if (eval5(arg, &var2, evaluate) == FAIL)
4307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 return FAIL;
4310 }
4311
4312 if (evaluate)
4313 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004314 if (type_is && rettv->v_type != var2.v_type)
4315 {
4316 /* For "is" a different type always means FALSE, for "notis"
4317 * it means TRUE. */
4318 n1 = (type == TYPE_NEQUAL);
4319 }
4320 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4321 {
4322 if (type_is)
4323 {
4324 n1 = (rettv->v_type == var2.v_type
4325 && rettv->vval.v_list == var2.vval.v_list);
4326 if (type == TYPE_NEQUAL)
4327 n1 = !n1;
4328 }
4329 else if (rettv->v_type != var2.v_type
4330 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4331 {
4332 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004333 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004334 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004335 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004336 clear_tv(rettv);
4337 clear_tv(&var2);
4338 return FAIL;
4339 }
4340 else
4341 {
4342 /* Compare two Lists for being equal or unequal. */
4343 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4344 if (type == TYPE_NEQUAL)
4345 n1 = !n1;
4346 }
4347 }
4348
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004349 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4350 {
4351 if (type_is)
4352 {
4353 n1 = (rettv->v_type == var2.v_type
4354 && rettv->vval.v_dict == var2.vval.v_dict);
4355 if (type == TYPE_NEQUAL)
4356 n1 = !n1;
4357 }
4358 else if (rettv->v_type != var2.v_type
4359 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4360 {
4361 if (rettv->v_type != var2.v_type)
4362 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4363 else
4364 EMSG(_("E736: Invalid operation for Dictionary"));
4365 clear_tv(rettv);
4366 clear_tv(&var2);
4367 return FAIL;
4368 }
4369 else
4370 {
4371 /* Compare two Dictionaries for being equal or unequal. */
4372 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4373 if (type == TYPE_NEQUAL)
4374 n1 = !n1;
4375 }
4376 }
4377
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004378 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4379 {
4380 if (rettv->v_type != var2.v_type
4381 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4382 {
4383 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004384 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004385 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004386 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004387 clear_tv(rettv);
4388 clear_tv(&var2);
4389 return FAIL;
4390 }
4391 else
4392 {
4393 /* Compare two Funcrefs for being equal or unequal. */
4394 if (rettv->vval.v_string == NULL
4395 || var2.vval.v_string == NULL)
4396 n1 = FALSE;
4397 else
4398 n1 = STRCMP(rettv->vval.v_string,
4399 var2.vval.v_string) == 0;
4400 if (type == TYPE_NEQUAL)
4401 n1 = !n1;
4402 }
4403 }
4404
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004405#ifdef FEAT_FLOAT
4406 /*
4407 * If one of the two variables is a float, compare as a float.
4408 * When using "=~" or "!~", always compare as string.
4409 */
4410 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4411 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4412 {
4413 float_T f1, f2;
4414
4415 if (rettv->v_type == VAR_FLOAT)
4416 f1 = rettv->vval.v_float;
4417 else
4418 f1 = get_tv_number(rettv);
4419 if (var2.v_type == VAR_FLOAT)
4420 f2 = var2.vval.v_float;
4421 else
4422 f2 = get_tv_number(&var2);
4423 n1 = FALSE;
4424 switch (type)
4425 {
4426 case TYPE_EQUAL: n1 = (f1 == f2); break;
4427 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4428 case TYPE_GREATER: n1 = (f1 > f2); break;
4429 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4430 case TYPE_SMALLER: n1 = (f1 < f2); break;
4431 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4432 case TYPE_UNKNOWN:
4433 case TYPE_MATCH:
4434 case TYPE_NOMATCH: break; /* avoid gcc warning */
4435 }
4436 }
4437#endif
4438
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 /*
4440 * If one of the two variables is a number, compare as a number.
4441 * When using "=~" or "!~", always compare as string.
4442 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004443 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004446 n1 = get_tv_number(rettv);
4447 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 switch (type)
4449 {
4450 case TYPE_EQUAL: n1 = (n1 == n2); break;
4451 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4452 case TYPE_GREATER: n1 = (n1 > n2); break;
4453 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4454 case TYPE_SMALLER: n1 = (n1 < n2); break;
4455 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4456 case TYPE_UNKNOWN:
4457 case TYPE_MATCH:
4458 case TYPE_NOMATCH: break; /* avoid gcc warning */
4459 }
4460 }
4461 else
4462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004463 s1 = get_tv_string_buf(rettv, buf1);
4464 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4466 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4467 else
4468 i = 0;
4469 n1 = FALSE;
4470 switch (type)
4471 {
4472 case TYPE_EQUAL: n1 = (i == 0); break;
4473 case TYPE_NEQUAL: n1 = (i != 0); break;
4474 case TYPE_GREATER: n1 = (i > 0); break;
4475 case TYPE_GEQUAL: n1 = (i >= 0); break;
4476 case TYPE_SMALLER: n1 = (i < 0); break;
4477 case TYPE_SEQUAL: n1 = (i <= 0); break;
4478
4479 case TYPE_MATCH:
4480 case TYPE_NOMATCH:
4481 /* avoid 'l' flag in 'cpoptions' */
4482 save_cpo = p_cpo;
4483 p_cpo = (char_u *)"";
4484 regmatch.regprog = vim_regcomp(s2,
4485 RE_MAGIC + RE_STRING);
4486 regmatch.rm_ic = ic;
4487 if (regmatch.regprog != NULL)
4488 {
4489 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4490 vim_free(regmatch.regprog);
4491 if (type == TYPE_NOMATCH)
4492 n1 = !n1;
4493 }
4494 p_cpo = save_cpo;
4495 break;
4496
4497 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4498 }
4499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004500 clear_tv(rettv);
4501 clear_tv(&var2);
4502 rettv->v_type = VAR_NUMBER;
4503 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505 }
4506
4507 return OK;
4508}
4509
4510/*
4511 * Handle fourth level expression:
4512 * + number addition
4513 * - number subtraction
4514 * . string concatenation
4515 *
4516 * "arg" must point to the first non-white of the expression.
4517 * "arg" is advanced to the next non-white after the recognized expression.
4518 *
4519 * Return OK or FAIL.
4520 */
4521 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004522eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 int evaluate;
4526{
Bram Moolenaar33570922005-01-25 22:26:29 +00004527 typval_T var2;
4528 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 int op;
4530 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004531#ifdef FEAT_FLOAT
4532 float_T f1 = 0, f2 = 0;
4533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 char_u *s1, *s2;
4535 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4536 char_u *p;
4537
4538 /*
4539 * Get the first variable.
4540 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004541 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 return FAIL;
4543
4544 /*
4545 * Repeat computing, until no '+', '-' or '.' is following.
4546 */
4547 for (;;)
4548 {
4549 op = **arg;
4550 if (op != '+' && op != '-' && op != '.')
4551 break;
4552
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004553 if ((op != '+' || rettv->v_type != VAR_LIST)
4554#ifdef FEAT_FLOAT
4555 && (op == '.' || rettv->v_type != VAR_FLOAT)
4556#endif
4557 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004558 {
4559 /* For "list + ...", an illegal use of the first operand as
4560 * a number cannot be determined before evaluating the 2nd
4561 * operand: if this is also a list, all is ok.
4562 * For "something . ...", "something - ..." or "non-list + ...",
4563 * we know that the first operand needs to be a string or number
4564 * without evaluating the 2nd operand. So check before to avoid
4565 * side effects after an error. */
4566 if (evaluate && get_tv_string_chk(rettv) == NULL)
4567 {
4568 clear_tv(rettv);
4569 return FAIL;
4570 }
4571 }
4572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 /*
4574 * Get the second variable.
4575 */
4576 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004577 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004579 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 return FAIL;
4581 }
4582
4583 if (evaluate)
4584 {
4585 /*
4586 * Compute the result.
4587 */
4588 if (op == '.')
4589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004590 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4591 s2 = get_tv_string_buf_chk(&var2, buf2);
4592 if (s2 == NULL) /* type error ? */
4593 {
4594 clear_tv(rettv);
4595 clear_tv(&var2);
4596 return FAIL;
4597 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004598 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 clear_tv(rettv);
4600 rettv->v_type = VAR_STRING;
4601 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004603 else if (op == '+' && rettv->v_type == VAR_LIST
4604 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004605 {
4606 /* concatenate Lists */
4607 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4608 &var3) == FAIL)
4609 {
4610 clear_tv(rettv);
4611 clear_tv(&var2);
4612 return FAIL;
4613 }
4614 clear_tv(rettv);
4615 *rettv = var3;
4616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 else
4618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004619 int error = FALSE;
4620
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004621#ifdef FEAT_FLOAT
4622 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004623 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004624 f1 = rettv->vval.v_float;
4625 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004626 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004627 else
4628#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004629 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630 n1 = get_tv_number_chk(rettv, &error);
4631 if (error)
4632 {
4633 /* This can only happen for "list + non-list". For
4634 * "non-list + ..." or "something - ...", we returned
4635 * before evaluating the 2nd operand. */
4636 clear_tv(rettv);
4637 return FAIL;
4638 }
4639#ifdef FEAT_FLOAT
4640 if (var2.v_type == VAR_FLOAT)
4641 f1 = n1;
4642#endif
4643 }
4644#ifdef FEAT_FLOAT
4645 if (var2.v_type == VAR_FLOAT)
4646 {
4647 f2 = var2.vval.v_float;
4648 n2 = 0;
4649 }
4650 else
4651#endif
4652 {
4653 n2 = get_tv_number_chk(&var2, &error);
4654 if (error)
4655 {
4656 clear_tv(rettv);
4657 clear_tv(&var2);
4658 return FAIL;
4659 }
4660#ifdef FEAT_FLOAT
4661 if (rettv->v_type == VAR_FLOAT)
4662 f2 = n2;
4663#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004666
4667#ifdef FEAT_FLOAT
4668 /* If there is a float on either side the result is a float. */
4669 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4670 {
4671 if (op == '+')
4672 f1 = f1 + f2;
4673 else
4674 f1 = f1 - f2;
4675 rettv->v_type = VAR_FLOAT;
4676 rettv->vval.v_float = f1;
4677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004679#endif
4680 {
4681 if (op == '+')
4682 n1 = n1 + n2;
4683 else
4684 n1 = n1 - n2;
4685 rettv->v_type = VAR_NUMBER;
4686 rettv->vval.v_number = n1;
4687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004689 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 }
4691 }
4692 return OK;
4693}
4694
4695/*
4696 * Handle fifth level expression:
4697 * * number multiplication
4698 * / number division
4699 * % number modulo
4700 *
4701 * "arg" must point to the first non-white of the expression.
4702 * "arg" is advanced to the next non-white after the recognized expression.
4703 *
4704 * Return OK or FAIL.
4705 */
4706 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004707eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004711 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
Bram Moolenaar33570922005-01-25 22:26:29 +00004713 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 int op;
4715 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716#ifdef FEAT_FLOAT
4717 int use_float = FALSE;
4718 float_T f1 = 0, f2;
4719#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721
4722 /*
4723 * Get the first variable.
4724 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004725 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 return FAIL;
4727
4728 /*
4729 * Repeat computing, until no '*', '/' or '%' is following.
4730 */
4731 for (;;)
4732 {
4733 op = **arg;
4734 if (op != '*' && op != '/' && op != '%')
4735 break;
4736
4737 if (evaluate)
4738 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004739#ifdef FEAT_FLOAT
4740 if (rettv->v_type == VAR_FLOAT)
4741 {
4742 f1 = rettv->vval.v_float;
4743 use_float = TRUE;
4744 n1 = 0;
4745 }
4746 else
4747#endif
4748 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004749 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004750 if (error)
4751 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
4753 else
4754 n1 = 0;
4755
4756 /*
4757 * Get the second variable.
4758 */
4759 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004760 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 return FAIL;
4762
4763 if (evaluate)
4764 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004765#ifdef FEAT_FLOAT
4766 if (var2.v_type == VAR_FLOAT)
4767 {
4768 if (!use_float)
4769 {
4770 f1 = n1;
4771 use_float = TRUE;
4772 }
4773 f2 = var2.vval.v_float;
4774 n2 = 0;
4775 }
4776 else
4777#endif
4778 {
4779 n2 = get_tv_number_chk(&var2, &error);
4780 clear_tv(&var2);
4781 if (error)
4782 return FAIL;
4783#ifdef FEAT_FLOAT
4784 if (use_float)
4785 f2 = n2;
4786#endif
4787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788
4789 /*
4790 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793#ifdef FEAT_FLOAT
4794 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796 if (op == '*')
4797 f1 = f1 * f2;
4798 else if (op == '/')
4799 {
4800 /* We rely on the floating point library to handle divide
4801 * by zero to result in "inf" and not a crash. */
4802 f1 = f1 / f2;
4803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004806 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004807 return FAIL;
4808 }
4809 rettv->v_type = VAR_FLOAT;
4810 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 }
4812 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004815 if (op == '*')
4816 n1 = n1 * n2;
4817 else if (op == '/')
4818 {
4819 if (n2 == 0) /* give an error message? */
4820 {
4821 if (n1 == 0)
4822 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4823 else if (n1 < 0)
4824 n1 = -0x7fffffffL;
4825 else
4826 n1 = 0x7fffffffL;
4827 }
4828 else
4829 n1 = n1 / n2;
4830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004832 {
4833 if (n2 == 0) /* give an error message? */
4834 n1 = 0;
4835 else
4836 n1 = n1 % n2;
4837 }
4838 rettv->v_type = VAR_NUMBER;
4839 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842 }
4843
4844 return OK;
4845}
4846
4847/*
4848 * Handle sixth level expression:
4849 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004850 * "string" string constant
4851 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 * &option-name option value
4853 * @r register contents
4854 * identifier variable value
4855 * function() function call
4856 * $VAR environment variable
4857 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004858 * [expr, expr] List
4859 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 *
4861 * Also handle:
4862 * ! in front logical NOT
4863 * - in front unary minus
4864 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004865 * trailing [] subscript in String or List
4866 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 *
4868 * "arg" must point to the first non-white of the expression.
4869 * "arg" is advanced to the next non-white after the recognized expression.
4870 *
4871 * Return OK or FAIL.
4872 */
4873 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004874eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004878 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 long n;
4881 int len;
4882 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 char_u *start_leader, *end_leader;
4884 int ret = OK;
4885 char_u *alias;
4886
4887 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004888 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004889 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892
4893 /*
4894 * Skip '!' and '-' characters. They are handled later.
4895 */
4896 start_leader = *arg;
4897 while (**arg == '!' || **arg == '-' || **arg == '+')
4898 *arg = skipwhite(*arg + 1);
4899 end_leader = *arg;
4900
4901 switch (**arg)
4902 {
4903 /*
4904 * Number constant.
4905 */
4906 case '0':
4907 case '1':
4908 case '2':
4909 case '3':
4910 case '4':
4911 case '5':
4912 case '6':
4913 case '7':
4914 case '8':
4915 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916 {
4917#ifdef FEAT_FLOAT
4918 char_u *p = skipdigits(*arg + 1);
4919 int get_float = FALSE;
4920
4921 /* We accept a float when the format matches
4922 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004923 * strict to avoid backwards compatibility problems.
4924 * Don't look for a float after the "." operator, so that
4925 * ":let vers = 1.2.3" doesn't fail. */
4926 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928 get_float = TRUE;
4929 p = skipdigits(p + 2);
4930 if (*p == 'e' || *p == 'E')
4931 {
4932 ++p;
4933 if (*p == '-' || *p == '+')
4934 ++p;
4935 if (!vim_isdigit(*p))
4936 get_float = FALSE;
4937 else
4938 p = skipdigits(p + 1);
4939 }
4940 if (ASCII_ISALPHA(*p) || *p == '.')
4941 get_float = FALSE;
4942 }
4943 if (get_float)
4944 {
4945 float_T f;
4946
4947 *arg += string2float(*arg, &f);
4948 if (evaluate)
4949 {
4950 rettv->v_type = VAR_FLOAT;
4951 rettv->vval.v_float = f;
4952 }
4953 }
4954 else
4955#endif
4956 {
4957 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4958 *arg += len;
4959 if (evaluate)
4960 {
4961 rettv->v_type = VAR_NUMBER;
4962 rettv->vval.v_number = n;
4963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967
4968 /*
4969 * String constant: "string".
4970 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004971 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 break;
4973
4974 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004977 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004978 break;
4979
4980 /*
4981 * List: [expr, expr]
4982 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 break;
4985
4986 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004987 * Dictionary: {key: val, key: val}
4988 */
4989 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4990 break;
4991
4992 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004993 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004995 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 break;
4997
4998 /*
4999 * Environment variable: $VAR.
5000 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005001 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 break;
5003
5004 /*
5005 * Register contents: @r.
5006 */
5007 case '@': ++*arg;
5008 if (evaluate)
5009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005010 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005011 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013 if (**arg != NUL)
5014 ++*arg;
5015 break;
5016
5017 /*
5018 * nested expression: (expression).
5019 */
5020 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005021 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 if (**arg == ')')
5023 ++*arg;
5024 else if (ret == OK)
5025 {
5026 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005027 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 ret = FAIL;
5029 }
5030 break;
5031
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 break;
5034 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035
5036 if (ret == NOTDONE)
5037 {
5038 /*
5039 * Must be a variable or function name.
5040 * Can also be a curly-braces kind of name: {expr}.
5041 */
5042 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005043 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 if (alias != NULL)
5045 s = alias;
5046
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005047 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 ret = FAIL;
5049 else
5050 {
5051 if (**arg == '(') /* recursive! */
5052 {
5053 /* If "s" is the name of a variable of type VAR_FUNC
5054 * use its contents. */
5055 s = deref_func_name(s, &len);
5056
5057 /* Invoke the function. */
5058 ret = get_func_tv(s, len, rettv, arg,
5059 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005060 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 /* Stop the expression evaluation when immediately
5062 * aborting on error, or when an interrupt occurred or
5063 * an exception was thrown but not caught. */
5064 if (aborting())
5065 {
5066 if (ret == OK)
5067 clear_tv(rettv);
5068 ret = FAIL;
5069 }
5070 }
5071 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005072 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005073 else
5074 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 }
5076
5077 if (alias != NULL)
5078 vim_free(alias);
5079 }
5080
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 *arg = skipwhite(*arg);
5082
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005083 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5084 * expr(expr). */
5085 if (ret == OK)
5086 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
5088 /*
5089 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5090 */
5091 if (ret == OK && evaluate && end_leader > start_leader)
5092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005093 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005094 int val = 0;
5095#ifdef FEAT_FLOAT
5096 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005097
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005098 if (rettv->v_type == VAR_FLOAT)
5099 f = rettv->vval.v_float;
5100 else
5101#endif
5102 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005103 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005105 clear_tv(rettv);
5106 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005108 else
5109 {
5110 while (end_leader > start_leader)
5111 {
5112 --end_leader;
5113 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005114 {
5115#ifdef FEAT_FLOAT
5116 if (rettv->v_type == VAR_FLOAT)
5117 f = !f;
5118 else
5119#endif
5120 val = !val;
5121 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005122 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005123 {
5124#ifdef FEAT_FLOAT
5125 if (rettv->v_type == VAR_FLOAT)
5126 f = -f;
5127 else
5128#endif
5129 val = -val;
5130 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005131 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005132#ifdef FEAT_FLOAT
5133 if (rettv->v_type == VAR_FLOAT)
5134 {
5135 clear_tv(rettv);
5136 rettv->vval.v_float = f;
5137 }
5138 else
5139#endif
5140 {
5141 clear_tv(rettv);
5142 rettv->v_type = VAR_NUMBER;
5143 rettv->vval.v_number = val;
5144 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147
5148 return ret;
5149}
5150
5151/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005152 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5153 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5155 */
5156 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005157eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005159 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005160 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005161 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162{
5163 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005164 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 long len = -1;
5167 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005168 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005170
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005171 if (rettv->v_type == VAR_FUNC
5172#ifdef FEAT_FLOAT
5173 || rettv->v_type == VAR_FLOAT
5174#endif
5175 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005177 if (verbose)
5178 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 return FAIL;
5180 }
5181
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 /*
5185 * dict.name
5186 */
5187 key = *arg + 1;
5188 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5189 ;
5190 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005191 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193 }
5194 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 /*
5197 * something[idx]
5198 *
5199 * Get the (first) variable from inside the [].
5200 */
5201 *arg = skipwhite(*arg + 1);
5202 if (**arg == ':')
5203 empty1 = TRUE;
5204 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5205 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5207 {
5208 /* not a number or string */
5209 clear_tv(&var1);
5210 return FAIL;
5211 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005212
5213 /*
5214 * Get the second variable from inside the [:].
5215 */
5216 if (**arg == ':')
5217 {
5218 range = TRUE;
5219 *arg = skipwhite(*arg + 1);
5220 if (**arg == ']')
5221 empty2 = TRUE;
5222 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005224 if (!empty1)
5225 clear_tv(&var1);
5226 return FAIL;
5227 }
5228 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5229 {
5230 /* not a number or string */
5231 if (!empty1)
5232 clear_tv(&var1);
5233 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 return FAIL;
5235 }
5236 }
5237
5238 /* Check for the ']'. */
5239 if (**arg != ']')
5240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005241 if (verbose)
5242 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243 clear_tv(&var1);
5244 if (range)
5245 clear_tv(&var2);
5246 return FAIL;
5247 }
5248 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249 }
5250
5251 if (evaluate)
5252 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 n1 = 0;
5254 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005256 n1 = get_tv_number(&var1);
5257 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 }
5259 if (range)
5260 {
5261 if (empty2)
5262 n2 = -1;
5263 else
5264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005265 n2 = get_tv_number(&var2);
5266 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 }
5268 }
5269
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005270 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 {
5272 case VAR_NUMBER:
5273 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 len = (long)STRLEN(s);
5276 if (range)
5277 {
5278 /* The resulting variable is a substring. If the indexes
5279 * are out of range the result is empty. */
5280 if (n1 < 0)
5281 {
5282 n1 = len + n1;
5283 if (n1 < 0)
5284 n1 = 0;
5285 }
5286 if (n2 < 0)
5287 n2 = len + n2;
5288 else if (n2 >= len)
5289 n2 = len;
5290 if (n1 >= len || n2 < 0 || n1 > n2)
5291 s = NULL;
5292 else
5293 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5294 }
5295 else
5296 {
5297 /* The resulting variable is a string of a single
5298 * character. If the index is too big or negative the
5299 * result is empty. */
5300 if (n1 >= len || n1 < 0)
5301 s = NULL;
5302 else
5303 s = vim_strnsave(s + n1, 1);
5304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305 clear_tv(rettv);
5306 rettv->v_type = VAR_STRING;
5307 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 break;
5309
5310 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005311 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 if (n1 < 0)
5313 n1 = len + n1;
5314 if (!empty1 && (n1 < 0 || n1 >= len))
5315 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005316 /* For a range we allow invalid values and return an empty
5317 * list. A list index out of range is an error. */
5318 if (!range)
5319 {
5320 if (verbose)
5321 EMSGN(_(e_listidx), n1);
5322 return FAIL;
5323 }
5324 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 }
5326 if (range)
5327 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005328 list_T *l;
5329 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330
5331 if (n2 < 0)
5332 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005333 else if (n2 >= len)
5334 n2 = len - 1;
5335 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005336 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 l = list_alloc();
5338 if (l == NULL)
5339 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005340 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 n1 <= n2; ++n1)
5342 {
5343 if (list_append_tv(l, &item->li_tv) == FAIL)
5344 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005345 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 return FAIL;
5347 }
5348 item = item->li_next;
5349 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005350 clear_tv(rettv);
5351 rettv->v_type = VAR_LIST;
5352 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005353 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 }
5355 else
5356 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005357 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005358 clear_tv(rettv);
5359 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 }
5361 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005362
5363 case VAR_DICT:
5364 if (range)
5365 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005366 if (verbose)
5367 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005368 if (len == -1)
5369 clear_tv(&var1);
5370 return FAIL;
5371 }
5372 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005373 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005374
5375 if (len == -1)
5376 {
5377 key = get_tv_string(&var1);
5378 if (*key == NUL)
5379 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005380 if (verbose)
5381 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 clear_tv(&var1);
5383 return FAIL;
5384 }
5385 }
5386
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005387 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005388
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005389 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005390 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 if (len == -1)
5392 clear_tv(&var1);
5393 if (item == NULL)
5394 return FAIL;
5395
5396 copy_tv(&item->di_tv, &var1);
5397 clear_tv(rettv);
5398 *rettv = var1;
5399 }
5400 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 }
5402 }
5403
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 return OK;
5405}
5406
5407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 * Get an option value.
5409 * "arg" points to the '&' or '+' before the option name.
5410 * "arg" is advanced to character after the option name.
5411 * Return OK or FAIL.
5412 */
5413 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005414get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005416 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 int evaluate;
5418{
5419 char_u *option_end;
5420 long numval;
5421 char_u *stringval;
5422 int opt_type;
5423 int c;
5424 int working = (**arg == '+'); /* has("+option") */
5425 int ret = OK;
5426 int opt_flags;
5427
5428 /*
5429 * Isolate the option name and find its value.
5430 */
5431 option_end = find_option_end(arg, &opt_flags);
5432 if (option_end == NULL)
5433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005434 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 EMSG2(_("E112: Option name missing: %s"), *arg);
5436 return FAIL;
5437 }
5438
5439 if (!evaluate)
5440 {
5441 *arg = option_end;
5442 return OK;
5443 }
5444
5445 c = *option_end;
5446 *option_end = NUL;
5447 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450 if (opt_type == -3) /* invalid name */
5451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005452 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 EMSG2(_("E113: Unknown option: %s"), *arg);
5454 ret = FAIL;
5455 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005456 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
5458 if (opt_type == -2) /* hidden string option */
5459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 rettv->v_type = VAR_STRING;
5461 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
5463 else if (opt_type == -1) /* hidden number option */
5464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 rettv->v_type = VAR_NUMBER;
5466 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468 else if (opt_type == 1) /* number option */
5469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 rettv->v_type = VAR_NUMBER;
5471 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
5473 else /* string option */
5474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 rettv->v_type = VAR_STRING;
5476 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
5478 }
5479 else if (working && (opt_type == -2 || opt_type == -1))
5480 ret = FAIL;
5481
5482 *option_end = c; /* put back for error messages */
5483 *arg = option_end;
5484
5485 return ret;
5486}
5487
5488/*
5489 * Allocate a variable for a string constant.
5490 * Return OK or FAIL.
5491 */
5492 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 int evaluate;
5497{
5498 char_u *p;
5499 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 int extra = 0;
5501
5502 /*
5503 * Find the end of the string, skipping backslashed characters.
5504 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005505 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
5507 if (*p == '\\' && p[1] != NUL)
5508 {
5509 ++p;
5510 /* A "\<x>" form occupies at least 4 characters, and produces up
5511 * to 6 characters: reserve space for 2 extra */
5512 if (*p == '<')
5513 extra += 2;
5514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 }
5516
5517 if (*p != '"')
5518 {
5519 EMSG2(_("E114: Missing quote: %s"), *arg);
5520 return FAIL;
5521 }
5522
5523 /* If only parsing, set *arg and return here */
5524 if (!evaluate)
5525 {
5526 *arg = p + 1;
5527 return OK;
5528 }
5529
5530 /*
5531 * Copy the string into allocated memory, handling backslashed
5532 * characters.
5533 */
5534 name = alloc((unsigned)(p - *arg + extra));
5535 if (name == NULL)
5536 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005537 rettv->v_type = VAR_STRING;
5538 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
Bram Moolenaar8c711452005-01-14 21:53:12 +00005540 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
5542 if (*p == '\\')
5543 {
5544 switch (*++p)
5545 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005546 case 'b': *name++ = BS; ++p; break;
5547 case 'e': *name++ = ESC; ++p; break;
5548 case 'f': *name++ = FF; ++p; break;
5549 case 'n': *name++ = NL; ++p; break;
5550 case 'r': *name++ = CAR; ++p; break;
5551 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
5553 case 'X': /* hex: "\x1", "\x12" */
5554 case 'x':
5555 case 'u': /* Unicode: "\u0023" */
5556 case 'U':
5557 if (vim_isxdigit(p[1]))
5558 {
5559 int n, nr;
5560 int c = toupper(*p);
5561
5562 if (c == 'X')
5563 n = 2;
5564 else
5565 n = 4;
5566 nr = 0;
5567 while (--n >= 0 && vim_isxdigit(p[1]))
5568 {
5569 ++p;
5570 nr = (nr << 4) + hex2nr(*p);
5571 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005572 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573#ifdef FEAT_MBYTE
5574 /* For "\u" store the number according to
5575 * 'encoding'. */
5576 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 else
5579#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 break;
5583
5584 /* octal: "\1", "\12", "\123" */
5585 case '0':
5586 case '1':
5587 case '2':
5588 case '3':
5589 case '4':
5590 case '5':
5591 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 case '7': *name = *p++ - '0';
5593 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 *name = (*name << 3) + *p++ - '0';
5596 if (*p >= '0' && *p <= '7')
5597 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 break;
5601
5602 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 if (extra != 0)
5605 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 break;
5608 }
5609 /* FALLTHROUGH */
5610
Bram Moolenaar8c711452005-01-14 21:53:12 +00005611 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 break;
5613 }
5614 }
5615 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 *arg = p + 1;
5621
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 return OK;
5623}
5624
5625/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 * Return OK or FAIL.
5628 */
5629 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 int evaluate;
5634{
5635 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005636 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005637 int reduce = 0;
5638
5639 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005641 */
5642 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5643 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005645 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005647 break;
5648 ++reduce;
5649 ++p;
5650 }
5651 }
5652
Bram Moolenaar8c711452005-01-14 21:53:12 +00005653 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005654 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005655 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005656 return FAIL;
5657 }
5658
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005660 if (!evaluate)
5661 {
5662 *arg = p + 1;
5663 return OK;
5664 }
5665
5666 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005668 */
5669 str = alloc((unsigned)((p - *arg) - reduce));
5670 if (str == NULL)
5671 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 rettv->v_type = VAR_STRING;
5673 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005674
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005680 break;
5681 ++p;
5682 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005686 *arg = p + 1;
5687
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005688 return OK;
5689}
5690
5691/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 * Allocate a variable for a List and fill it from "*arg".
5693 * Return OK or FAIL.
5694 */
5695 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005696get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005697 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005698 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005699 int evaluate;
5700{
Bram Moolenaar33570922005-01-25 22:26:29 +00005701 list_T *l = NULL;
5702 typval_T tv;
5703 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704
5705 if (evaluate)
5706 {
5707 l = list_alloc();
5708 if (l == NULL)
5709 return FAIL;
5710 }
5711
5712 *arg = skipwhite(*arg + 1);
5713 while (**arg != ']' && **arg != NUL)
5714 {
5715 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5716 goto failret;
5717 if (evaluate)
5718 {
5719 item = listitem_alloc();
5720 if (item != NULL)
5721 {
5722 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005723 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724 list_append(l, item);
5725 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005726 else
5727 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728 }
5729
5730 if (**arg == ']')
5731 break;
5732 if (**arg != ',')
5733 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005735 goto failret;
5736 }
5737 *arg = skipwhite(*arg + 1);
5738 }
5739
5740 if (**arg != ']')
5741 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005743failret:
5744 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005745 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005746 return FAIL;
5747 }
5748
5749 *arg = skipwhite(*arg + 1);
5750 if (evaluate)
5751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005752 rettv->v_type = VAR_LIST;
5753 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754 ++l->lv_refcount;
5755 }
5756
5757 return OK;
5758}
5759
5760/*
5761 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005762 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005764 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005765list_alloc()
5766{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005767 list_T *l;
5768
5769 l = (list_T *)alloc_clear(sizeof(list_T));
5770 if (l != NULL)
5771 {
5772 /* Prepend the list to the list of lists for garbage collection. */
5773 if (first_list != NULL)
5774 first_list->lv_used_prev = l;
5775 l->lv_used_prev = NULL;
5776 l->lv_used_next = first_list;
5777 first_list = l;
5778 }
5779 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005780}
5781
5782/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005783 * Allocate an empty list for a return value.
5784 * Returns OK or FAIL.
5785 */
5786 static int
5787rettv_list_alloc(rettv)
5788 typval_T *rettv;
5789{
5790 list_T *l = list_alloc();
5791
5792 if (l == NULL)
5793 return FAIL;
5794
5795 rettv->vval.v_list = l;
5796 rettv->v_type = VAR_LIST;
5797 ++l->lv_refcount;
5798 return OK;
5799}
5800
5801/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802 * Unreference a list: decrement the reference count and free it when it
5803 * becomes zero.
5804 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005805 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005807 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005809 if (l != NULL && --l->lv_refcount <= 0)
5810 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811}
5812
5813/*
5814 * Free a list, including all items it points to.
5815 * Ignores the reference count.
5816 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005817 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005818list_free(l, recurse)
5819 list_T *l;
5820 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821{
Bram Moolenaar33570922005-01-25 22:26:29 +00005822 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005824 /* Remove the list from the list of lists for garbage collection. */
5825 if (l->lv_used_prev == NULL)
5826 first_list = l->lv_used_next;
5827 else
5828 l->lv_used_prev->lv_used_next = l->lv_used_next;
5829 if (l->lv_used_next != NULL)
5830 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5831
Bram Moolenaard9fba312005-06-26 22:34:35 +00005832 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005834 /* Remove the item before deleting it. */
5835 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005836 if (recurse || (item->li_tv.v_type != VAR_LIST
5837 && item->li_tv.v_type != VAR_DICT))
5838 clear_tv(&item->li_tv);
5839 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840 }
5841 vim_free(l);
5842}
5843
5844/*
5845 * Allocate a list item.
5846 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005847 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848listitem_alloc()
5849{
Bram Moolenaar33570922005-01-25 22:26:29 +00005850 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851}
5852
5853/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005854 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855 */
5856 static void
5857listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005858 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005860 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 vim_free(item);
5862}
5863
5864/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005865 * Remove a list item from a List and free it. Also clears the value.
5866 */
5867 static void
5868listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005869 list_T *l;
5870 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005871{
5872 list_remove(l, item, item);
5873 listitem_free(item);
5874}
5875
5876/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877 * Get the number of items in a list.
5878 */
5879 static long
5880list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005881 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 if (l == NULL)
5884 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005885 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886}
5887
5888/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005889 * Return TRUE when two lists have exactly the same values.
5890 */
5891 static int
5892list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 list_T *l1;
5894 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005895 int ic; /* ignore case for strings */
5896{
Bram Moolenaar33570922005-01-25 22:26:29 +00005897 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005898
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005899 if (l1 == NULL || l2 == NULL)
5900 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005901 if (l1 == l2)
5902 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005903 if (list_len(l1) != list_len(l2))
5904 return FALSE;
5905
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005906 for (item1 = l1->lv_first, item2 = l2->lv_first;
5907 item1 != NULL && item2 != NULL;
5908 item1 = item1->li_next, item2 = item2->li_next)
5909 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5910 return FALSE;
5911 return item1 == NULL && item2 == NULL;
5912}
5913
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005914#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5915 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005916/*
5917 * Return the dictitem that an entry in a hashtable points to.
5918 */
5919 dictitem_T *
5920dict_lookup(hi)
5921 hashitem_T *hi;
5922{
5923 return HI2DI(hi);
5924}
5925#endif
5926
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005927/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005928 * Return TRUE when two dictionaries have exactly the same key/values.
5929 */
5930 static int
5931dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 dict_T *d1;
5933 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005934 int ic; /* ignore case for strings */
5935{
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 hashitem_T *hi;
5937 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005938 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005939
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005940 if (d1 == NULL || d2 == NULL)
5941 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005942 if (d1 == d2)
5943 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005944 if (dict_len(d1) != dict_len(d2))
5945 return FALSE;
5946
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005947 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005949 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005950 if (!HASHITEM_EMPTY(hi))
5951 {
5952 item2 = dict_find(d2, hi->hi_key, -1);
5953 if (item2 == NULL)
5954 return FALSE;
5955 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5956 return FALSE;
5957 --todo;
5958 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005959 }
5960 return TRUE;
5961}
5962
5963/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005964 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005965 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005966 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005967 */
5968 static int
5969tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005970 typval_T *tv1;
5971 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005972 int ic; /* ignore case */
5973{
5974 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005975 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005976 static int recursive = 0; /* cach recursive loops */
5977 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005978
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005979 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005980 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005981 /* Catch lists and dicts that have an endless loop by limiting
5982 * recursiveness to 1000. We guess they are equal then. */
5983 if (recursive >= 1000)
5984 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005985
5986 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005987 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005988 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005989 ++recursive;
5990 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5991 --recursive;
5992 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005993
5994 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005995 ++recursive;
5996 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5997 --recursive;
5998 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005999
6000 case VAR_FUNC:
6001 return (tv1->vval.v_string != NULL
6002 && tv2->vval.v_string != NULL
6003 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6004
6005 case VAR_NUMBER:
6006 return tv1->vval.v_number == tv2->vval.v_number;
6007
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006008#ifdef FEAT_FLOAT
6009 case VAR_FLOAT:
6010 return tv1->vval.v_float == tv2->vval.v_float;
6011#endif
6012
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006013 case VAR_STRING:
6014 s1 = get_tv_string_buf(tv1, buf1);
6015 s2 = get_tv_string_buf(tv2, buf2);
6016 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006017 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006018
6019 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006020 return TRUE;
6021}
6022
6023/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 * Locate item with index "n" in list "l" and return it.
6025 * A negative index is counted from the end; -1 is the last item.
6026 * Returns NULL when "n" is out of range.
6027 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006030 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031 long n;
6032{
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 long idx;
6035
6036 if (l == NULL)
6037 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006038
6039 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006041 n = l->lv_len + n;
6042
6043 /* Check for index out of range. */
6044 if (n < 0 || n >= l->lv_len)
6045 return NULL;
6046
6047 /* When there is a cached index may start search from there. */
6048 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006050 if (n < l->lv_idx / 2)
6051 {
6052 /* closest to the start of the list */
6053 item = l->lv_first;
6054 idx = 0;
6055 }
6056 else if (n > (l->lv_idx + l->lv_len) / 2)
6057 {
6058 /* closest to the end of the list */
6059 item = l->lv_last;
6060 idx = l->lv_len - 1;
6061 }
6062 else
6063 {
6064 /* closest to the cached index */
6065 item = l->lv_idx_item;
6066 idx = l->lv_idx;
6067 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 }
6069 else
6070 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006071 if (n < l->lv_len / 2)
6072 {
6073 /* closest to the start of the list */
6074 item = l->lv_first;
6075 idx = 0;
6076 }
6077 else
6078 {
6079 /* closest to the end of the list */
6080 item = l->lv_last;
6081 idx = l->lv_len - 1;
6082 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006084
6085 while (n > idx)
6086 {
6087 /* search forward */
6088 item = item->li_next;
6089 ++idx;
6090 }
6091 while (n < idx)
6092 {
6093 /* search backward */
6094 item = item->li_prev;
6095 --idx;
6096 }
6097
6098 /* cache the used index */
6099 l->lv_idx = idx;
6100 l->lv_idx_item = item;
6101
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102 return item;
6103}
6104
6105/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006106 * Get list item "l[idx]" as a number.
6107 */
6108 static long
6109list_find_nr(l, idx, errorp)
6110 list_T *l;
6111 long idx;
6112 int *errorp; /* set to TRUE when something wrong */
6113{
6114 listitem_T *li;
6115
6116 li = list_find(l, idx);
6117 if (li == NULL)
6118 {
6119 if (errorp != NULL)
6120 *errorp = TRUE;
6121 return -1L;
6122 }
6123 return get_tv_number_chk(&li->li_tv, errorp);
6124}
6125
6126/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006127 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6128 */
6129 char_u *
6130list_find_str(l, idx)
6131 list_T *l;
6132 long idx;
6133{
6134 listitem_T *li;
6135
6136 li = list_find(l, idx - 1);
6137 if (li == NULL)
6138 {
6139 EMSGN(_(e_listidx), idx);
6140 return NULL;
6141 }
6142 return get_tv_string(&li->li_tv);
6143}
6144
6145/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006146 * Locate "item" list "l" and return its index.
6147 * Returns -1 when "item" is not in the list.
6148 */
6149 static long
6150list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006151 list_T *l;
6152 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006153{
6154 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006156
6157 if (l == NULL)
6158 return -1;
6159 idx = 0;
6160 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6161 ++idx;
6162 if (li == NULL)
6163 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006164 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006165}
6166
6167/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 * Append item "item" to the end of list "l".
6169 */
6170 static void
6171list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 list_T *l;
6173 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174{
6175 if (l->lv_last == NULL)
6176 {
6177 /* empty list */
6178 l->lv_first = item;
6179 l->lv_last = item;
6180 item->li_prev = NULL;
6181 }
6182 else
6183 {
6184 l->lv_last->li_next = item;
6185 item->li_prev = l->lv_last;
6186 l->lv_last = item;
6187 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006188 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189 item->li_next = NULL;
6190}
6191
6192/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006194 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006196 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006198 list_T *l;
6199 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006201 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202
Bram Moolenaar05159a02005-02-26 23:04:13 +00006203 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006204 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006205 copy_tv(tv, &li->li_tv);
6206 list_append(l, li);
6207 return OK;
6208}
6209
6210/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006211 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006212 * Return FAIL when out of memory.
6213 */
6214 int
6215list_append_dict(list, dict)
6216 list_T *list;
6217 dict_T *dict;
6218{
6219 listitem_T *li = listitem_alloc();
6220
6221 if (li == NULL)
6222 return FAIL;
6223 li->li_tv.v_type = VAR_DICT;
6224 li->li_tv.v_lock = 0;
6225 li->li_tv.vval.v_dict = dict;
6226 list_append(list, li);
6227 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228 return OK;
6229}
6230
6231/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006232 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006233 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006234 * Returns FAIL when out of memory.
6235 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006236 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006237list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006238 list_T *l;
6239 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006240 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006241{
6242 listitem_T *li = listitem_alloc();
6243
6244 if (li == NULL)
6245 return FAIL;
6246 list_append(l, li);
6247 li->li_tv.v_type = VAR_STRING;
6248 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006249 if (str == NULL)
6250 li->li_tv.vval.v_string = NULL;
6251 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006252 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006253 return FAIL;
6254 return OK;
6255}
6256
6257/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006258 * Append "n" to list "l".
6259 * Returns FAIL when out of memory.
6260 */
6261 static int
6262list_append_number(l, n)
6263 list_T *l;
6264 varnumber_T n;
6265{
6266 listitem_T *li;
6267
6268 li = listitem_alloc();
6269 if (li == NULL)
6270 return FAIL;
6271 li->li_tv.v_type = VAR_NUMBER;
6272 li->li_tv.v_lock = 0;
6273 li->li_tv.vval.v_number = n;
6274 list_append(l, li);
6275 return OK;
6276}
6277
6278/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006279 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006280 * If "item" is NULL append at the end.
6281 * Return FAIL when out of memory.
6282 */
6283 static int
6284list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 list_T *l;
6286 typval_T *tv;
6287 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006288{
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006290
6291 if (ni == NULL)
6292 return FAIL;
6293 copy_tv(tv, &ni->li_tv);
6294 if (item == NULL)
6295 /* Append new item at end of list. */
6296 list_append(l, ni);
6297 else
6298 {
6299 /* Insert new item before existing item. */
6300 ni->li_prev = item->li_prev;
6301 ni->li_next = item;
6302 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006303 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006304 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006305 ++l->lv_idx;
6306 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006307 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006308 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310 l->lv_idx_item = NULL;
6311 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006312 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006314 }
6315 return OK;
6316}
6317
6318/*
6319 * Extend "l1" with "l2".
6320 * If "bef" is NULL append at the end, otherwise insert before this item.
6321 * Returns FAIL when out of memory.
6322 */
6323 static int
6324list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 list_T *l1;
6326 list_T *l2;
6327 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328{
Bram Moolenaar33570922005-01-25 22:26:29 +00006329 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006330 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006331
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006332 /* We also quit the loop when we have inserted the original item count of
6333 * the list, avoid a hang when we extend a list with itself. */
6334 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006335 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6336 return FAIL;
6337 return OK;
6338}
6339
6340/*
6341 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6342 * Return FAIL when out of memory.
6343 */
6344 static int
6345list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006346 list_T *l1;
6347 list_T *l2;
6348 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006349{
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006352 if (l1 == NULL || l2 == NULL)
6353 return FAIL;
6354
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006355 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006356 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006357 if (l == NULL)
6358 return FAIL;
6359 tv->v_type = VAR_LIST;
6360 tv->vval.v_list = l;
6361
6362 /* append all items from the second list */
6363 return list_extend(l, l2, NULL);
6364}
6365
6366/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006367 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006369 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006370 * Returns NULL when out of memory.
6371 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006373list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006376 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377{
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 list_T *copy;
6379 listitem_T *item;
6380 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381
6382 if (orig == NULL)
6383 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384
6385 copy = list_alloc();
6386 if (copy != NULL)
6387 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006388 if (copyID != 0)
6389 {
6390 /* Do this before adding the items, because one of the items may
6391 * refer back to this list. */
6392 orig->lv_copyID = copyID;
6393 orig->lv_copylist = copy;
6394 }
6395 for (item = orig->lv_first; item != NULL && !got_int;
6396 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 {
6398 ni = listitem_alloc();
6399 if (ni == NULL)
6400 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006401 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006402 {
6403 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6404 {
6405 vim_free(ni);
6406 break;
6407 }
6408 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006410 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 list_append(copy, ni);
6412 }
6413 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006414 if (item != NULL)
6415 {
6416 list_unref(copy);
6417 copy = NULL;
6418 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419 }
6420
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421 return copy;
6422}
6423
6424/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006426 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006429list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 list_T *l;
6431 listitem_T *item;
6432 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433{
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 /* notify watchers */
6437 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006439 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006440 list_fix_watch(l, ip);
6441 if (ip == item2)
6442 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006444
6445 if (item2->li_next == NULL)
6446 l->lv_last = item->li_prev;
6447 else
6448 item2->li_next->li_prev = item->li_prev;
6449 if (item->li_prev == NULL)
6450 l->lv_first = item2->li_next;
6451 else
6452 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006453 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006454}
6455
6456/*
6457 * Return an allocated string with the string representation of a list.
6458 * May return NULL.
6459 */
6460 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006461list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006462 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006463 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006464{
6465 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006466
6467 if (tv->vval.v_list == NULL)
6468 return NULL;
6469 ga_init2(&ga, (int)sizeof(char), 80);
6470 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006471 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006472 {
6473 vim_free(ga.ga_data);
6474 return NULL;
6475 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006476 ga_append(&ga, ']');
6477 ga_append(&ga, NUL);
6478 return (char_u *)ga.ga_data;
6479}
6480
6481/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006482 * Join list "l" into a string in "*gap", using separator "sep".
6483 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006485 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006487list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006488 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006490 char_u *sep;
6491 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006492 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006493{
6494 int first = TRUE;
6495 char_u *tofree;
6496 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006498 char_u *s;
6499
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006501 {
6502 if (first)
6503 first = FALSE;
6504 else
6505 ga_concat(gap, sep);
6506
6507 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006508 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006509 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006510 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006511 if (s != NULL)
6512 ga_concat(gap, s);
6513 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006514 if (s == NULL)
6515 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006516 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006517 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006518 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006519}
6520
6521/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006522 * Garbage collection for lists and dictionaries.
6523 *
6524 * We use reference counts to be able to free most items right away when they
6525 * are no longer used. But for composite items it's possible that it becomes
6526 * unused while the reference count is > 0: When there is a recursive
6527 * reference. Example:
6528 * :let l = [1, 2, 3]
6529 * :let d = {9: l}
6530 * :let l[1] = d
6531 *
6532 * Since this is quite unusual we handle this with garbage collection: every
6533 * once in a while find out which lists and dicts are not referenced from any
6534 * variable.
6535 *
6536 * Here is a good reference text about garbage collection (refers to Python
6537 * but it applies to all reference-counting mechanisms):
6538 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006539 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006540
6541/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006542 * Do garbage collection for lists and dicts.
6543 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006544 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006545 int
6546garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006547{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006548 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006549 buf_T *buf;
6550 win_T *wp;
6551 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006552 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006553 int did_free;
6554 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006555#ifdef FEAT_WINDOWS
6556 tabpage_T *tp;
6557#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006558
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006559 /* Only do this once. */
6560 want_garbage_collect = FALSE;
6561 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006562 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006563
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006564 /* We advance by two because we add one for items referenced through
6565 * previous_funccal. */
6566 current_copyID += COPYID_INC;
6567 copyID = current_copyID;
6568
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006569 /*
6570 * 1. Go through all accessible variables and mark all lists and dicts
6571 * with copyID.
6572 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006573
6574 /* Don't free variables in the previous_funccal list unless they are only
6575 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006576 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006577 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6578 {
6579 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6580 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6581 }
6582
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006583 /* script-local variables */
6584 for (i = 1; i <= ga_scripts.ga_len; ++i)
6585 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6586
6587 /* buffer-local variables */
6588 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6589 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6590
6591 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006592 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006593 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6594
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006595#ifdef FEAT_WINDOWS
6596 /* tabpage-local variables */
6597 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6598 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6599#endif
6600
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006601 /* global variables */
6602 set_ref_in_ht(&globvarht, copyID);
6603
6604 /* function-local variables */
6605 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6606 {
6607 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6608 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6609 }
6610
Bram Moolenaard812df62008-11-09 12:46:09 +00006611 /* v: vars */
6612 set_ref_in_ht(&vimvarht, copyID);
6613
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006614 /*
6615 * 2. Free lists and dictionaries that are not referenced.
6616 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006617 did_free = free_unref_items(copyID);
6618
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006619 /*
6620 * 3. Check if any funccal can be freed now.
6621 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006622 for (pfc = &previous_funccal; *pfc != NULL; )
6623 {
6624 if (can_free_funccal(*pfc, copyID))
6625 {
6626 fc = *pfc;
6627 *pfc = fc->caller;
6628 free_funccal(fc, TRUE);
6629 did_free = TRUE;
6630 did_free_funccal = TRUE;
6631 }
6632 else
6633 pfc = &(*pfc)->caller;
6634 }
6635 if (did_free_funccal)
6636 /* When a funccal was freed some more items might be garbage
6637 * collected, so run again. */
6638 (void)garbage_collect();
6639
6640 return did_free;
6641}
6642
6643/*
6644 * Free lists and dictionaries that are no longer referenced.
6645 */
6646 static int
6647free_unref_items(copyID)
6648 int copyID;
6649{
6650 dict_T *dd;
6651 list_T *ll;
6652 int did_free = FALSE;
6653
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006654 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006655 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006656 */
6657 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006658 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006659 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006660 /* Free the Dictionary and ordinary items it contains, but don't
6661 * recurse into Lists and Dictionaries, they will be in the list
6662 * of dicts or list of lists. */
6663 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006664 did_free = TRUE;
6665
6666 /* restart, next dict may also have been freed */
6667 dd = first_dict;
6668 }
6669 else
6670 dd = dd->dv_used_next;
6671
6672 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006673 * Go through the list of lists and free items without the copyID.
6674 * But don't free a list that has a watcher (used in a for loop), these
6675 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006676 */
6677 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006678 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6679 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006680 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006681 /* Free the List and ordinary items it contains, but don't recurse
6682 * into Lists and Dictionaries, they will be in the list of dicts
6683 * or list of lists. */
6684 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006685 did_free = TRUE;
6686
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006687 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006688 ll = first_list;
6689 }
6690 else
6691 ll = ll->lv_used_next;
6692
6693 return did_free;
6694}
6695
6696/*
6697 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6698 */
6699 static void
6700set_ref_in_ht(ht, copyID)
6701 hashtab_T *ht;
6702 int copyID;
6703{
6704 int todo;
6705 hashitem_T *hi;
6706
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006707 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006708 for (hi = ht->ht_array; todo > 0; ++hi)
6709 if (!HASHITEM_EMPTY(hi))
6710 {
6711 --todo;
6712 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6713 }
6714}
6715
6716/*
6717 * Mark all lists and dicts referenced through list "l" with "copyID".
6718 */
6719 static void
6720set_ref_in_list(l, copyID)
6721 list_T *l;
6722 int copyID;
6723{
6724 listitem_T *li;
6725
6726 for (li = l->lv_first; li != NULL; li = li->li_next)
6727 set_ref_in_item(&li->li_tv, copyID);
6728}
6729
6730/*
6731 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6732 */
6733 static void
6734set_ref_in_item(tv, copyID)
6735 typval_T *tv;
6736 int copyID;
6737{
6738 dict_T *dd;
6739 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006740
6741 switch (tv->v_type)
6742 {
6743 case VAR_DICT:
6744 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006745 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006746 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747 /* Didn't see this dict yet. */
6748 dd->dv_copyID = copyID;
6749 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006750 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006751 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006752
6753 case VAR_LIST:
6754 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006755 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006756 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757 /* Didn't see this list yet. */
6758 ll->lv_copyID = copyID;
6759 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006760 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006761 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006762 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006763 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764}
6765
6766/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006767 * Allocate an empty header for a dictionary.
6768 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006769 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006770dict_alloc()
6771{
Bram Moolenaar33570922005-01-25 22:26:29 +00006772 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006773
Bram Moolenaar33570922005-01-25 22:26:29 +00006774 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006775 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006776 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006777 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006778 if (first_dict != NULL)
6779 first_dict->dv_used_prev = d;
6780 d->dv_used_next = first_dict;
6781 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006782 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006783
Bram Moolenaar33570922005-01-25 22:26:29 +00006784 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006785 d->dv_lock = 0;
6786 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006787 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006788 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006789 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006790}
6791
6792/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006793 * Allocate an empty dict for a return value.
6794 * Returns OK or FAIL.
6795 */
6796 static int
6797rettv_dict_alloc(rettv)
6798 typval_T *rettv;
6799{
6800 dict_T *d = dict_alloc();
6801
6802 if (d == NULL)
6803 return FAIL;
6804
6805 rettv->vval.v_dict = d;
6806 rettv->v_type = VAR_DICT;
6807 ++d->dv_refcount;
6808 return OK;
6809}
6810
6811
6812/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006813 * Unreference a Dictionary: decrement the reference count and free it when it
6814 * becomes zero.
6815 */
6816 static void
6817dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006818 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006819{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006820 if (d != NULL && --d->dv_refcount <= 0)
6821 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006822}
6823
6824/*
6825 * Free a Dictionary, including all items it contains.
6826 * Ignores the reference count.
6827 */
6828 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006829dict_free(d, recurse)
6830 dict_T *d;
6831 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006832{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006833 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006834 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006835 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006836
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 /* Remove the dict from the list of dicts for garbage collection. */
6838 if (d->dv_used_prev == NULL)
6839 first_dict = d->dv_used_next;
6840 else
6841 d->dv_used_prev->dv_used_next = d->dv_used_next;
6842 if (d->dv_used_next != NULL)
6843 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6844
6845 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006846 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006847 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006848 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006849 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006850 if (!HASHITEM_EMPTY(hi))
6851 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006852 /* Remove the item before deleting it, just in case there is
6853 * something recursive causing trouble. */
6854 di = HI2DI(hi);
6855 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006856 if (recurse || (di->di_tv.v_type != VAR_LIST
6857 && di->di_tv.v_type != VAR_DICT))
6858 clear_tv(&di->di_tv);
6859 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006860 --todo;
6861 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006862 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006863 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006864 vim_free(d);
6865}
6866
6867/*
6868 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006869 * The "key" is copied to the new item.
6870 * Note that the value of the item "di_tv" still needs to be initialized!
6871 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006872 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006873 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006874dictitem_alloc(key)
6875 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006876{
Bram Moolenaar33570922005-01-25 22:26:29 +00006877 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006878
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006879 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006880 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006881 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006882 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006883 di->di_flags = 0;
6884 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006885 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006886}
6887
6888/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006889 * Make a copy of a Dictionary item.
6890 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006891 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006892dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006893 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006894{
Bram Moolenaar33570922005-01-25 22:26:29 +00006895 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006896
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006897 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6898 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006899 if (di != NULL)
6900 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006901 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006902 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006903 copy_tv(&org->di_tv, &di->di_tv);
6904 }
6905 return di;
6906}
6907
6908/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006909 * Remove item "item" from Dictionary "dict" and free it.
6910 */
6911 static void
6912dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006913 dict_T *dict;
6914 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006915{
Bram Moolenaar33570922005-01-25 22:26:29 +00006916 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006917
Bram Moolenaar33570922005-01-25 22:26:29 +00006918 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006919 if (HASHITEM_EMPTY(hi))
6920 EMSG2(_(e_intern2), "dictitem_remove()");
6921 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006922 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006923 dictitem_free(item);
6924}
6925
6926/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006927 * Free a dict item. Also clears the value.
6928 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006929 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006930dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006931 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006932{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006933 clear_tv(&item->di_tv);
6934 vim_free(item);
6935}
6936
6937/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006938 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6939 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006940 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006941 * Returns NULL when out of memory.
6942 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006944dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006945 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006946 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006947 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006948{
Bram Moolenaar33570922005-01-25 22:26:29 +00006949 dict_T *copy;
6950 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006951 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006952 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006953
6954 if (orig == NULL)
6955 return NULL;
6956
6957 copy = dict_alloc();
6958 if (copy != NULL)
6959 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006960 if (copyID != 0)
6961 {
6962 orig->dv_copyID = copyID;
6963 orig->dv_copydict = copy;
6964 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006965 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006966 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006967 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006968 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006969 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006970 --todo;
6971
6972 di = dictitem_alloc(hi->hi_key);
6973 if (di == NULL)
6974 break;
6975 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006976 {
6977 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6978 copyID) == FAIL)
6979 {
6980 vim_free(di);
6981 break;
6982 }
6983 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006984 else
6985 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6986 if (dict_add(copy, di) == FAIL)
6987 {
6988 dictitem_free(di);
6989 break;
6990 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006991 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006992 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006993
Bram Moolenaare9a41262005-01-15 22:18:47 +00006994 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006995 if (todo > 0)
6996 {
6997 dict_unref(copy);
6998 copy = NULL;
6999 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007000 }
7001
7002 return copy;
7003}
7004
7005/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007006 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007007 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007008 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007009 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 dict_T *d;
7012 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013{
Bram Moolenaar33570922005-01-25 22:26:29 +00007014 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007015}
7016
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007018 * Add a number or string entry to dictionary "d".
7019 * When "str" is NULL use number "nr", otherwise use "str".
7020 * Returns FAIL when out of memory and when key already exists.
7021 */
7022 int
7023dict_add_nr_str(d, key, nr, str)
7024 dict_T *d;
7025 char *key;
7026 long nr;
7027 char_u *str;
7028{
7029 dictitem_T *item;
7030
7031 item = dictitem_alloc((char_u *)key);
7032 if (item == NULL)
7033 return FAIL;
7034 item->di_tv.v_lock = 0;
7035 if (str == NULL)
7036 {
7037 item->di_tv.v_type = VAR_NUMBER;
7038 item->di_tv.vval.v_number = nr;
7039 }
7040 else
7041 {
7042 item->di_tv.v_type = VAR_STRING;
7043 item->di_tv.vval.v_string = vim_strsave(str);
7044 }
7045 if (dict_add(d, item) == FAIL)
7046 {
7047 dictitem_free(item);
7048 return FAIL;
7049 }
7050 return OK;
7051}
7052
7053/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007054 * Add a list entry to dictionary "d".
7055 * Returns FAIL when out of memory and when key already exists.
7056 */
7057 int
7058dict_add_list(d, key, list)
7059 dict_T *d;
7060 char *key;
7061 list_T *list;
7062{
7063 dictitem_T *item;
7064
7065 item = dictitem_alloc((char_u *)key);
7066 if (item == NULL)
7067 return FAIL;
7068 item->di_tv.v_lock = 0;
7069 item->di_tv.v_type = VAR_LIST;
7070 item->di_tv.vval.v_list = list;
7071 if (dict_add(d, item) == FAIL)
7072 {
7073 dictitem_free(item);
7074 return FAIL;
7075 }
7076 return OK;
7077}
7078
7079/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007080 * Get the number of items in a Dictionary.
7081 */
7082 static long
7083dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007084 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007085{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007086 if (d == NULL)
7087 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007088 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089}
7090
7091/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092 * Find item "key[len]" in Dictionary "d".
7093 * If "len" is negative use strlen(key).
7094 * Returns NULL when not found.
7095 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007096 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007097dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007098 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099 char_u *key;
7100 int len;
7101{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007102#define AKEYLEN 200
7103 char_u buf[AKEYLEN];
7104 char_u *akey;
7105 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007107
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007108 if (len < 0)
7109 akey = key;
7110 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007111 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 tofree = akey = vim_strnsave(key, len);
7113 if (akey == NULL)
7114 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007115 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116 else
7117 {
7118 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007119 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 akey = buf;
7121 }
7122
Bram Moolenaar33570922005-01-25 22:26:29 +00007123 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 vim_free(tofree);
7125 if (HASHITEM_EMPTY(hi))
7126 return NULL;
7127 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007128}
7129
7130/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007131 * Get a string item from a dictionary.
7132 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007133 * Returns NULL if the entry doesn't exist or out of memory.
7134 */
7135 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007136get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007137 dict_T *d;
7138 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007139 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007140{
7141 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007142 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007143
7144 di = dict_find(d, key, -1);
7145 if (di == NULL)
7146 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007147 s = get_tv_string(&di->di_tv);
7148 if (save && s != NULL)
7149 s = vim_strsave(s);
7150 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007151}
7152
7153/*
7154 * Get a number item from a dictionary.
7155 * Returns 0 if the entry doesn't exist or out of memory.
7156 */
7157 long
7158get_dict_number(d, key)
7159 dict_T *d;
7160 char_u *key;
7161{
7162 dictitem_T *di;
7163
7164 di = dict_find(d, key, -1);
7165 if (di == NULL)
7166 return 0;
7167 return get_tv_number(&di->di_tv);
7168}
7169
7170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 * Return an allocated string with the string representation of a Dictionary.
7172 * May return NULL.
7173 */
7174 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007175dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007177 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007178{
7179 garray_T ga;
7180 int first = TRUE;
7181 char_u *tofree;
7182 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007185 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007186 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007187
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007189 return NULL;
7190 ga_init2(&ga, (int)sizeof(char), 80);
7191 ga_append(&ga, '{');
7192
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007193 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007194 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007195 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007198 --todo;
7199
7200 if (first)
7201 first = FALSE;
7202 else
7203 ga_concat(&ga, (char_u *)", ");
7204
7205 tofree = string_quote(hi->hi_key, FALSE);
7206 if (tofree != NULL)
7207 {
7208 ga_concat(&ga, tofree);
7209 vim_free(tofree);
7210 }
7211 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007212 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213 if (s != NULL)
7214 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007216 if (s == NULL)
7217 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007220 if (todo > 0)
7221 {
7222 vim_free(ga.ga_data);
7223 return NULL;
7224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225
7226 ga_append(&ga, '}');
7227 ga_append(&ga, NUL);
7228 return (char_u *)ga.ga_data;
7229}
7230
7231/*
7232 * Allocate a variable for a Dictionary and fill it from "*arg".
7233 * Return OK or FAIL. Returns NOTDONE for {expr}.
7234 */
7235 static int
7236get_dict_tv(arg, rettv, evaluate)
7237 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007238 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239 int evaluate;
7240{
Bram Moolenaar33570922005-01-25 22:26:29 +00007241 dict_T *d = NULL;
7242 typval_T tvkey;
7243 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007244 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007245 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248
7249 /*
7250 * First check if it's not a curly-braces thing: {expr}.
7251 * Must do this without evaluating, otherwise a function may be called
7252 * twice. Unfortunately this means we need to call eval1() twice for the
7253 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007254 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007256 if (*start != '}')
7257 {
7258 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7259 return FAIL;
7260 if (*start == '}')
7261 return NOTDONE;
7262 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263
7264 if (evaluate)
7265 {
7266 d = dict_alloc();
7267 if (d == NULL)
7268 return FAIL;
7269 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 tvkey.v_type = VAR_UNKNOWN;
7271 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272
7273 *arg = skipwhite(*arg + 1);
7274 while (**arg != '}' && **arg != NUL)
7275 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277 goto failret;
7278 if (**arg != ':')
7279 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007280 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007281 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282 goto failret;
7283 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007284 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007286 key = get_tv_string_buf_chk(&tvkey, buf);
7287 if (key == NULL || *key == NUL)
7288 {
7289 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7290 if (key != NULL)
7291 EMSG(_(e_emptykey));
7292 clear_tv(&tvkey);
7293 goto failret;
7294 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296
7297 *arg = skipwhite(*arg + 1);
7298 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7299 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007300 if (evaluate)
7301 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 goto failret;
7303 }
7304 if (evaluate)
7305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007307 if (item != NULL)
7308 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007309 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 clear_tv(&tv);
7312 goto failret;
7313 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314 item = dictitem_alloc(key);
7315 clear_tv(&tvkey);
7316 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007317 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007318 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007319 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (dict_add(d, item) == FAIL)
7321 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322 }
7323 }
7324
7325 if (**arg == '}')
7326 break;
7327 if (**arg != ',')
7328 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007329 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007330 goto failret;
7331 }
7332 *arg = skipwhite(*arg + 1);
7333 }
7334
7335 if (**arg != '}')
7336 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007337 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338failret:
7339 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007340 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341 return FAIL;
7342 }
7343
7344 *arg = skipwhite(*arg + 1);
7345 if (evaluate)
7346 {
7347 rettv->v_type = VAR_DICT;
7348 rettv->vval.v_dict = d;
7349 ++d->dv_refcount;
7350 }
7351
7352 return OK;
7353}
7354
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007356 * Return a string with the string representation of a variable.
7357 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007358 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007359 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007360 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007361 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007362 */
7363 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007364echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007366 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007367 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007368 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007369{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 static int recurse = 0;
7371 char_u *r = NULL;
7372
Bram Moolenaar33570922005-01-25 22:26:29 +00007373 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007375 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 *tofree = NULL;
7377 return NULL;
7378 }
7379 ++recurse;
7380
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007381 switch (tv->v_type)
7382 {
7383 case VAR_FUNC:
7384 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385 r = tv->vval.v_string;
7386 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007387
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007388 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007389 if (tv->vval.v_list == NULL)
7390 {
7391 *tofree = NULL;
7392 r = NULL;
7393 }
7394 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7395 {
7396 *tofree = NULL;
7397 r = (char_u *)"[...]";
7398 }
7399 else
7400 {
7401 tv->vval.v_list->lv_copyID = copyID;
7402 *tofree = list2string(tv, copyID);
7403 r = *tofree;
7404 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007405 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007406
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007408 if (tv->vval.v_dict == NULL)
7409 {
7410 *tofree = NULL;
7411 r = NULL;
7412 }
7413 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7414 {
7415 *tofree = NULL;
7416 r = (char_u *)"{...}";
7417 }
7418 else
7419 {
7420 tv->vval.v_dict->dv_copyID = copyID;
7421 *tofree = dict2string(tv, copyID);
7422 r = *tofree;
7423 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007424 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426 case VAR_STRING:
7427 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 *tofree = NULL;
7429 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007430 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007431
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007432#ifdef FEAT_FLOAT
7433 case VAR_FLOAT:
7434 *tofree = NULL;
7435 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7436 r = numbuf;
7437 break;
7438#endif
7439
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007440 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007441 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007442 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007443 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007444
7445 --recurse;
7446 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007447}
7448
7449/*
7450 * Return a string with the string representation of a variable.
7451 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7452 * "numbuf" is used for a number.
7453 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007454 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007455 */
7456 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007457tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007459 char_u **tofree;
7460 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007461 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007462{
7463 switch (tv->v_type)
7464 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007465 case VAR_FUNC:
7466 *tofree = string_quote(tv->vval.v_string, TRUE);
7467 return *tofree;
7468 case VAR_STRING:
7469 *tofree = string_quote(tv->vval.v_string, FALSE);
7470 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007471#ifdef FEAT_FLOAT
7472 case VAR_FLOAT:
7473 *tofree = NULL;
7474 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7475 return numbuf;
7476#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007478 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007481 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007482 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007483 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007484 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007485}
7486
7487/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 * Return string "str" in ' quotes, doubling ' characters.
7489 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007491 */
7492 static char_u *
7493string_quote(str, function)
7494 char_u *str;
7495 int function;
7496{
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007498 char_u *p, *r, *s;
7499
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 len = (function ? 13 : 3);
7501 if (str != NULL)
7502 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007503 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 for (p = str; *p != NUL; mb_ptr_adv(p))
7505 if (*p == '\'')
7506 ++len;
7507 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007508 s = r = alloc(len);
7509 if (r != NULL)
7510 {
7511 if (function)
7512 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007514 r += 10;
7515 }
7516 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007518 if (str != NULL)
7519 for (p = str; *p != NUL; )
7520 {
7521 if (*p == '\'')
7522 *r++ = '\'';
7523 MB_COPY_CHAR(p, r);
7524 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007526 if (function)
7527 *r++ = ')';
7528 *r++ = NUL;
7529 }
7530 return s;
7531}
7532
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007533#ifdef FEAT_FLOAT
7534/*
7535 * Convert the string "text" to a floating point number.
7536 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7537 * this always uses a decimal point.
7538 * Returns the length of the text that was consumed.
7539 */
7540 static int
7541string2float(text, value)
7542 char_u *text;
7543 float_T *value; /* result stored here */
7544{
7545 char *s = (char *)text;
7546 float_T f;
7547
7548 f = strtod(s, &s);
7549 *value = f;
7550 return (int)((char_u *)s - text);
7551}
7552#endif
7553
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 * Get the value of an environment variable.
7556 * "arg" is pointing to the '$'. It is advanced to after the name.
7557 * If the environment variable was not set, silently assume it is empty.
7558 * Always return OK.
7559 */
7560 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007561get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 int evaluate;
7565{
7566 char_u *string = NULL;
7567 int len;
7568 int cc;
7569 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007570 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571
7572 ++*arg;
7573 name = *arg;
7574 len = get_env_len(arg);
7575 if (evaluate)
7576 {
7577 if (len != 0)
7578 {
7579 cc = name[len];
7580 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007581 /* first try vim_getenv(), fast for normal environment vars */
7582 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007584 {
7585 if (!mustfree)
7586 string = vim_strsave(string);
7587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 else
7589 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007590 if (mustfree)
7591 vim_free(string);
7592
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 /* next try expanding things like $VIM and ${HOME} */
7594 string = expand_env_save(name - 1);
7595 if (string != NULL && *string == '$')
7596 {
7597 vim_free(string);
7598 string = NULL;
7599 }
7600 }
7601 name[len] = cc;
7602 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007603 rettv->v_type = VAR_STRING;
7604 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 }
7606
7607 return OK;
7608}
7609
7610/*
7611 * Array with names and number of arguments of all internal functions
7612 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7613 */
7614static struct fst
7615{
7616 char *f_name; /* function name */
7617 char f_min_argc; /* minimal number of arguments */
7618 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007620 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621} functions[] =
7622{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007623#ifdef FEAT_FLOAT
7624 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007625 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007626#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007627 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 {"append", 2, 2, f_append},
7629 {"argc", 0, 0, f_argc},
7630 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007631 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007632#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007633 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007634 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007635 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007638 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 {"bufexists", 1, 1, f_bufexists},
7640 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7641 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7642 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7643 {"buflisted", 1, 1, f_buflisted},
7644 {"bufloaded", 1, 1, f_bufloaded},
7645 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007646 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {"bufwinnr", 1, 1, f_bufwinnr},
7648 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007649 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007650 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007651#ifdef FEAT_FLOAT
7652 {"ceil", 1, 1, f_ceil},
7653#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007654 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {"char2nr", 1, 1, f_char2nr},
7656 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007657 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007659#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007660 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007661 {"complete_add", 1, 1, f_complete_add},
7662 {"complete_check", 0, 0, f_complete_check},
7663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007665 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007666#ifdef FEAT_FLOAT
7667 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007668 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007669#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007670 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007672 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007673 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 {"delete", 1, 1, f_delete},
7675 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007676 {"diff_filler", 1, 1, f_diff_filler},
7677 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007678 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007680 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {"eventhandler", 0, 0, f_eventhandler},
7682 {"executable", 1, 1, f_executable},
7683 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007684#ifdef FEAT_FLOAT
7685 {"exp", 1, 1, f_exp},
7686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007688 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007689 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7691 {"filereadable", 1, 1, f_filereadable},
7692 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007694 {"finddir", 1, 3, f_finddir},
7695 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007696#ifdef FEAT_FLOAT
7697 {"float2nr", 1, 1, f_float2nr},
7698 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007699 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007700#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007701 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {"fnamemodify", 2, 2, f_fnamemodify},
7703 {"foldclosed", 1, 1, f_foldclosed},
7704 {"foldclosedend", 1, 1, f_foldclosedend},
7705 {"foldlevel", 1, 1, f_foldlevel},
7706 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007707 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007709 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007710 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007711 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007712 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {"getbufvar", 2, 2, f_getbufvar},
7714 {"getchar", 0, 1, f_getchar},
7715 {"getcharmod", 0, 0, f_getcharmod},
7716 {"getcmdline", 0, 0, f_getcmdline},
7717 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007718 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007720 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007721 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {"getfsize", 1, 1, f_getfsize},
7723 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007724 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007725 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007726 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007727 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007728 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007729 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007730 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007731 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007733 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007734 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 {"getwinposx", 0, 0, f_getwinposx},
7736 {"getwinposy", 0, 0, f_getwinposy},
7737 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007738 {"glob", 1, 2, f_glob},
7739 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007741 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007742 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007743 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7745 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7746 {"histadd", 2, 2, f_histadd},
7747 {"histdel", 1, 2, f_histdel},
7748 {"histget", 1, 2, f_histget},
7749 {"histnr", 1, 1, f_histnr},
7750 {"hlID", 1, 1, f_hlID},
7751 {"hlexists", 1, 1, f_hlexists},
7752 {"hostname", 0, 0, f_hostname},
7753 {"iconv", 3, 3, f_iconv},
7754 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007755 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007756 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007758 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 {"inputrestore", 0, 0, f_inputrestore},
7760 {"inputsave", 0, 0, f_inputsave},
7761 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007762 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007764 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007766 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007767 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007769 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 {"libcall", 3, 3, f_libcall},
7771 {"libcallnr", 3, 3, f_libcallnr},
7772 {"line", 1, 1, f_line},
7773 {"line2byte", 1, 1, f_line2byte},
7774 {"lispindent", 1, 1, f_lispindent},
7775 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007776#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007777 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007778 {"log10", 1, 1, f_log10},
7779#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007780 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007781 {"maparg", 1, 3, f_maparg},
7782 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007783 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007784 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007785 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007786 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007787 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007788 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007789 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007790 {"max", 1, 1, f_max},
7791 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007792#ifdef vim_mkdir
7793 {"mkdir", 1, 3, f_mkdir},
7794#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007795 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007796#ifdef FEAT_MZSCHEME
7797 {"mzeval", 1, 1, f_mzeval},
7798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 {"nextnonblank", 1, 1, f_nextnonblank},
7800 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007801 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007802#ifdef FEAT_FLOAT
7803 {"pow", 2, 2, f_pow},
7804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007806 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007807 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007808 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007809 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007810 {"reltime", 0, 2, f_reltime},
7811 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 {"remote_expr", 2, 3, f_remote_expr},
7813 {"remote_foreground", 1, 1, f_remote_foreground},
7814 {"remote_peek", 1, 2, f_remote_peek},
7815 {"remote_read", 1, 1, f_remote_read},
7816 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007817 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007819 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007821 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007822#ifdef FEAT_FLOAT
7823 {"round", 1, 1, f_round},
7824#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007825 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007826 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007827 {"searchpair", 3, 7, f_searchpair},
7828 {"searchpairpos", 3, 7, f_searchpairpos},
7829 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {"server2client", 2, 2, f_server2client},
7831 {"serverlist", 0, 0, f_serverlist},
7832 {"setbufvar", 3, 3, f_setbufvar},
7833 {"setcmdpos", 1, 1, f_setcmdpos},
7834 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007835 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007836 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007837 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007838 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007840 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007841 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007843 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845#ifdef FEAT_FLOAT
7846 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007847 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007848#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007849 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007850 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007851 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007852 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007853 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007854#ifdef FEAT_FLOAT
7855 {"sqrt", 1, 1, f_sqrt},
7856 {"str2float", 1, 1, f_str2float},
7857#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007858 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859#ifdef HAVE_STRFTIME
7860 {"strftime", 1, 2, f_strftime},
7861#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007863 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {"strlen", 1, 1, f_strlen},
7865 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007866 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {"strtrans", 1, 1, f_strtrans},
7868 {"submatch", 1, 1, f_submatch},
7869 {"substitute", 4, 4, f_substitute},
7870 {"synID", 3, 3, f_synID},
7871 {"synIDattr", 2, 3, f_synIDattr},
7872 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007873 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007874 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007875 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007876 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007877 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007878 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007879 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007880#ifdef FEAT_FLOAT
7881 {"tan", 1, 1, f_tan},
7882 {"tanh", 1, 1, f_tanh},
7883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007885 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {"tolower", 1, 1, f_tolower},
7887 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007888 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007889#ifdef FEAT_FLOAT
7890 {"trunc", 1, 1, f_trunc},
7891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007893 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007894 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007895 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {"virtcol", 1, 1, f_virtcol},
7897 {"visualmode", 0, 1, f_visualmode},
7898 {"winbufnr", 1, 1, f_winbufnr},
7899 {"wincol", 0, 0, f_wincol},
7900 {"winheight", 1, 1, f_winheight},
7901 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007902 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007904 {"winrestview", 1, 1, f_winrestview},
7905 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007907 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908};
7909
7910#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7911
7912/*
7913 * Function given to ExpandGeneric() to obtain the list of internal
7914 * or user defined function names.
7915 */
7916 char_u *
7917get_function_name(xp, idx)
7918 expand_T *xp;
7919 int idx;
7920{
7921 static int intidx = -1;
7922 char_u *name;
7923
7924 if (idx == 0)
7925 intidx = -1;
7926 if (intidx < 0)
7927 {
7928 name = get_user_func_name(xp, idx);
7929 if (name != NULL)
7930 return name;
7931 }
7932 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7933 {
7934 STRCPY(IObuff, functions[intidx].f_name);
7935 STRCAT(IObuff, "(");
7936 if (functions[intidx].f_max_argc == 0)
7937 STRCAT(IObuff, ")");
7938 return IObuff;
7939 }
7940
7941 return NULL;
7942}
7943
7944/*
7945 * Function given to ExpandGeneric() to obtain the list of internal or
7946 * user defined variable or function names.
7947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 char_u *
7949get_expr_name(xp, idx)
7950 expand_T *xp;
7951 int idx;
7952{
7953 static int intidx = -1;
7954 char_u *name;
7955
7956 if (idx == 0)
7957 intidx = -1;
7958 if (intidx < 0)
7959 {
7960 name = get_function_name(xp, idx);
7961 if (name != NULL)
7962 return name;
7963 }
7964 return get_user_var_name(xp, ++intidx);
7965}
7966
7967#endif /* FEAT_CMDL_COMPL */
7968
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007969#if defined(EBCDIC) || defined(PROTO)
7970/*
7971 * Compare struct fst by function name.
7972 */
7973 static int
7974compare_func_name(s1, s2)
7975 const void *s1;
7976 const void *s2;
7977{
7978 struct fst *p1 = (struct fst *)s1;
7979 struct fst *p2 = (struct fst *)s2;
7980
7981 return STRCMP(p1->f_name, p2->f_name);
7982}
7983
7984/*
7985 * Sort the function table by function name.
7986 * The sorting of the table above is ASCII dependant.
7987 * On machines using EBCDIC we have to sort it.
7988 */
7989 static void
7990sortFunctions()
7991{
7992 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
7993
7994 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
7995}
7996#endif
7997
7998
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999/*
8000 * Find internal function in table above.
8001 * Return index, or -1 if not found
8002 */
8003 static int
8004find_internal_func(name)
8005 char_u *name; /* name of the function */
8006{
8007 int first = 0;
8008 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8009 int cmp;
8010 int x;
8011
8012 /*
8013 * Find the function name in the table. Binary search.
8014 */
8015 while (first <= last)
8016 {
8017 x = first + ((unsigned)(last - first) >> 1);
8018 cmp = STRCMP(name, functions[x].f_name);
8019 if (cmp < 0)
8020 last = x - 1;
8021 else if (cmp > 0)
8022 first = x + 1;
8023 else
8024 return x;
8025 }
8026 return -1;
8027}
8028
8029/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008030 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8031 * name it contains, otherwise return "name".
8032 */
8033 static char_u *
8034deref_func_name(name, lenp)
8035 char_u *name;
8036 int *lenp;
8037{
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008039 int cc;
8040
8041 cc = name[*lenp];
8042 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008043 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008044 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008046 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008047 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008048 {
8049 *lenp = 0;
8050 return (char_u *)""; /* just in case */
8051 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008052 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008053 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008054 }
8055
8056 return name;
8057}
8058
8059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 * Allocate a variable for the result of a function.
8061 * Return OK or FAIL.
8062 */
8063 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008064get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8065 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 char_u *name; /* name of the function */
8067 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 char_u **arg; /* argument, pointing to the '(' */
8070 linenr_T firstline; /* first line of range */
8071 linenr_T lastline; /* last line of range */
8072 int *doesrange; /* return: function handled range */
8073 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008074 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076 char_u *argp;
8077 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008078 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 int argcount = 0; /* number of arguments found */
8080
8081 /*
8082 * Get the arguments.
8083 */
8084 argp = *arg;
8085 while (argcount < MAX_FUNC_ARGS)
8086 {
8087 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8088 if (*argp == ')' || *argp == ',' || *argp == NUL)
8089 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8091 {
8092 ret = FAIL;
8093 break;
8094 }
8095 ++argcount;
8096 if (*argp != ',')
8097 break;
8098 }
8099 if (*argp == ')')
8100 ++argp;
8101 else
8102 ret = FAIL;
8103
8104 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008105 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008106 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008108 {
8109 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008110 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008111 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008112 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114
8115 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008116 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117
8118 *arg = skipwhite(argp);
8119 return ret;
8120}
8121
8122
8123/*
8124 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008125 * Return OK when the function can't be called, FAIL otherwise.
8126 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 */
8128 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008129call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008130 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008131 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008133 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008135 typval_T *argvars; /* vars for arguments, must have "argcount"
8136 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 linenr_T firstline; /* first line of range */
8138 linenr_T lastline; /* last line of range */
8139 int *doesrange; /* return: function handled range */
8140 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008141 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142{
8143 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144#define ERROR_UNKNOWN 0
8145#define ERROR_TOOMANY 1
8146#define ERROR_TOOFEW 2
8147#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008148#define ERROR_DICT 4
8149#define ERROR_NONE 5
8150#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 int error = ERROR_NONE;
8152 int i;
8153 int llen;
8154 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155#define FLEN_FIXED 40
8156 char_u fname_buf[FLEN_FIXED + 1];
8157 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008158 char_u *name;
8159
8160 /* Make a copy of the name, if it comes from a funcref variable it could
8161 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008162 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008163 if (name == NULL)
8164 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165
8166 /*
8167 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8168 * Change <SNR>123_name() to K_SNR 123_name().
8169 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 llen = eval_fname_script(name);
8172 if (llen > 0)
8173 {
8174 fname_buf[0] = K_SPECIAL;
8175 fname_buf[1] = KS_EXTRA;
8176 fname_buf[2] = (int)KE_SNR;
8177 i = 3;
8178 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8179 {
8180 if (current_SID <= 0)
8181 error = ERROR_SCRIPT;
8182 else
8183 {
8184 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8185 i = (int)STRLEN(fname_buf);
8186 }
8187 }
8188 if (i + STRLEN(name + llen) < FLEN_FIXED)
8189 {
8190 STRCPY(fname_buf + i, name + llen);
8191 fname = fname_buf;
8192 }
8193 else
8194 {
8195 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8196 if (fname == NULL)
8197 error = ERROR_OTHER;
8198 else
8199 {
8200 mch_memmove(fname, fname_buf, (size_t)i);
8201 STRCPY(fname + i, name + llen);
8202 }
8203 }
8204 }
8205 else
8206 fname = name;
8207
8208 *doesrange = FALSE;
8209
8210
8211 /* execute the function if no errors detected and executing */
8212 if (evaluate && error == ERROR_NONE)
8213 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008214 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8215 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 error = ERROR_UNKNOWN;
8217
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008218 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {
8220 /*
8221 * User defined function.
8222 */
8223 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008224
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008226 /* Trigger FuncUndefined event, may load the function. */
8227 if (fp == NULL
8228 && apply_autocmds(EVENT_FUNCUNDEFINED,
8229 fname, fname, TRUE, NULL)
8230 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008232 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 fp = find_func(fname);
8234 }
8235#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008236 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008237 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008238 {
8239 /* loaded a package, search for the function again */
8240 fp = find_func(fname);
8241 }
8242
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 if (fp != NULL)
8244 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008245 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008247 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008249 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008251 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008252 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 else
8254 {
8255 /*
8256 * Call the user function.
8257 * Save and restore search patterns, script variables and
8258 * redo buffer.
8259 */
8260 save_search_patterns();
8261 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008262 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008263 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008264 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008265 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8266 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8267 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008268 /* Function was unreferenced while being used, free it
8269 * now. */
8270 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 restoreRedobuff();
8272 restore_search_patterns();
8273 error = ERROR_NONE;
8274 }
8275 }
8276 }
8277 else
8278 {
8279 /*
8280 * Find the function name in the table, call its implementation.
8281 */
8282 i = find_internal_func(fname);
8283 if (i >= 0)
8284 {
8285 if (argcount < functions[i].f_min_argc)
8286 error = ERROR_TOOFEW;
8287 else if (argcount > functions[i].f_max_argc)
8288 error = ERROR_TOOMANY;
8289 else
8290 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008291 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 error = ERROR_NONE;
8294 }
8295 }
8296 }
8297 /*
8298 * The function call (or "FuncUndefined" autocommand sequence) might
8299 * have been aborted by an error, an interrupt, or an explicitly thrown
8300 * exception that has not been caught so far. This situation can be
8301 * tested for by calling aborting(). For an error in an internal
8302 * function or for the "E132" error in call_user_func(), however, the
8303 * throw point at which the "force_abort" flag (temporarily reset by
8304 * emsg()) is normally updated has not been reached yet. We need to
8305 * update that flag first to make aborting() reliable.
8306 */
8307 update_force_abort();
8308 }
8309 if (error == ERROR_NONE)
8310 ret = OK;
8311
8312 /*
8313 * Report an error unless the argument evaluation or function call has been
8314 * cancelled due to an aborting error, an interrupt, or an exception.
8315 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008316 if (!aborting())
8317 {
8318 switch (error)
8319 {
8320 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008321 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008322 break;
8323 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008324 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008325 break;
8326 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008327 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008328 name);
8329 break;
8330 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008331 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008332 name);
8333 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008334 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008335 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008336 name);
8337 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008338 }
8339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 if (fname != name && fname != fname_buf)
8342 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008343 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
8345 return ret;
8346}
8347
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008348/*
8349 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008350 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008351 */
8352 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008353emsg_funcname(ermsg, name)
8354 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008355 char_u *name;
8356{
8357 char_u *p;
8358
8359 if (*name == K_SPECIAL)
8360 p = concat_str((char_u *)"<SNR>", name + 3);
8361 else
8362 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008363 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008364 if (p != name)
8365 vim_free(p);
8366}
8367
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008368/*
8369 * Return TRUE for a non-zero Number and a non-empty String.
8370 */
8371 static int
8372non_zero_arg(argvars)
8373 typval_T *argvars;
8374{
8375 return ((argvars[0].v_type == VAR_NUMBER
8376 && argvars[0].vval.v_number != 0)
8377 || (argvars[0].v_type == VAR_STRING
8378 && argvars[0].vval.v_string != NULL
8379 && *argvars[0].vval.v_string != NUL));
8380}
8381
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382/*********************************************
8383 * Implementation of the built-in functions
8384 */
8385
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008386#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008387static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8388
8389/*
8390 * Get the float value of "argvars[0]" into "f".
8391 * Returns FAIL when the argument is not a Number or Float.
8392 */
8393 static int
8394get_float_arg(argvars, f)
8395 typval_T *argvars;
8396 float_T *f;
8397{
8398 if (argvars[0].v_type == VAR_FLOAT)
8399 {
8400 *f = argvars[0].vval.v_float;
8401 return OK;
8402 }
8403 if (argvars[0].v_type == VAR_NUMBER)
8404 {
8405 *f = (float_T)argvars[0].vval.v_number;
8406 return OK;
8407 }
8408 EMSG(_("E808: Number or Float required"));
8409 return FAIL;
8410}
8411
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008412/*
8413 * "abs(expr)" function
8414 */
8415 static void
8416f_abs(argvars, rettv)
8417 typval_T *argvars;
8418 typval_T *rettv;
8419{
8420 if (argvars[0].v_type == VAR_FLOAT)
8421 {
8422 rettv->v_type = VAR_FLOAT;
8423 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8424 }
8425 else
8426 {
8427 varnumber_T n;
8428 int error = FALSE;
8429
8430 n = get_tv_number_chk(&argvars[0], &error);
8431 if (error)
8432 rettv->vval.v_number = -1;
8433 else if (n > 0)
8434 rettv->vval.v_number = n;
8435 else
8436 rettv->vval.v_number = -n;
8437 }
8438}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008439
8440/*
8441 * "acos()" function
8442 */
8443 static void
8444f_acos(argvars, rettv)
8445 typval_T *argvars;
8446 typval_T *rettv;
8447{
8448 float_T f;
8449
8450 rettv->v_type = VAR_FLOAT;
8451 if (get_float_arg(argvars, &f) == OK)
8452 rettv->vval.v_float = acos(f);
8453 else
8454 rettv->vval.v_float = 0.0;
8455}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008456#endif
8457
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008459 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 */
8461 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008462f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008463 typval_T *argvars;
8464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465{
Bram Moolenaar33570922005-01-25 22:26:29 +00008466 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008468 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008469 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008471 if ((l = argvars[0].vval.v_list) != NULL
8472 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8473 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008474 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008475 }
8476 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008477 EMSG(_(e_listreq));
8478}
8479
8480/*
8481 * "append(lnum, string/list)" function
8482 */
8483 static void
8484f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008485 typval_T *argvars;
8486 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008487{
8488 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008489 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008490 list_T *l = NULL;
8491 listitem_T *li = NULL;
8492 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008493 long added = 0;
8494
Bram Moolenaar0d660222005-01-07 21:51:51 +00008495 lnum = get_tv_lnum(argvars);
8496 if (lnum >= 0
8497 && lnum <= curbuf->b_ml.ml_line_count
8498 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008499 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008500 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008502 l = argvars[1].vval.v_list;
8503 if (l == NULL)
8504 return;
8505 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008506 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008507 for (;;)
8508 {
8509 if (l == NULL)
8510 tv = &argvars[1]; /* append a string */
8511 else if (li == NULL)
8512 break; /* end of list */
8513 else
8514 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008515 line = get_tv_string_chk(tv);
8516 if (line == NULL) /* type error */
8517 {
8518 rettv->vval.v_number = 1; /* Failed */
8519 break;
8520 }
8521 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008522 ++added;
8523 if (l == NULL)
8524 break;
8525 li = li->li_next;
8526 }
8527
8528 appended_lines_mark(lnum, added);
8529 if (curwin->w_cursor.lnum > lnum)
8530 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008532 else
8533 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534}
8535
8536/*
8537 * "argc()" function
8538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008540f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008541 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008544 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545}
8546
8547/*
8548 * "argidx()" function
8549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008551f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008552 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008555 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556}
8557
8558/*
8559 * "argv(nr)" function
8560 */
8561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008563 typval_T *argvars;
8564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565{
8566 int idx;
8567
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008568 if (argvars[0].v_type != VAR_UNKNOWN)
8569 {
8570 idx = get_tv_number_chk(&argvars[0], NULL);
8571 if (idx >= 0 && idx < ARGCOUNT)
8572 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8573 else
8574 rettv->vval.v_string = NULL;
8575 rettv->v_type = VAR_STRING;
8576 }
8577 else if (rettv_list_alloc(rettv) == OK)
8578 for (idx = 0; idx < ARGCOUNT; ++idx)
8579 list_append_string(rettv->vval.v_list,
8580 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581}
8582
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008583#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008584/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008585 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008586 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008587 static void
8588f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008589 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008590 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008591{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008592 float_T f;
8593
8594 rettv->v_type = VAR_FLOAT;
8595 if (get_float_arg(argvars, &f) == OK)
8596 rettv->vval.v_float = asin(f);
8597 else
8598 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008599}
8600
8601/*
8602 * "atan()" function
8603 */
8604 static void
8605f_atan(argvars, rettv)
8606 typval_T *argvars;
8607 typval_T *rettv;
8608{
8609 float_T f;
8610
8611 rettv->v_type = VAR_FLOAT;
8612 if (get_float_arg(argvars, &f) == OK)
8613 rettv->vval.v_float = atan(f);
8614 else
8615 rettv->vval.v_float = 0.0;
8616}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008617
8618/*
8619 * "atan2()" function
8620 */
8621 static void
8622f_atan2(argvars, rettv)
8623 typval_T *argvars;
8624 typval_T *rettv;
8625{
8626 float_T fx, fy;
8627
8628 rettv->v_type = VAR_FLOAT;
8629 if (get_float_arg(argvars, &fx) == OK
8630 && get_float_arg(&argvars[1], &fy) == OK)
8631 rettv->vval.v_float = atan2(fx, fy);
8632 else
8633 rettv->vval.v_float = 0.0;
8634}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008635#endif
8636
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637/*
8638 * "browse(save, title, initdir, default)" function
8639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008641f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008642 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644{
8645#ifdef FEAT_BROWSE
8646 int save;
8647 char_u *title;
8648 char_u *initdir;
8649 char_u *defname;
8650 char_u buf[NUMBUFLEN];
8651 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008652 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008654 save = get_tv_number_chk(&argvars[0], &error);
8655 title = get_tv_string_chk(&argvars[1]);
8656 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8657 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008659 if (error || title == NULL || initdir == NULL || defname == NULL)
8660 rettv->vval.v_string = NULL;
8661 else
8662 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008663 do_browse(save ? BROWSE_SAVE : 0,
8664 title, defname, NULL, initdir, NULL, curbuf);
8665#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008666 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008667#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008669}
8670
8671/*
8672 * "browsedir(title, initdir)" function
8673 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008676 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008677 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008678{
8679#ifdef FEAT_BROWSE
8680 char_u *title;
8681 char_u *initdir;
8682 char_u buf[NUMBUFLEN];
8683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008684 title = get_tv_string_chk(&argvars[0]);
8685 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008687 if (title == NULL || initdir == NULL)
8688 rettv->vval.v_string = NULL;
8689 else
8690 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008691 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008693 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696}
8697
Bram Moolenaar33570922005-01-25 22:26:29 +00008698static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008699
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700/*
8701 * Find a buffer by number or exact name.
8702 */
8703 static buf_T *
8704find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008705 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706{
8707 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008709 if (avar->v_type == VAR_NUMBER)
8710 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008711 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008713 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008714 if (buf == NULL)
8715 {
8716 /* No full path name match, try a match with a URL or a "nofile"
8717 * buffer, these don't use the full path. */
8718 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8719 if (buf->b_fname != NULL
8720 && (path_with_url(buf->b_fname)
8721#ifdef FEAT_QUICKFIX
8722 || bt_nofile(buf)
8723#endif
8724 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008725 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008726 break;
8727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 }
8729 return buf;
8730}
8731
8732/*
8733 * "bufexists(expr)" function
8734 */
8735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008736f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008737 typval_T *argvars;
8738 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008740 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741}
8742
8743/*
8744 * "buflisted(expr)" function
8745 */
8746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008748 typval_T *argvars;
8749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
8751 buf_T *buf;
8752
8753 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755}
8756
8757/*
8758 * "bufloaded(expr)" function
8759 */
8760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008761f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 typval_T *argvars;
8763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
8765 buf_T *buf;
8766
8767 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008768 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769}
8770
Bram Moolenaar33570922005-01-25 22:26:29 +00008771static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008772
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773/*
8774 * Get buffer by number or pattern.
8775 */
8776 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008778 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 int save_magic;
8782 char_u *save_cpo;
8783 buf_T *buf;
8784
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785 if (tv->v_type == VAR_NUMBER)
8786 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008787 if (tv->v_type != VAR_STRING)
8788 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 if (name == NULL || *name == NUL)
8790 return curbuf;
8791 if (name[0] == '$' && name[1] == NUL)
8792 return lastbuf;
8793
8794 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8795 save_magic = p_magic;
8796 p_magic = TRUE;
8797 save_cpo = p_cpo;
8798 p_cpo = (char_u *)"";
8799
8800 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8801 TRUE, FALSE));
8802
8803 p_magic = save_magic;
8804 p_cpo = save_cpo;
8805
8806 /* If not found, try expanding the name, like done for bufexists(). */
8807 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809
8810 return buf;
8811}
8812
8813/*
8814 * "bufname(expr)" function
8815 */
8816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008817f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008818 typval_T *argvars;
8819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 buf_T *buf;
8822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008823 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 buf = get_buf_tv(&argvars[0]);
8826 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008828 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 --emsg_off;
8832}
8833
8834/*
8835 * "bufnr(expr)" function
8836 */
8837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008839 typval_T *argvars;
8840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
8842 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008843 int error = FALSE;
8844 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008846 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008849 --emsg_off;
8850
8851 /* If the buffer isn't found and the second argument is not zero create a
8852 * new buffer. */
8853 if (buf == NULL
8854 && argvars[1].v_type != VAR_UNKNOWN
8855 && get_tv_number_chk(&argvars[1], &error) != 0
8856 && !error
8857 && (name = get_tv_string_chk(&argvars[0])) != NULL
8858 && !error)
8859 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8860
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008862 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865}
8866
8867/*
8868 * "bufwinnr(nr)" function
8869 */
8870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 typval_T *argvars;
8873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874{
8875#ifdef FEAT_WINDOWS
8876 win_T *wp;
8877 int winnr = 0;
8878#endif
8879 buf_T *buf;
8880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008881 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008883 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884#ifdef FEAT_WINDOWS
8885 for (wp = firstwin; wp; wp = wp->w_next)
8886 {
8887 ++winnr;
8888 if (wp->w_buffer == buf)
8889 break;
8890 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894#endif
8895 --emsg_off;
8896}
8897
8898/*
8899 * "byte2line(byte)" function
8900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008903 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
8906#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908#else
8909 long boff = 0;
8910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008915 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 (linenr_T)0, &boff);
8917#endif
8918}
8919
8920/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008921 * "byteidx()" function
8922 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008925 typval_T *argvars;
8926 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008927{
8928#ifdef FEAT_MBYTE
8929 char_u *t;
8930#endif
8931 char_u *str;
8932 long idx;
8933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 str = get_tv_string_chk(&argvars[0]);
8935 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008937 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008938 return;
8939
8940#ifdef FEAT_MBYTE
8941 t = str;
8942 for ( ; idx > 0; idx--)
8943 {
8944 if (*t == NUL) /* EOL reached */
8945 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008946 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008947 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008948 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008949#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008950 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008951 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008952#endif
8953}
8954
8955/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008956 * "call(func, arglist)" function
8957 */
8958 static void
8959f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008960 typval_T *argvars;
8961 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008962{
8963 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008964 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008965 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008966 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008967 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008969
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008970 if (argvars[1].v_type != VAR_LIST)
8971 {
8972 EMSG(_(e_listreq));
8973 return;
8974 }
8975 if (argvars[1].vval.v_list == NULL)
8976 return;
8977
8978 if (argvars[0].v_type == VAR_FUNC)
8979 func = argvars[0].vval.v_string;
8980 else
8981 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008982 if (*func == NUL)
8983 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008984
Bram Moolenaare9a41262005-01-15 22:18:47 +00008985 if (argvars[2].v_type != VAR_UNKNOWN)
8986 {
8987 if (argvars[2].v_type != VAR_DICT)
8988 {
8989 EMSG(_(e_dictreq));
8990 return;
8991 }
8992 selfdict = argvars[2].vval.v_dict;
8993 }
8994
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008995 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
8996 item = item->li_next)
8997 {
8998 if (argc == MAX_FUNC_ARGS)
8999 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009000 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009001 break;
9002 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009003 /* Make a copy of each argument. This is needed to be able to set
9004 * v_lock to VAR_FIXED in the copy without changing the original list.
9005 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009006 copy_tv(&item->li_tv, &argv[argc++]);
9007 }
9008
9009 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009010 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009011 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9012 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009013
9014 /* Free the arguments. */
9015 while (argc > 0)
9016 clear_tv(&argv[--argc]);
9017}
9018
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009019#ifdef FEAT_FLOAT
9020/*
9021 * "ceil({float})" function
9022 */
9023 static void
9024f_ceil(argvars, rettv)
9025 typval_T *argvars;
9026 typval_T *rettv;
9027{
9028 float_T f;
9029
9030 rettv->v_type = VAR_FLOAT;
9031 if (get_float_arg(argvars, &f) == OK)
9032 rettv->vval.v_float = ceil(f);
9033 else
9034 rettv->vval.v_float = 0.0;
9035}
9036#endif
9037
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009038/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009039 * "changenr()" function
9040 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009041 static void
9042f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009043 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009044 typval_T *rettv;
9045{
9046 rettv->vval.v_number = curbuf->b_u_seq_cur;
9047}
9048
9049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 * "char2nr(string)" function
9051 */
9052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009053f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009054 typval_T *argvars;
9055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
9057#ifdef FEAT_MBYTE
9058 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009059 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 else
9061#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063}
9064
9065/*
9066 * "cindent(lnum)" function
9067 */
9068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009069f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009070 typval_T *argvars;
9071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072{
9073#ifdef FEAT_CINDENT
9074 pos_T pos;
9075 linenr_T lnum;
9076
9077 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9080 {
9081 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 curwin->w_cursor = pos;
9084 }
9085 else
9086#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088}
9089
9090/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009091 * "clearmatches()" function
9092 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009093 static void
9094f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009095 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009096 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009097{
9098#ifdef FEAT_SEARCH_EXTRA
9099 clear_matches(curwin);
9100#endif
9101}
9102
9103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 * "col(string)" function
9105 */
9106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009108 typval_T *argvars;
9109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110{
9111 colnr_T col = 0;
9112 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009113 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009115 fp = var2fpos(&argvars[0], FALSE, &fnum);
9116 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 {
9118 if (fp->col == MAXCOL)
9119 {
9120 /* '> can be MAXCOL, get the length of the line then */
9121 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009122 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 else
9124 col = MAXCOL;
9125 }
9126 else
9127 {
9128 col = fp->col + 1;
9129#ifdef FEAT_VIRTUALEDIT
9130 /* col(".") when the cursor is on the NUL at the end of the line
9131 * because of "coladd" can be seen as an extra column. */
9132 if (virtual_active() && fp == &curwin->w_cursor)
9133 {
9134 char_u *p = ml_get_cursor();
9135
9136 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9137 curwin->w_virtcol - curwin->w_cursor.coladd))
9138 {
9139# ifdef FEAT_MBYTE
9140 int l;
9141
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009142 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 col += l;
9144# else
9145 if (*p != NUL && p[1] == NUL)
9146 ++col;
9147# endif
9148 }
9149 }
9150#endif
9151 }
9152 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154}
9155
Bram Moolenaar572cb562005-08-05 21:35:02 +00009156#if defined(FEAT_INS_EXPAND)
9157/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009158 * "complete()" function
9159 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009160 static void
9161f_complete(argvars, rettv)
9162 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009163 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009164{
9165 int startcol;
9166
9167 if ((State & INSERT) == 0)
9168 {
9169 EMSG(_("E785: complete() can only be used in Insert mode"));
9170 return;
9171 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009172
9173 /* Check for undo allowed here, because if something was already inserted
9174 * the line was already saved for undo and this check isn't done. */
9175 if (!undo_allowed())
9176 return;
9177
Bram Moolenaarade00832006-03-10 21:46:58 +00009178 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9179 {
9180 EMSG(_(e_invarg));
9181 return;
9182 }
9183
9184 startcol = get_tv_number_chk(&argvars[0], NULL);
9185 if (startcol <= 0)
9186 return;
9187
9188 set_completion(startcol - 1, argvars[1].vval.v_list);
9189}
9190
9191/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009192 * "complete_add()" function
9193 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009194 static void
9195f_complete_add(argvars, rettv)
9196 typval_T *argvars;
9197 typval_T *rettv;
9198{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009199 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009200}
9201
9202/*
9203 * "complete_check()" function
9204 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009205 static void
9206f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009207 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009208 typval_T *rettv;
9209{
9210 int saved = RedrawingDisabled;
9211
9212 RedrawingDisabled = 0;
9213 ins_compl_check_keys(0);
9214 rettv->vval.v_number = compl_interrupted;
9215 RedrawingDisabled = saved;
9216}
9217#endif
9218
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219/*
9220 * "confirm(message, buttons[, default [, type]])" function
9221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009223f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009224 typval_T *argvars UNUSED;
9225 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226{
9227#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9228 char_u *message;
9229 char_u *buttons = NULL;
9230 char_u buf[NUMBUFLEN];
9231 char_u buf2[NUMBUFLEN];
9232 int def = 1;
9233 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009234 char_u *typestr;
9235 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 message = get_tv_string_chk(&argvars[0]);
9238 if (message == NULL)
9239 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009240 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9243 if (buttons == NULL)
9244 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009245 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009247 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009248 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9251 if (typestr == NULL)
9252 error = TRUE;
9253 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255 switch (TOUPPER_ASC(*typestr))
9256 {
9257 case 'E': type = VIM_ERROR; break;
9258 case 'Q': type = VIM_QUESTION; break;
9259 case 'I': type = VIM_INFO; break;
9260 case 'W': type = VIM_WARNING; break;
9261 case 'G': type = VIM_GENERIC; break;
9262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 }
9264 }
9265 }
9266 }
9267
9268 if (buttons == NULL || *buttons == NUL)
9269 buttons = (char_u *)_("&Ok");
9270
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009271 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009272 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274#endif
9275}
9276
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009277/*
9278 * "copy()" function
9279 */
9280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009281f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009282 typval_T *argvars;
9283 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009284{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009285 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009286}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009288#ifdef FEAT_FLOAT
9289/*
9290 * "cos()" function
9291 */
9292 static void
9293f_cos(argvars, rettv)
9294 typval_T *argvars;
9295 typval_T *rettv;
9296{
9297 float_T f;
9298
9299 rettv->v_type = VAR_FLOAT;
9300 if (get_float_arg(argvars, &f) == OK)
9301 rettv->vval.v_float = cos(f);
9302 else
9303 rettv->vval.v_float = 0.0;
9304}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009305
9306/*
9307 * "cosh()" function
9308 */
9309 static void
9310f_cosh(argvars, rettv)
9311 typval_T *argvars;
9312 typval_T *rettv;
9313{
9314 float_T f;
9315
9316 rettv->v_type = VAR_FLOAT;
9317 if (get_float_arg(argvars, &f) == OK)
9318 rettv->vval.v_float = cosh(f);
9319 else
9320 rettv->vval.v_float = 0.0;
9321}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009322#endif
9323
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009325 * "count()" function
9326 */
9327 static void
9328f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009329 typval_T *argvars;
9330 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009331{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009332 long n = 0;
9333 int ic = FALSE;
9334
Bram Moolenaare9a41262005-01-15 22:18:47 +00009335 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009336 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009337 listitem_T *li;
9338 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009339 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009340
Bram Moolenaare9a41262005-01-15 22:18:47 +00009341 if ((l = argvars[0].vval.v_list) != NULL)
9342 {
9343 li = l->lv_first;
9344 if (argvars[2].v_type != VAR_UNKNOWN)
9345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009346 int error = FALSE;
9347
9348 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009349 if (argvars[3].v_type != VAR_UNKNOWN)
9350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009351 idx = get_tv_number_chk(&argvars[3], &error);
9352 if (!error)
9353 {
9354 li = list_find(l, idx);
9355 if (li == NULL)
9356 EMSGN(_(e_listidx), idx);
9357 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009358 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009359 if (error)
9360 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009361 }
9362
9363 for ( ; li != NULL; li = li->li_next)
9364 if (tv_equal(&li->li_tv, &argvars[1], ic))
9365 ++n;
9366 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009367 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009368 else if (argvars[0].v_type == VAR_DICT)
9369 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009370 int todo;
9371 dict_T *d;
9372 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009373
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009374 if ((d = argvars[0].vval.v_dict) != NULL)
9375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009376 int error = FALSE;
9377
Bram Moolenaare9a41262005-01-15 22:18:47 +00009378 if (argvars[2].v_type != VAR_UNKNOWN)
9379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009380 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009381 if (argvars[3].v_type != VAR_UNKNOWN)
9382 EMSG(_(e_invarg));
9383 }
9384
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009385 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009386 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009387 {
9388 if (!HASHITEM_EMPTY(hi))
9389 {
9390 --todo;
9391 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9392 ++n;
9393 }
9394 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009395 }
9396 }
9397 else
9398 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009399 rettv->vval.v_number = n;
9400}
9401
9402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9404 *
9405 * Checks the existence of a cscope connection.
9406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009409 typval_T *argvars UNUSED;
9410 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411{
9412#ifdef FEAT_CSCOPE
9413 int num = 0;
9414 char_u *dbpath = NULL;
9415 char_u *prepend = NULL;
9416 char_u buf[NUMBUFLEN];
9417
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009418 if (argvars[0].v_type != VAR_UNKNOWN
9419 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421 num = (int)get_tv_number(&argvars[0]);
9422 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009423 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 }
9426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009427 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428#endif
9429}
9430
9431/*
9432 * "cursor(lnum, col)" function
9433 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009434 * Moves the cursor to the specified line and column.
9435 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009439 typval_T *argvars;
9440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441{
9442 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009443#ifdef FEAT_VIRTUALEDIT
9444 long coladd = 0;
9445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009447 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009448 if (argvars[1].v_type == VAR_UNKNOWN)
9449 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009450 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009451
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009452 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009453 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009454 line = pos.lnum;
9455 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009456#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009457 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009458#endif
9459 }
9460 else
9461 {
9462 line = get_tv_lnum(argvars);
9463 col = get_tv_number_chk(&argvars[1], NULL);
9464#ifdef FEAT_VIRTUALEDIT
9465 if (argvars[2].v_type != VAR_UNKNOWN)
9466 coladd = get_tv_number_chk(&argvars[2], NULL);
9467#endif
9468 }
9469 if (line < 0 || col < 0
9470#ifdef FEAT_VIRTUALEDIT
9471 || coladd < 0
9472#endif
9473 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 if (line > 0)
9476 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 if (col > 0)
9478 curwin->w_cursor.col = col - 1;
9479#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009480 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481#endif
9482
9483 /* Make sure the cursor is in a valid position. */
9484 check_cursor();
9485#ifdef FEAT_MBYTE
9486 /* Correct cursor for multi-byte character. */
9487 if (has_mbyte)
9488 mb_adjust_cursor();
9489#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009490
9491 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009492 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493}
9494
9495/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009496 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 */
9498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009500 typval_T *argvars;
9501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009503 int noref = 0;
9504
9505 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009506 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009507 if (noref < 0 || noref > 1)
9508 EMSG(_(e_invarg));
9509 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009510 {
9511 current_copyID += COPYID_INC;
9512 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514}
9515
9516/*
9517 * "delete()" function
9518 */
9519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009520f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009521 typval_T *argvars;
9522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
9524 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528}
9529
9530/*
9531 * "did_filetype()" function
9532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009535 typval_T *argvars UNUSED;
9536 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537{
9538#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009539 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540#endif
9541}
9542
9543/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009544 * "diff_filler()" function
9545 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009548 typval_T *argvars UNUSED;
9549 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009550{
9551#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009553#endif
9554}
9555
9556/*
9557 * "diff_hlID()" function
9558 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009561 typval_T *argvars UNUSED;
9562 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009563{
9564#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009566 static linenr_T prev_lnum = 0;
9567 static int changedtick = 0;
9568 static int fnum = 0;
9569 static int change_start = 0;
9570 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009571 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009572 int filler_lines;
9573 int col;
9574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575 if (lnum < 0) /* ignore type error in {lnum} arg */
9576 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009577 if (lnum != prev_lnum
9578 || changedtick != curbuf->b_changedtick
9579 || fnum != curbuf->b_fnum)
9580 {
9581 /* New line, buffer, change: need to get the values. */
9582 filler_lines = diff_check(curwin, lnum);
9583 if (filler_lines < 0)
9584 {
9585 if (filler_lines == -1)
9586 {
9587 change_start = MAXCOL;
9588 change_end = -1;
9589 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9590 hlID = HLF_ADD; /* added line */
9591 else
9592 hlID = HLF_CHD; /* changed line */
9593 }
9594 else
9595 hlID = HLF_ADD; /* added line */
9596 }
9597 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009598 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009599 prev_lnum = lnum;
9600 changedtick = curbuf->b_changedtick;
9601 fnum = curbuf->b_fnum;
9602 }
9603
9604 if (hlID == HLF_CHD || hlID == HLF_TXD)
9605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009606 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009607 if (col >= change_start && col <= change_end)
9608 hlID = HLF_TXD; /* changed text */
9609 else
9610 hlID = HLF_CHD; /* changed line */
9611 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009612 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009613#endif
9614}
9615
9616/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009617 * "empty({expr})" function
9618 */
9619 static void
9620f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 typval_T *argvars;
9622 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009623{
9624 int n;
9625
9626 switch (argvars[0].v_type)
9627 {
9628 case VAR_STRING:
9629 case VAR_FUNC:
9630 n = argvars[0].vval.v_string == NULL
9631 || *argvars[0].vval.v_string == NUL;
9632 break;
9633 case VAR_NUMBER:
9634 n = argvars[0].vval.v_number == 0;
9635 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009636#ifdef FEAT_FLOAT
9637 case VAR_FLOAT:
9638 n = argvars[0].vval.v_float == 0.0;
9639 break;
9640#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009641 case VAR_LIST:
9642 n = argvars[0].vval.v_list == NULL
9643 || argvars[0].vval.v_list->lv_first == NULL;
9644 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 case VAR_DICT:
9646 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009647 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009648 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009649 default:
9650 EMSG2(_(e_intern2), "f_empty()");
9651 n = 0;
9652 }
9653
9654 rettv->vval.v_number = n;
9655}
9656
9657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 * "escape({string}, {chars})" function
9659 */
9660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009662 typval_T *argvars;
9663 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665 char_u buf[NUMBUFLEN];
9666
Bram Moolenaar758711c2005-02-02 23:11:38 +00009667 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9668 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670}
9671
9672/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009673 * "eval()" function
9674 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009675 static void
9676f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009677 typval_T *argvars;
9678 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009679{
9680 char_u *s;
9681
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009682 s = get_tv_string_chk(&argvars[0]);
9683 if (s != NULL)
9684 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009685
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009686 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9687 {
9688 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009689 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009690 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009691 else if (*s != NUL)
9692 EMSG(_(e_trailing));
9693}
9694
9695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 * "eventhandler()" function
9697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009700 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704}
9705
9706/*
9707 * "executable()" function
9708 */
9709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009711 typval_T *argvars;
9712 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715}
9716
9717/*
9718 * "exists()" function
9719 */
9720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009722 typval_T *argvars;
9723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725 char_u *p;
9726 char_u *name;
9727 int n = FALSE;
9728 int len = 0;
9729
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009730 no_autoload = TRUE;
9731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 if (*p == '$') /* environment variable */
9734 {
9735 /* first try "normal" environment variables (fast) */
9736 if (mch_getenv(p + 1) != NULL)
9737 n = TRUE;
9738 else
9739 {
9740 /* try expanding things like $VIM and ${HOME} */
9741 p = expand_env_save(p);
9742 if (p != NULL && *p != '$')
9743 n = TRUE;
9744 vim_free(p);
9745 }
9746 }
9747 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009750 if (*skipwhite(p) != NUL)
9751 n = FALSE; /* trailing garbage */
9752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 else if (*p == '*') /* internal or user defined function */
9754 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009755 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 }
9757 else if (*p == ':')
9758 {
9759 n = cmd_exists(p + 1);
9760 }
9761 else if (*p == '#')
9762 {
9763#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009764 if (p[1] == '#')
9765 n = autocmd_supported(p + 2);
9766 else
9767 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768#endif
9769 }
9770 else /* internal variable */
9771 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009772 char_u *tofree;
9773 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009775 /* get_name_len() takes care of expanding curly braces */
9776 name = p;
9777 len = get_name_len(&p, &tofree, TRUE, FALSE);
9778 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009780 if (tofree != NULL)
9781 name = tofree;
9782 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9783 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009785 /* handle d.key, l[idx], f(expr) */
9786 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9787 if (n)
9788 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 }
9790 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009791 if (*p != NUL)
9792 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009794 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 }
9796
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009798
9799 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800}
9801
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009802#ifdef FEAT_FLOAT
9803/*
9804 * "exp()" function
9805 */
9806 static void
9807f_exp(argvars, rettv)
9808 typval_T *argvars;
9809 typval_T *rettv;
9810{
9811 float_T f;
9812
9813 rettv->v_type = VAR_FLOAT;
9814 if (get_float_arg(argvars, &f) == OK)
9815 rettv->vval.v_float = exp(f);
9816 else
9817 rettv->vval.v_float = 0.0;
9818}
9819#endif
9820
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821/*
9822 * "expand()" function
9823 */
9824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009826 typval_T *argvars;
9827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828{
9829 char_u *s;
9830 int len;
9831 char_u *errormsg;
9832 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9833 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009834 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 rettv->v_type = VAR_STRING;
9837 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 if (*s == '%' || *s == '#' || *s == '<')
9839 {
9840 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009841 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 --emsg_off;
9843 }
9844 else
9845 {
9846 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009847 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009848 if (argvars[1].v_type != VAR_UNKNOWN
9849 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009851 if (!error)
9852 {
9853 ExpandInit(&xpc);
9854 xpc.xp_context = EXPAND_FILES;
9855 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 }
9857 else
9858 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 }
9860}
9861
9862/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009863 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009864 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009865 */
9866 static void
9867f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009868 typval_T *argvars;
9869 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009870{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009871 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009872 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009873 list_T *l1, *l2;
9874 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009875 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009876 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009877
Bram Moolenaare9a41262005-01-15 22:18:47 +00009878 l1 = argvars[0].vval.v_list;
9879 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009880 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9881 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009882 {
9883 if (argvars[2].v_type != VAR_UNKNOWN)
9884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009885 before = get_tv_number_chk(&argvars[2], &error);
9886 if (error)
9887 return; /* type error; errmsg already given */
9888
Bram Moolenaar758711c2005-02-02 23:11:38 +00009889 if (before == l1->lv_len)
9890 item = NULL;
9891 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009892 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009893 item = list_find(l1, before);
9894 if (item == NULL)
9895 {
9896 EMSGN(_(e_listidx), before);
9897 return;
9898 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009899 }
9900 }
9901 else
9902 item = NULL;
9903 list_extend(l1, l2, item);
9904
Bram Moolenaare9a41262005-01-15 22:18:47 +00009905 copy_tv(&argvars[0], rettv);
9906 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009907 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009908 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9909 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009910 dict_T *d1, *d2;
9911 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009912 char_u *action;
9913 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009914 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009915 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009916
9917 d1 = argvars[0].vval.v_dict;
9918 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009919 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9920 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 {
9922 /* Check the third argument. */
9923 if (argvars[2].v_type != VAR_UNKNOWN)
9924 {
9925 static char *(av[]) = {"keep", "force", "error"};
9926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009927 action = get_tv_string_chk(&argvars[2]);
9928 if (action == NULL)
9929 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009930 for (i = 0; i < 3; ++i)
9931 if (STRCMP(action, av[i]) == 0)
9932 break;
9933 if (i == 3)
9934 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009935 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009936 return;
9937 }
9938 }
9939 else
9940 action = (char_u *)"force";
9941
9942 /* Go over all entries in the second dict and add them to the
9943 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009944 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009945 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009946 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009947 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009948 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009949 --todo;
9950 di1 = dict_find(d1, hi2->hi_key, -1);
9951 if (di1 == NULL)
9952 {
9953 di1 = dictitem_copy(HI2DI(hi2));
9954 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9955 dictitem_free(di1);
9956 }
9957 else if (*action == 'e')
9958 {
9959 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9960 break;
9961 }
9962 else if (*action == 'f')
9963 {
9964 clear_tv(&di1->di_tv);
9965 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9966 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009967 }
9968 }
9969
Bram Moolenaare9a41262005-01-15 22:18:47 +00009970 copy_tv(&argvars[0], rettv);
9971 }
9972 }
9973 else
9974 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009975}
9976
9977/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009978 * "feedkeys()" function
9979 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009980 static void
9981f_feedkeys(argvars, rettv)
9982 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009983 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009984{
9985 int remap = TRUE;
9986 char_u *keys, *flags;
9987 char_u nbuf[NUMBUFLEN];
9988 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009989 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009990
Bram Moolenaar3d43a662007-04-27 20:15:55 +00009991 /* This is not allowed in the sandbox. If the commands would still be
9992 * executed in the sandbox it would be OK, but it probably happens later,
9993 * when "sandbox" is no longer set. */
9994 if (check_secure())
9995 return;
9996
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00009997 keys = get_tv_string(&argvars[0]);
9998 if (*keys != NUL)
9999 {
10000 if (argvars[1].v_type != VAR_UNKNOWN)
10001 {
10002 flags = get_tv_string_buf(&argvars[1], nbuf);
10003 for ( ; *flags != NUL; ++flags)
10004 {
10005 switch (*flags)
10006 {
10007 case 'n': remap = FALSE; break;
10008 case 'm': remap = TRUE; break;
10009 case 't': typed = TRUE; break;
10010 }
10011 }
10012 }
10013
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010014 /* Need to escape K_SPECIAL and CSI before putting the string in the
10015 * typeahead buffer. */
10016 keys_esc = vim_strsave_escape_csi(keys);
10017 if (keys_esc != NULL)
10018 {
10019 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010020 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010021 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010022 if (vgetc_busy)
10023 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010024 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010025 }
10026}
10027
10028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 * "filereadable()" function
10030 */
10031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010033 typval_T *argvars;
10034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010036 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 char_u *p;
10038 int n;
10039
Bram Moolenaarc236c162008-07-13 17:41:49 +000010040#ifndef O_NONBLOCK
10041# define O_NONBLOCK 0
10042#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010044 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10045 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 {
10047 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010048 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 }
10050 else
10051 n = FALSE;
10052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010053 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054}
10055
10056/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010057 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 * rights to write into.
10059 */
10060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010065 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066}
10067
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010068static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010069
10070 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010071findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010072 typval_T *argvars;
10073 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010074 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010075{
10076#ifdef FEAT_SEARCHPATH
10077 char_u *fname;
10078 char_u *fresult = NULL;
10079 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10080 char_u *p;
10081 char_u pathbuf[NUMBUFLEN];
10082 int count = 1;
10083 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010084 int error = FALSE;
10085#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010086
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010087 rettv->vval.v_string = NULL;
10088 rettv->v_type = VAR_STRING;
10089
10090#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010092
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010093 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010095 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10096 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010097 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 else
10099 {
10100 if (*p != NUL)
10101 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010103 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010104 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010105 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010106 }
10107
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010108 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10109 error = TRUE;
10110
10111 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010113 do
10114 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010115 if (rettv->v_type == VAR_STRING)
10116 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010117 fresult = find_file_in_path_option(first ? fname : NULL,
10118 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010119 0, first, path,
10120 find_what,
10121 curbuf->b_ffname,
10122 find_what == FINDFILE_DIR
10123 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010124 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010125
10126 if (fresult != NULL && rettv->v_type == VAR_LIST)
10127 list_append_string(rettv->vval.v_list, fresult, -1);
10128
10129 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010130 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010131
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010132 if (rettv->v_type == VAR_STRING)
10133 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010134#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010135}
10136
Bram Moolenaar33570922005-01-25 22:26:29 +000010137static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10138static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010139
10140/*
10141 * Implementation of map() and filter().
10142 */
10143 static void
10144filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010145 typval_T *argvars;
10146 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010147 int map;
10148{
10149 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010150 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010151 listitem_T *li, *nli;
10152 list_T *l = NULL;
10153 dictitem_T *di;
10154 hashtab_T *ht;
10155 hashitem_T *hi;
10156 dict_T *d = NULL;
10157 typval_T save_val;
10158 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010159 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010160 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010161 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010162 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010163 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010164
Bram Moolenaare9a41262005-01-15 22:18:47 +000010165 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010166 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010167 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010168 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010169 return;
10170 }
10171 else if (argvars[0].v_type == VAR_DICT)
10172 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010173 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010174 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010175 return;
10176 }
10177 else
10178 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010179 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010180 return;
10181 }
10182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010183 expr = get_tv_string_buf_chk(&argvars[1], buf);
10184 /* On type errors, the preceding call has already displayed an error
10185 * message. Avoid a misleading error message for an empty string that
10186 * was not passed as argument. */
10187 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 prepare_vimvar(VV_VAL, &save_val);
10190 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010191
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010192 /* We reset "did_emsg" to be able to detect whether an error
10193 * occurred during evaluation of the expression. */
10194 save_did_emsg = did_emsg;
10195 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010196
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010197 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010200 vimvars[VV_KEY].vv_type = VAR_STRING;
10201
10202 ht = &d->dv_hashtab;
10203 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010204 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010205 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010207 if (!HASHITEM_EMPTY(hi))
10208 {
10209 --todo;
10210 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010211 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 break;
10213 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010214 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010215 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010216 break;
10217 if (!map && rem)
10218 dictitem_remove(d, di);
10219 clear_tv(&vimvars[VV_KEY].vv_tv);
10220 }
10221 }
10222 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 }
10224 else
10225 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010226 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10227
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 for (li = l->lv_first; li != NULL; li = nli)
10229 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010230 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010231 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010232 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010233 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010234 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010235 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010236 break;
10237 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010238 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010239 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010240 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010241 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010242
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010243 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010244 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010245
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010246 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010247 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248
10249 copy_tv(&argvars[0], rettv);
10250}
10251
10252 static int
10253filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010254 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010255 char_u *expr;
10256 int map;
10257 int *remp;
10258{
Bram Moolenaar33570922005-01-25 22:26:29 +000010259 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010260 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010261 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010262
Bram Moolenaar33570922005-01-25 22:26:29 +000010263 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010264 s = expr;
10265 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010266 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010267 if (*s != NUL) /* check for trailing chars after expr */
10268 {
10269 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010270 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271 }
10272 if (map)
10273 {
10274 /* map(): replace the list item value */
10275 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010276 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 *tv = rettv;
10278 }
10279 else
10280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010281 int error = FALSE;
10282
Bram Moolenaare9a41262005-01-15 22:18:47 +000010283 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010284 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010285 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010286 /* On type error, nothing has been removed; return FAIL to stop the
10287 * loop. The error message was given by get_tv_number_chk(). */
10288 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010289 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010290 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010291 retval = OK;
10292theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010294 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010295}
10296
10297/*
10298 * "filter()" function
10299 */
10300 static void
10301f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010302 typval_T *argvars;
10303 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010304{
10305 filter_map(argvars, rettv, FALSE);
10306}
10307
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010308/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010309 * "finddir({fname}[, {path}[, {count}]])" function
10310 */
10311 static void
10312f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010313 typval_T *argvars;
10314 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010315{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010316 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010317}
10318
10319/*
10320 * "findfile({fname}[, {path}[, {count}]])" function
10321 */
10322 static void
10323f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010324 typval_T *argvars;
10325 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010326{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010327 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010328}
10329
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010330#ifdef FEAT_FLOAT
10331/*
10332 * "float2nr({float})" function
10333 */
10334 static void
10335f_float2nr(argvars, rettv)
10336 typval_T *argvars;
10337 typval_T *rettv;
10338{
10339 float_T f;
10340
10341 if (get_float_arg(argvars, &f) == OK)
10342 {
10343 if (f < -0x7fffffff)
10344 rettv->vval.v_number = -0x7fffffff;
10345 else if (f > 0x7fffffff)
10346 rettv->vval.v_number = 0x7fffffff;
10347 else
10348 rettv->vval.v_number = (varnumber_T)f;
10349 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010350}
10351
10352/*
10353 * "floor({float})" function
10354 */
10355 static void
10356f_floor(argvars, rettv)
10357 typval_T *argvars;
10358 typval_T *rettv;
10359{
10360 float_T f;
10361
10362 rettv->v_type = VAR_FLOAT;
10363 if (get_float_arg(argvars, &f) == OK)
10364 rettv->vval.v_float = floor(f);
10365 else
10366 rettv->vval.v_float = 0.0;
10367}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010368
10369/*
10370 * "fmod()" function
10371 */
10372 static void
10373f_fmod(argvars, rettv)
10374 typval_T *argvars;
10375 typval_T *rettv;
10376{
10377 float_T fx, fy;
10378
10379 rettv->v_type = VAR_FLOAT;
10380 if (get_float_arg(argvars, &fx) == OK
10381 && get_float_arg(&argvars[1], &fy) == OK)
10382 rettv->vval.v_float = fmod(fx, fy);
10383 else
10384 rettv->vval.v_float = 0.0;
10385}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010386#endif
10387
Bram Moolenaar0d660222005-01-07 21:51:51 +000010388/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010389 * "fnameescape({string})" function
10390 */
10391 static void
10392f_fnameescape(argvars, rettv)
10393 typval_T *argvars;
10394 typval_T *rettv;
10395{
10396 rettv->vval.v_string = vim_strsave_fnameescape(
10397 get_tv_string(&argvars[0]), FALSE);
10398 rettv->v_type = VAR_STRING;
10399}
10400
10401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 * "fnamemodify({fname}, {mods})" function
10403 */
10404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 typval_T *argvars;
10407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
10409 char_u *fname;
10410 char_u *mods;
10411 int usedlen = 0;
10412 int len;
10413 char_u *fbuf = NULL;
10414 char_u buf[NUMBUFLEN];
10415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010416 fname = get_tv_string_chk(&argvars[0]);
10417 mods = get_tv_string_buf_chk(&argvars[1], buf);
10418 if (fname == NULL || mods == NULL)
10419 fname = NULL;
10420 else
10421 {
10422 len = (int)STRLEN(fname);
10423 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010426 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010428 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010430 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 vim_free(fbuf);
10432}
10433
Bram Moolenaar33570922005-01-25 22:26:29 +000010434static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435
10436/*
10437 * "foldclosed()" function
10438 */
10439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010440foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010441 typval_T *argvars;
10442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 int end;
10444{
10445#ifdef FEAT_FOLDING
10446 linenr_T lnum;
10447 linenr_T first, last;
10448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010449 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10451 {
10452 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10453 {
10454 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 return;
10459 }
10460 }
10461#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463}
10464
10465/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010466 * "foldclosed()" function
10467 */
10468 static void
10469f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010470 typval_T *argvars;
10471 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010472{
10473 foldclosed_both(argvars, rettv, FALSE);
10474}
10475
10476/*
10477 * "foldclosedend()" function
10478 */
10479 static void
10480f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010481 typval_T *argvars;
10482 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010483{
10484 foldclosed_both(argvars, rettv, TRUE);
10485}
10486
10487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 * "foldlevel()" function
10489 */
10490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010491f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010492 typval_T *argvars;
10493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494{
10495#ifdef FEAT_FOLDING
10496 linenr_T lnum;
10497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010498 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502}
10503
10504/*
10505 * "foldtext()" function
10506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010508f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010509 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511{
10512#ifdef FEAT_FOLDING
10513 linenr_T lnum;
10514 char_u *s;
10515 char_u *r;
10516 int len;
10517 char *txt;
10518#endif
10519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520 rettv->v_type = VAR_STRING;
10521 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010523 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10524 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10525 <= curbuf->b_ml.ml_line_count
10526 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 {
10528 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010529 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10530 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 {
10532 if (!linewhite(lnum))
10533 break;
10534 ++lnum;
10535 }
10536
10537 /* Find interesting text in this line. */
10538 s = skipwhite(ml_get(lnum));
10539 /* skip C comment-start */
10540 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010543 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010544 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010545 {
10546 s = skipwhite(ml_get(lnum + 1));
10547 if (*s == '*')
10548 s = skipwhite(s + 1);
10549 }
10550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 txt = _("+-%s%3ld lines: ");
10552 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010553 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 + 20 /* for %3ld */
10555 + STRLEN(s))); /* concatenated */
10556 if (r != NULL)
10557 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010558 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10559 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10560 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 len = (int)STRLEN(r);
10562 STRCAT(r, s);
10563 /* remove 'foldmarker' and 'commentstring' */
10564 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 }
10567 }
10568#endif
10569}
10570
10571/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010572 * "foldtextresult(lnum)" function
10573 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010576 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010577 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010578{
10579#ifdef FEAT_FOLDING
10580 linenr_T lnum;
10581 char_u *text;
10582 char_u buf[51];
10583 foldinfo_T foldinfo;
10584 int fold_count;
10585#endif
10586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010587 rettv->v_type = VAR_STRING;
10588 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010589#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010590 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010591 /* treat illegal types and illegal string values for {lnum} the same */
10592 if (lnum < 0)
10593 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010594 fold_count = foldedCount(curwin, lnum, &foldinfo);
10595 if (fold_count > 0)
10596 {
10597 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10598 &foldinfo, buf);
10599 if (text == buf)
10600 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010601 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010602 }
10603#endif
10604}
10605
10606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 * "foreground()" function
10608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010610f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010611 typval_T *argvars UNUSED;
10612 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614#ifdef FEAT_GUI
10615 if (gui.in_use)
10616 gui_mch_set_foreground();
10617#else
10618# ifdef WIN32
10619 win32_set_foreground();
10620# endif
10621#endif
10622}
10623
10624/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010625 * "function()" function
10626 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010628f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010629 typval_T *argvars;
10630 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010631{
10632 char_u *s;
10633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010635 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010636 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010637 /* Don't check an autoload name for existence here. */
10638 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010639 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010640 else
10641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010642 rettv->vval.v_string = vim_strsave(s);
10643 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010644 }
10645}
10646
10647/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010648 * "garbagecollect()" function
10649 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010650 static void
10651f_garbagecollect(argvars, rettv)
10652 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010653 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010654{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010655 /* This is postponed until we are back at the toplevel, because we may be
10656 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10657 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010658
10659 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10660 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010661}
10662
10663/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010664 * "get()" function
10665 */
10666 static void
10667f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010668 typval_T *argvars;
10669 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010670{
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 listitem_T *li;
10672 list_T *l;
10673 dictitem_T *di;
10674 dict_T *d;
10675 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010676
Bram Moolenaare9a41262005-01-15 22:18:47 +000010677 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010678 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010679 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010681 int error = FALSE;
10682
10683 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10684 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010685 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010686 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010687 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010688 else if (argvars[0].v_type == VAR_DICT)
10689 {
10690 if ((d = argvars[0].vval.v_dict) != NULL)
10691 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010692 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010693 if (di != NULL)
10694 tv = &di->di_tv;
10695 }
10696 }
10697 else
10698 EMSG2(_(e_listdictarg), "get()");
10699
10700 if (tv == NULL)
10701 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010702 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010703 copy_tv(&argvars[2], rettv);
10704 }
10705 else
10706 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010707}
10708
Bram Moolenaar342337a2005-07-21 21:11:17 +000010709static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010710
10711/*
10712 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010713 * Return a range (from start to end) of lines in rettv from the specified
10714 * buffer.
10715 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010716 */
10717 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010718get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010719 buf_T *buf;
10720 linenr_T start;
10721 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010722 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010723 typval_T *rettv;
10724{
10725 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010726
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010727 if (retlist && rettv_list_alloc(rettv) == FAIL)
10728 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010729
10730 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10731 return;
10732
10733 if (!retlist)
10734 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010735 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10736 p = ml_get_buf(buf, start, FALSE);
10737 else
10738 p = (char_u *)"";
10739
10740 rettv->v_type = VAR_STRING;
10741 rettv->vval.v_string = vim_strsave(p);
10742 }
10743 else
10744 {
10745 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010746 return;
10747
10748 if (start < 1)
10749 start = 1;
10750 if (end > buf->b_ml.ml_line_count)
10751 end = buf->b_ml.ml_line_count;
10752 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010753 if (list_append_string(rettv->vval.v_list,
10754 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010755 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010756 }
10757}
10758
10759/*
10760 * "getbufline()" function
10761 */
10762 static void
10763f_getbufline(argvars, rettv)
10764 typval_T *argvars;
10765 typval_T *rettv;
10766{
10767 linenr_T lnum;
10768 linenr_T end;
10769 buf_T *buf;
10770
10771 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10772 ++emsg_off;
10773 buf = get_buf_tv(&argvars[0]);
10774 --emsg_off;
10775
Bram Moolenaar661b1822005-07-28 22:36:45 +000010776 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010777 if (argvars[2].v_type == VAR_UNKNOWN)
10778 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010779 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010780 end = get_tv_lnum_buf(&argvars[2], buf);
10781
Bram Moolenaar342337a2005-07-21 21:11:17 +000010782 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010783}
10784
Bram Moolenaar0d660222005-01-07 21:51:51 +000010785/*
10786 * "getbufvar()" function
10787 */
10788 static void
10789f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010790 typval_T *argvars;
10791 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010792{
10793 buf_T *buf;
10794 buf_T *save_curbuf;
10795 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010796 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10799 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010800 ++emsg_off;
10801 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010802
10803 rettv->v_type = VAR_STRING;
10804 rettv->vval.v_string = NULL;
10805
10806 if (buf != NULL && varname != NULL)
10807 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010808 /* set curbuf to be our buf, temporarily */
10809 save_curbuf = curbuf;
10810 curbuf = buf;
10811
Bram Moolenaar0d660222005-01-07 21:51:51 +000010812 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010814 else
10815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010816 if (*varname == NUL)
10817 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10818 * scope prefix before the NUL byte is required by
10819 * find_var_in_ht(). */
10820 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010821 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010822 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010823 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010824 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010825 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010826
10827 /* restore previous notion of curbuf */
10828 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010829 }
10830
10831 --emsg_off;
10832}
10833
10834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 * "getchar()" function
10836 */
10837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010839 typval_T *argvars;
10840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841{
10842 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010843 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010845 /* Position the cursor. Needed after a message that ends in a space. */
10846 windgoto(msg_row, msg_col);
10847
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 ++no_mapping;
10849 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010850 for (;;)
10851 {
10852 if (argvars[0].v_type == VAR_UNKNOWN)
10853 /* getchar(): blocking wait. */
10854 n = safe_vgetc();
10855 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10856 /* getchar(1): only check if char avail */
10857 n = vpeekc();
10858 else if (error || vpeekc() == NUL)
10859 /* illegal argument or getchar(0) and no char avail: return zero */
10860 n = 0;
10861 else
10862 /* getchar(0) and char avail: return char */
10863 n = safe_vgetc();
10864 if (n == K_IGNORE)
10865 continue;
10866 break;
10867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 --no_mapping;
10869 --allow_keys;
10870
Bram Moolenaar219b8702006-11-01 14:32:36 +000010871 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10872 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10873 vimvars[VV_MOUSE_COL].vv_nr = 0;
10874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 if (IS_SPECIAL(n) || mod_mask != 0)
10877 {
10878 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10879 int i = 0;
10880
10881 /* Turn a special key into three bytes, plus modifier. */
10882 if (mod_mask != 0)
10883 {
10884 temp[i++] = K_SPECIAL;
10885 temp[i++] = KS_MODIFIER;
10886 temp[i++] = mod_mask;
10887 }
10888 if (IS_SPECIAL(n))
10889 {
10890 temp[i++] = K_SPECIAL;
10891 temp[i++] = K_SECOND(n);
10892 temp[i++] = K_THIRD(n);
10893 }
10894#ifdef FEAT_MBYTE
10895 else if (has_mbyte)
10896 i += (*mb_char2bytes)(n, temp + i);
10897#endif
10898 else
10899 temp[i++] = n;
10900 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010901 rettv->v_type = VAR_STRING;
10902 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010903
10904#ifdef FEAT_MOUSE
10905 if (n == K_LEFTMOUSE
10906 || n == K_LEFTMOUSE_NM
10907 || n == K_LEFTDRAG
10908 || n == K_LEFTRELEASE
10909 || n == K_LEFTRELEASE_NM
10910 || n == K_MIDDLEMOUSE
10911 || n == K_MIDDLEDRAG
10912 || n == K_MIDDLERELEASE
10913 || n == K_RIGHTMOUSE
10914 || n == K_RIGHTDRAG
10915 || n == K_RIGHTRELEASE
10916 || n == K_X1MOUSE
10917 || n == K_X1DRAG
10918 || n == K_X1RELEASE
10919 || n == K_X2MOUSE
10920 || n == K_X2DRAG
10921 || n == K_X2RELEASE
10922 || n == K_MOUSEDOWN
10923 || n == K_MOUSEUP)
10924 {
10925 int row = mouse_row;
10926 int col = mouse_col;
10927 win_T *win;
10928 linenr_T lnum;
10929# ifdef FEAT_WINDOWS
10930 win_T *wp;
10931# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010932 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010933
10934 if (row >= 0 && col >= 0)
10935 {
10936 /* Find the window at the mouse coordinates and compute the
10937 * text position. */
10938 win = mouse_find_win(&row, &col);
10939 (void)mouse_comp_pos(win, &row, &col, &lnum);
10940# ifdef FEAT_WINDOWS
10941 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010942 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010943# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010944 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010945 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10946 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10947 }
10948 }
10949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 }
10951}
10952
10953/*
10954 * "getcharmod()" function
10955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010958 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010961 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962}
10963
10964/*
10965 * "getcmdline()" function
10966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010968f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010969 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010972 rettv->v_type = VAR_STRING;
10973 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974}
10975
10976/*
10977 * "getcmdpos()" function
10978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010980f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010981 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985}
10986
10987/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010988 * "getcmdtype()" function
10989 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010990 static void
10991f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010992 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000010993 typval_T *rettv;
10994{
10995 rettv->v_type = VAR_STRING;
10996 rettv->vval.v_string = alloc(2);
10997 if (rettv->vval.v_string != NULL)
10998 {
10999 rettv->vval.v_string[0] = get_cmdline_type();
11000 rettv->vval.v_string[1] = NUL;
11001 }
11002}
11003
11004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 * "getcwd()" function
11006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011008f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011009 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011{
11012 char_u cwd[MAXPATHL];
11013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 else
11018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011019 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011021 if (rettv->vval.v_string != NULL)
11022 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023#endif
11024 }
11025}
11026
11027/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011028 * "getfontname()" function
11029 */
11030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011031f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011032 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011033 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011034{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035 rettv->v_type = VAR_STRING;
11036 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011037#ifdef FEAT_GUI
11038 if (gui.in_use)
11039 {
11040 GuiFont font;
11041 char_u *name = NULL;
11042
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011043 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011044 {
11045 /* Get the "Normal" font. Either the name saved by
11046 * hl_set_font_name() or from the font ID. */
11047 font = gui.norm_font;
11048 name = hl_get_font_name();
11049 }
11050 else
11051 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011052 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011053 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11054 return;
11055 font = gui_mch_get_font(name, FALSE);
11056 if (font == NOFONT)
11057 return; /* Invalid font name, return empty string. */
11058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011060 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011061 gui_mch_free_font(font);
11062 }
11063#endif
11064}
11065
11066/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011067 * "getfperm({fname})" function
11068 */
11069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011071 typval_T *argvars;
11072 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011073{
11074 char_u *fname;
11075 struct stat st;
11076 char_u *perm = NULL;
11077 char_u flags[] = "rwx";
11078 int i;
11079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011080 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011082 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011083 if (mch_stat((char *)fname, &st) >= 0)
11084 {
11085 perm = vim_strsave((char_u *)"---------");
11086 if (perm != NULL)
11087 {
11088 for (i = 0; i < 9; i++)
11089 {
11090 if (st.st_mode & (1 << (8 - i)))
11091 perm[i] = flags[i % 3];
11092 }
11093 }
11094 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011095 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011096}
11097
11098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 * "getfsize({fname})" function
11100 */
11101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011102f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 typval_T *argvars;
11104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105{
11106 char_u *fname;
11107 struct stat st;
11108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011111 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112
11113 if (mch_stat((char *)fname, &st) >= 0)
11114 {
11115 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011118 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011120
11121 /* non-perfect check for overflow */
11122 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11123 rettv->vval.v_number = -2;
11124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 }
11126 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128}
11129
11130/*
11131 * "getftime({fname})" function
11132 */
11133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 typval_T *argvars;
11136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
11138 char_u *fname;
11139 struct stat st;
11140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142
11143 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147}
11148
11149/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011150 * "getftype({fname})" function
11151 */
11152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011153f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011154 typval_T *argvars;
11155 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011156{
11157 char_u *fname;
11158 struct stat st;
11159 char_u *type = NULL;
11160 char *t;
11161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011162 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011165 if (mch_lstat((char *)fname, &st) >= 0)
11166 {
11167#ifdef S_ISREG
11168 if (S_ISREG(st.st_mode))
11169 t = "file";
11170 else if (S_ISDIR(st.st_mode))
11171 t = "dir";
11172# ifdef S_ISLNK
11173 else if (S_ISLNK(st.st_mode))
11174 t = "link";
11175# endif
11176# ifdef S_ISBLK
11177 else if (S_ISBLK(st.st_mode))
11178 t = "bdev";
11179# endif
11180# ifdef S_ISCHR
11181 else if (S_ISCHR(st.st_mode))
11182 t = "cdev";
11183# endif
11184# ifdef S_ISFIFO
11185 else if (S_ISFIFO(st.st_mode))
11186 t = "fifo";
11187# endif
11188# ifdef S_ISSOCK
11189 else if (S_ISSOCK(st.st_mode))
11190 t = "fifo";
11191# endif
11192 else
11193 t = "other";
11194#else
11195# ifdef S_IFMT
11196 switch (st.st_mode & S_IFMT)
11197 {
11198 case S_IFREG: t = "file"; break;
11199 case S_IFDIR: t = "dir"; break;
11200# ifdef S_IFLNK
11201 case S_IFLNK: t = "link"; break;
11202# endif
11203# ifdef S_IFBLK
11204 case S_IFBLK: t = "bdev"; break;
11205# endif
11206# ifdef S_IFCHR
11207 case S_IFCHR: t = "cdev"; break;
11208# endif
11209# ifdef S_IFIFO
11210 case S_IFIFO: t = "fifo"; break;
11211# endif
11212# ifdef S_IFSOCK
11213 case S_IFSOCK: t = "socket"; break;
11214# endif
11215 default: t = "other";
11216 }
11217# else
11218 if (mch_isdir(fname))
11219 t = "dir";
11220 else
11221 t = "file";
11222# endif
11223#endif
11224 type = vim_strsave((char_u *)t);
11225 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011227}
11228
11229/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011230 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011231 */
11232 static void
11233f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011234 typval_T *argvars;
11235 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011236{
11237 linenr_T lnum;
11238 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011239 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011240
11241 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011242 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011243 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011244 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011245 retlist = FALSE;
11246 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011247 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011248 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011249 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011250 retlist = TRUE;
11251 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011252
Bram Moolenaar342337a2005-07-21 21:11:17 +000011253 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011254}
11255
11256/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011257 * "getmatches()" function
11258 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011259 static void
11260f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011261 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011262 typval_T *rettv;
11263{
11264#ifdef FEAT_SEARCH_EXTRA
11265 dict_T *dict;
11266 matchitem_T *cur = curwin->w_match_head;
11267
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011268 if (rettv_list_alloc(rettv) == OK)
11269 {
11270 while (cur != NULL)
11271 {
11272 dict = dict_alloc();
11273 if (dict == NULL)
11274 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011275 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11276 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11277 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11278 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11279 list_append_dict(rettv->vval.v_list, dict);
11280 cur = cur->next;
11281 }
11282 }
11283#endif
11284}
11285
11286/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011287 * "getpid()" function
11288 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011289 static void
11290f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011291 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011292 typval_T *rettv;
11293{
11294 rettv->vval.v_number = mch_get_pid();
11295}
11296
11297/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011298 * "getpos(string)" function
11299 */
11300 static void
11301f_getpos(argvars, rettv)
11302 typval_T *argvars;
11303 typval_T *rettv;
11304{
11305 pos_T *fp;
11306 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011307 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011308
11309 if (rettv_list_alloc(rettv) == OK)
11310 {
11311 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011312 fp = var2fpos(&argvars[0], TRUE, &fnum);
11313 if (fnum != -1)
11314 list_append_number(l, (varnumber_T)fnum);
11315 else
11316 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011317 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11318 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011319 list_append_number(l, (fp != NULL)
11320 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011321 : (varnumber_T)0);
11322 list_append_number(l,
11323#ifdef FEAT_VIRTUALEDIT
11324 (fp != NULL) ? (varnumber_T)fp->coladd :
11325#endif
11326 (varnumber_T)0);
11327 }
11328 else
11329 rettv->vval.v_number = FALSE;
11330}
11331
11332/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011333 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011334 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011335 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011336f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011337 typval_T *argvars UNUSED;
11338 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011339{
11340#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011341 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011342#endif
11343
Bram Moolenaar2641f772005-03-25 21:58:17 +000011344#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011345 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011346 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011347 wp = NULL;
11348 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11349 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011350 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011351 if (wp == NULL)
11352 return;
11353 }
11354
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011355 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011356 }
11357#endif
11358}
11359
11360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 * "getreg()" function
11362 */
11363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011365 typval_T *argvars;
11366 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367{
11368 char_u *strregname;
11369 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011370 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011373 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011375 strregname = get_tv_string_chk(&argvars[0]);
11376 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011377 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011378 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011381 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 regname = (strregname == NULL ? '"' : *strregname);
11383 if (regname == 0)
11384 regname = '"';
11385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011387 rettv->vval.v_string = error ? NULL :
11388 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389}
11390
11391/*
11392 * "getregtype()" function
11393 */
11394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011396 typval_T *argvars;
11397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398{
11399 char_u *strregname;
11400 int regname;
11401 char_u buf[NUMBUFLEN + 2];
11402 long reglen = 0;
11403
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011404 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 {
11406 strregname = get_tv_string_chk(&argvars[0]);
11407 if (strregname == NULL) /* type error; errmsg already given */
11408 {
11409 rettv->v_type = VAR_STRING;
11410 rettv->vval.v_string = NULL;
11411 return;
11412 }
11413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 else
11415 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011416 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417
11418 regname = (strregname == NULL ? '"' : *strregname);
11419 if (regname == 0)
11420 regname = '"';
11421
11422 buf[0] = NUL;
11423 buf[1] = NUL;
11424 switch (get_reg_type(regname, &reglen))
11425 {
11426 case MLINE: buf[0] = 'V'; break;
11427 case MCHAR: buf[0] = 'v'; break;
11428#ifdef FEAT_VISUAL
11429 case MBLOCK:
11430 buf[0] = Ctrl_V;
11431 sprintf((char *)buf + 1, "%ld", reglen + 1);
11432 break;
11433#endif
11434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 rettv->v_type = VAR_STRING;
11436 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437}
11438
11439/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011440 * "gettabvar()" function
11441 */
11442 static void
11443f_gettabvar(argvars, rettv)
11444 typval_T *argvars;
11445 typval_T *rettv;
11446{
11447 tabpage_T *tp;
11448 dictitem_T *v;
11449 char_u *varname;
11450
11451 rettv->v_type = VAR_STRING;
11452 rettv->vval.v_string = NULL;
11453
11454 varname = get_tv_string_chk(&argvars[1]);
11455 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11456 if (tp != NULL && varname != NULL)
11457 {
11458 /* look up the variable */
11459 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11460 if (v != NULL)
11461 copy_tv(&v->di_tv, rettv);
11462 }
11463}
11464
11465/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011466 * "gettabwinvar()" function
11467 */
11468 static void
11469f_gettabwinvar(argvars, rettv)
11470 typval_T *argvars;
11471 typval_T *rettv;
11472{
11473 getwinvar(argvars, rettv, 1);
11474}
11475
11476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 * "getwinposx()" function
11478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011481 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485#ifdef FEAT_GUI
11486 if (gui.in_use)
11487 {
11488 int x, y;
11489
11490 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 }
11493#endif
11494}
11495
11496/*
11497 * "getwinposy()" function
11498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011501 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505#ifdef FEAT_GUI
11506 if (gui.in_use)
11507 {
11508 int x, y;
11509
11510 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 }
11513#endif
11514}
11515
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011516/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011517 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011518 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011519 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011520find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011521 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011522 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011523{
11524#ifdef FEAT_WINDOWS
11525 win_T *wp;
11526#endif
11527 int nr;
11528
11529 nr = get_tv_number_chk(vp, NULL);
11530
11531#ifdef FEAT_WINDOWS
11532 if (nr < 0)
11533 return NULL;
11534 if (nr == 0)
11535 return curwin;
11536
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011537 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11538 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011539 if (--nr <= 0)
11540 break;
11541 return wp;
11542#else
11543 if (nr == 0 || nr == 1)
11544 return curwin;
11545 return NULL;
11546#endif
11547}
11548
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549/*
11550 * "getwinvar()" function
11551 */
11552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011554 typval_T *argvars;
11555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011557 getwinvar(argvars, rettv, 0);
11558}
11559
11560/*
11561 * getwinvar() and gettabwinvar()
11562 */
11563 static void
11564getwinvar(argvars, rettv, off)
11565 typval_T *argvars;
11566 typval_T *rettv;
11567 int off; /* 1 for gettabwinvar() */
11568{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 win_T *win, *oldcurwin;
11570 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011572 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011574#ifdef FEAT_WINDOWS
11575 if (off == 1)
11576 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11577 else
11578 tp = curtab;
11579#endif
11580 win = find_win_by_nr(&argvars[off], tp);
11581 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011582 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 rettv->v_type = VAR_STRING;
11585 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586
11587 if (win != NULL && varname != NULL)
11588 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011589 /* Set curwin to be our win, temporarily. Also set curbuf, so
11590 * that we can get buffer-local options. */
11591 oldcurwin = curwin;
11592 curwin = win;
11593 curbuf = win->w_buffer;
11594
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011597 else
11598 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011599 if (*varname == NUL)
11600 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11601 * scope prefix before the NUL byte is required by
11602 * find_var_in_ht(). */
11603 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011605 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011606 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011607 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011609
11610 /* restore previous notion of curwin */
11611 curwin = oldcurwin;
11612 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 }
11614
11615 --emsg_off;
11616}
11617
11618/*
11619 * "glob()" function
11620 */
11621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011623 typval_T *argvars;
11624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011626 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011628 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011630 /* When the optional second argument is non-zero, don't remove matches
11631 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11632 if (argvars[1].v_type != VAR_UNKNOWN
11633 && get_tv_number_chk(&argvars[1], &error))
11634 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011636 if (!error)
11637 {
11638 ExpandInit(&xpc);
11639 xpc.xp_context = EXPAND_FILES;
11640 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11641 NULL, flags, WILD_ALL);
11642 }
11643 else
11644 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645}
11646
11647/*
11648 * "globpath()" function
11649 */
11650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011652 typval_T *argvars;
11653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011655 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011657 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011658 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011660 /* When the optional second argument is non-zero, don't remove matches
11661 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11662 if (argvars[2].v_type != VAR_UNKNOWN
11663 && get_tv_number_chk(&argvars[2], &error))
11664 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011666 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 rettv->vval.v_string = NULL;
11668 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011669 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11670 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671}
11672
11673/*
11674 * "has()" function
11675 */
11676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011677f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011678 typval_T *argvars;
11679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680{
11681 int i;
11682 char_u *name;
11683 int n = FALSE;
11684 static char *(has_list[]) =
11685 {
11686#ifdef AMIGA
11687 "amiga",
11688# ifdef FEAT_ARP
11689 "arp",
11690# endif
11691#endif
11692#ifdef __BEOS__
11693 "beos",
11694#endif
11695#ifdef MSDOS
11696# ifdef DJGPP
11697 "dos32",
11698# else
11699 "dos16",
11700# endif
11701#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011702#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703 "mac",
11704#endif
11705#if defined(MACOS_X_UNIX)
11706 "macunix",
11707#endif
11708#ifdef OS2
11709 "os2",
11710#endif
11711#ifdef __QNX__
11712 "qnx",
11713#endif
11714#ifdef RISCOS
11715 "riscos",
11716#endif
11717#ifdef UNIX
11718 "unix",
11719#endif
11720#ifdef VMS
11721 "vms",
11722#endif
11723#ifdef WIN16
11724 "win16",
11725#endif
11726#ifdef WIN32
11727 "win32",
11728#endif
11729#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11730 "win32unix",
11731#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011732#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 "win64",
11734#endif
11735#ifdef EBCDIC
11736 "ebcdic",
11737#endif
11738#ifndef CASE_INSENSITIVE_FILENAME
11739 "fname_case",
11740#endif
11741#ifdef FEAT_ARABIC
11742 "arabic",
11743#endif
11744#ifdef FEAT_AUTOCMD
11745 "autocmd",
11746#endif
11747#ifdef FEAT_BEVAL
11748 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011749# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11750 "balloon_multiline",
11751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752#endif
11753#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11754 "builtin_terms",
11755# ifdef ALL_BUILTIN_TCAPS
11756 "all_builtin_terms",
11757# endif
11758#endif
11759#ifdef FEAT_BYTEOFF
11760 "byte_offset",
11761#endif
11762#ifdef FEAT_CINDENT
11763 "cindent",
11764#endif
11765#ifdef FEAT_CLIENTSERVER
11766 "clientserver",
11767#endif
11768#ifdef FEAT_CLIPBOARD
11769 "clipboard",
11770#endif
11771#ifdef FEAT_CMDL_COMPL
11772 "cmdline_compl",
11773#endif
11774#ifdef FEAT_CMDHIST
11775 "cmdline_hist",
11776#endif
11777#ifdef FEAT_COMMENTS
11778 "comments",
11779#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011780#ifdef FEAT_CONCEAL
11781 "conceal",
11782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783#ifdef FEAT_CRYPT
11784 "cryptv",
11785#endif
11786#ifdef FEAT_CSCOPE
11787 "cscope",
11788#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011789#ifdef FEAT_CURSORBIND
11790 "cursorbind",
11791#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011792#ifdef CURSOR_SHAPE
11793 "cursorshape",
11794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795#ifdef DEBUG
11796 "debug",
11797#endif
11798#ifdef FEAT_CON_DIALOG
11799 "dialog_con",
11800#endif
11801#ifdef FEAT_GUI_DIALOG
11802 "dialog_gui",
11803#endif
11804#ifdef FEAT_DIFF
11805 "diff",
11806#endif
11807#ifdef FEAT_DIGRAPHS
11808 "digraphs",
11809#endif
11810#ifdef FEAT_DND
11811 "dnd",
11812#endif
11813#ifdef FEAT_EMACS_TAGS
11814 "emacs_tags",
11815#endif
11816 "eval", /* always present, of course! */
11817#ifdef FEAT_EX_EXTRA
11818 "ex_extra",
11819#endif
11820#ifdef FEAT_SEARCH_EXTRA
11821 "extra_search",
11822#endif
11823#ifdef FEAT_FKMAP
11824 "farsi",
11825#endif
11826#ifdef FEAT_SEARCHPATH
11827 "file_in_path",
11828#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011829#if defined(UNIX) && !defined(USE_SYSTEM)
11830 "filterpipe",
11831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832#ifdef FEAT_FIND_ID
11833 "find_in_path",
11834#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011835#ifdef FEAT_FLOAT
11836 "float",
11837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838#ifdef FEAT_FOLDING
11839 "folding",
11840#endif
11841#ifdef FEAT_FOOTER
11842 "footer",
11843#endif
11844#if !defined(USE_SYSTEM) && defined(UNIX)
11845 "fork",
11846#endif
11847#ifdef FEAT_GETTEXT
11848 "gettext",
11849#endif
11850#ifdef FEAT_GUI
11851 "gui",
11852#endif
11853#ifdef FEAT_GUI_ATHENA
11854# ifdef FEAT_GUI_NEXTAW
11855 "gui_neXtaw",
11856# else
11857 "gui_athena",
11858# endif
11859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860#ifdef FEAT_GUI_GTK
11861 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011864#ifdef FEAT_GUI_GNOME
11865 "gui_gnome",
11866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867#ifdef FEAT_GUI_MAC
11868 "gui_mac",
11869#endif
11870#ifdef FEAT_GUI_MOTIF
11871 "gui_motif",
11872#endif
11873#ifdef FEAT_GUI_PHOTON
11874 "gui_photon",
11875#endif
11876#ifdef FEAT_GUI_W16
11877 "gui_win16",
11878#endif
11879#ifdef FEAT_GUI_W32
11880 "gui_win32",
11881#endif
11882#ifdef FEAT_HANGULIN
11883 "hangul_input",
11884#endif
11885#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11886 "iconv",
11887#endif
11888#ifdef FEAT_INS_EXPAND
11889 "insert_expand",
11890#endif
11891#ifdef FEAT_JUMPLIST
11892 "jumplist",
11893#endif
11894#ifdef FEAT_KEYMAP
11895 "keymap",
11896#endif
11897#ifdef FEAT_LANGMAP
11898 "langmap",
11899#endif
11900#ifdef FEAT_LIBCALL
11901 "libcall",
11902#endif
11903#ifdef FEAT_LINEBREAK
11904 "linebreak",
11905#endif
11906#ifdef FEAT_LISP
11907 "lispindent",
11908#endif
11909#ifdef FEAT_LISTCMDS
11910 "listcmds",
11911#endif
11912#ifdef FEAT_LOCALMAP
11913 "localmap",
11914#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011915#ifdef FEAT_LUA
11916# ifndef DYNAMIC_LUA
11917 "lua",
11918# endif
11919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920#ifdef FEAT_MENU
11921 "menu",
11922#endif
11923#ifdef FEAT_SESSION
11924 "mksession",
11925#endif
11926#ifdef FEAT_MODIFY_FNAME
11927 "modify_fname",
11928#endif
11929#ifdef FEAT_MOUSE
11930 "mouse",
11931#endif
11932#ifdef FEAT_MOUSESHAPE
11933 "mouseshape",
11934#endif
11935#if defined(UNIX) || defined(VMS)
11936# ifdef FEAT_MOUSE_DEC
11937 "mouse_dec",
11938# endif
11939# ifdef FEAT_MOUSE_GPM
11940 "mouse_gpm",
11941# endif
11942# ifdef FEAT_MOUSE_JSB
11943 "mouse_jsbterm",
11944# endif
11945# ifdef FEAT_MOUSE_NET
11946 "mouse_netterm",
11947# endif
11948# ifdef FEAT_MOUSE_PTERM
11949 "mouse_pterm",
11950# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011951# ifdef FEAT_SYSMOUSE
11952 "mouse_sysmouse",
11953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954# ifdef FEAT_MOUSE_XTERM
11955 "mouse_xterm",
11956# endif
11957#endif
11958#ifdef FEAT_MBYTE
11959 "multi_byte",
11960#endif
11961#ifdef FEAT_MBYTE_IME
11962 "multi_byte_ime",
11963#endif
11964#ifdef FEAT_MULTI_LANG
11965 "multi_lang",
11966#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011967#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011968#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011969 "mzscheme",
11970#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000011971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972#ifdef FEAT_OLE
11973 "ole",
11974#endif
11975#ifdef FEAT_OSFILETYPE
11976 "osfiletype",
11977#endif
11978#ifdef FEAT_PATH_EXTRA
11979 "path_extra",
11980#endif
11981#ifdef FEAT_PERL
11982#ifndef DYNAMIC_PERL
11983 "perl",
11984#endif
11985#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020011986#ifdef FEAT_PERSISTENT_UNDO
11987 "persistent_undo",
11988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989#ifdef FEAT_PYTHON
11990#ifndef DYNAMIC_PYTHON
11991 "python",
11992#endif
11993#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020011994#ifdef FEAT_PYTHON3
11995#ifndef DYNAMIC_PYTHON3
11996 "python3",
11997#endif
11998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999#ifdef FEAT_POSTSCRIPT
12000 "postscript",
12001#endif
12002#ifdef FEAT_PRINTER
12003 "printer",
12004#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012005#ifdef FEAT_PROFILE
12006 "profile",
12007#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012008#ifdef FEAT_RELTIME
12009 "reltime",
12010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011#ifdef FEAT_QUICKFIX
12012 "quickfix",
12013#endif
12014#ifdef FEAT_RIGHTLEFT
12015 "rightleft",
12016#endif
12017#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12018 "ruby",
12019#endif
12020#ifdef FEAT_SCROLLBIND
12021 "scrollbind",
12022#endif
12023#ifdef FEAT_CMDL_INFO
12024 "showcmd",
12025 "cmdline_info",
12026#endif
12027#ifdef FEAT_SIGNS
12028 "signs",
12029#endif
12030#ifdef FEAT_SMARTINDENT
12031 "smartindent",
12032#endif
12033#ifdef FEAT_SNIFF
12034 "sniff",
12035#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012036#ifdef STARTUPTIME
12037 "startuptime",
12038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039#ifdef FEAT_STL_OPT
12040 "statusline",
12041#endif
12042#ifdef FEAT_SUN_WORKSHOP
12043 "sun_workshop",
12044#endif
12045#ifdef FEAT_NETBEANS_INTG
12046 "netbeans_intg",
12047#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012048#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012049 "spell",
12050#endif
12051#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052 "syntax",
12053#endif
12054#if defined(USE_SYSTEM) || !defined(UNIX)
12055 "system",
12056#endif
12057#ifdef FEAT_TAG_BINS
12058 "tag_binary",
12059#endif
12060#ifdef FEAT_TAG_OLDSTATIC
12061 "tag_old_static",
12062#endif
12063#ifdef FEAT_TAG_ANYWHITE
12064 "tag_any_white",
12065#endif
12066#ifdef FEAT_TCL
12067# ifndef DYNAMIC_TCL
12068 "tcl",
12069# endif
12070#endif
12071#ifdef TERMINFO
12072 "terminfo",
12073#endif
12074#ifdef FEAT_TERMRESPONSE
12075 "termresponse",
12076#endif
12077#ifdef FEAT_TEXTOBJ
12078 "textobjects",
12079#endif
12080#ifdef HAVE_TGETENT
12081 "tgetent",
12082#endif
12083#ifdef FEAT_TITLE
12084 "title",
12085#endif
12086#ifdef FEAT_TOOLBAR
12087 "toolbar",
12088#endif
12089#ifdef FEAT_USR_CMDS
12090 "user-commands", /* was accidentally included in 5.4 */
12091 "user_commands",
12092#endif
12093#ifdef FEAT_VIMINFO
12094 "viminfo",
12095#endif
12096#ifdef FEAT_VERTSPLIT
12097 "vertsplit",
12098#endif
12099#ifdef FEAT_VIRTUALEDIT
12100 "virtualedit",
12101#endif
12102#ifdef FEAT_VISUAL
12103 "visual",
12104#endif
12105#ifdef FEAT_VISUALEXTRA
12106 "visualextra",
12107#endif
12108#ifdef FEAT_VREPLACE
12109 "vreplace",
12110#endif
12111#ifdef FEAT_WILDIGN
12112 "wildignore",
12113#endif
12114#ifdef FEAT_WILDMENU
12115 "wildmenu",
12116#endif
12117#ifdef FEAT_WINDOWS
12118 "windows",
12119#endif
12120#ifdef FEAT_WAK
12121 "winaltkeys",
12122#endif
12123#ifdef FEAT_WRITEBACKUP
12124 "writebackup",
12125#endif
12126#ifdef FEAT_XIM
12127 "xim",
12128#endif
12129#ifdef FEAT_XFONTSET
12130 "xfontset",
12131#endif
12132#ifdef USE_XSMP
12133 "xsmp",
12134#endif
12135#ifdef USE_XSMP_INTERACT
12136 "xsmp_interact",
12137#endif
12138#ifdef FEAT_XCLIPBOARD
12139 "xterm_clipboard",
12140#endif
12141#ifdef FEAT_XTERM_SAVE
12142 "xterm_save",
12143#endif
12144#if defined(UNIX) && defined(FEAT_X11)
12145 "X11",
12146#endif
12147 NULL
12148 };
12149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 for (i = 0; has_list[i] != NULL; ++i)
12152 if (STRICMP(name, has_list[i]) == 0)
12153 {
12154 n = TRUE;
12155 break;
12156 }
12157
12158 if (n == FALSE)
12159 {
12160 if (STRNICMP(name, "patch", 5) == 0)
12161 n = has_patch(atoi((char *)name + 5));
12162 else if (STRICMP(name, "vim_starting") == 0)
12163 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012164#ifdef FEAT_MBYTE
12165 else if (STRICMP(name, "multi_byte_encoding") == 0)
12166 n = has_mbyte;
12167#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012168#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12169 else if (STRICMP(name, "balloon_multiline") == 0)
12170 n = multiline_balloon_available();
12171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172#ifdef DYNAMIC_TCL
12173 else if (STRICMP(name, "tcl") == 0)
12174 n = tcl_enabled(FALSE);
12175#endif
12176#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12177 else if (STRICMP(name, "iconv") == 0)
12178 n = iconv_enabled(FALSE);
12179#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012180#ifdef DYNAMIC_LUA
12181 else if (STRICMP(name, "lua") == 0)
12182 n = lua_enabled(FALSE);
12183#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012184#ifdef DYNAMIC_MZSCHEME
12185 else if (STRICMP(name, "mzscheme") == 0)
12186 n = mzscheme_enabled(FALSE);
12187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188#ifdef DYNAMIC_RUBY
12189 else if (STRICMP(name, "ruby") == 0)
12190 n = ruby_enabled(FALSE);
12191#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012192#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193#ifdef DYNAMIC_PYTHON
12194 else if (STRICMP(name, "python") == 0)
12195 n = python_enabled(FALSE);
12196#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012197#endif
12198#ifdef FEAT_PYTHON3
12199#ifdef DYNAMIC_PYTHON3
12200 else if (STRICMP(name, "python3") == 0)
12201 n = python3_enabled(FALSE);
12202#endif
12203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204#ifdef DYNAMIC_PERL
12205 else if (STRICMP(name, "perl") == 0)
12206 n = perl_enabled(FALSE);
12207#endif
12208#ifdef FEAT_GUI
12209 else if (STRICMP(name, "gui_running") == 0)
12210 n = (gui.in_use || gui.starting);
12211# ifdef FEAT_GUI_W32
12212 else if (STRICMP(name, "gui_win32s") == 0)
12213 n = gui_is_win32s();
12214# endif
12215# ifdef FEAT_BROWSE
12216 else if (STRICMP(name, "browse") == 0)
12217 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12218# endif
12219#endif
12220#ifdef FEAT_SYN_HL
12221 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012222 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223#endif
12224#if defined(WIN3264)
12225 else if (STRICMP(name, "win95") == 0)
12226 n = mch_windows95();
12227#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012228#ifdef FEAT_NETBEANS_INTG
12229 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012230 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 }
12233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012234 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235}
12236
12237/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012238 * "has_key()" function
12239 */
12240 static void
12241f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012242 typval_T *argvars;
12243 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012244{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012245 if (argvars[0].v_type != VAR_DICT)
12246 {
12247 EMSG(_(e_dictreq));
12248 return;
12249 }
12250 if (argvars[0].vval.v_dict == NULL)
12251 return;
12252
12253 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012254 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012255}
12256
12257/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012258 * "haslocaldir()" function
12259 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012260 static void
12261f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012262 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012263 typval_T *rettv;
12264{
12265 rettv->vval.v_number = (curwin->w_localdir != NULL);
12266}
12267
12268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269 * "hasmapto()" function
12270 */
12271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012273 typval_T *argvars;
12274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275{
12276 char_u *name;
12277 char_u *mode;
12278 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012279 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012281 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012282 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 mode = (char_u *)"nvo";
12284 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012285 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012286 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012287 if (argvars[2].v_type != VAR_UNKNOWN)
12288 abbr = get_tv_number(&argvars[2]);
12289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290
Bram Moolenaar2c932302006-03-18 21:42:09 +000012291 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012292 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295}
12296
12297/*
12298 * "histadd()" function
12299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012301f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012302 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304{
12305#ifdef FEAT_CMDHIST
12306 int histype;
12307 char_u *str;
12308 char_u buf[NUMBUFLEN];
12309#endif
12310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012311 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 if (check_restricted() || check_secure())
12313 return;
12314#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012315 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12316 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317 if (histype >= 0)
12318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012319 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320 if (*str != NUL)
12321 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012322 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325 return;
12326 }
12327 }
12328#endif
12329}
12330
12331/*
12332 * "histdel()" function
12333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012335f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012336 typval_T *argvars UNUSED;
12337 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338{
12339#ifdef FEAT_CMDHIST
12340 int n;
12341 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012342 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012344 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12345 if (str == NULL)
12346 n = 0;
12347 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012349 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012350 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012352 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012353 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 else
12355 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012356 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012357 get_tv_string_buf(&argvars[1], buf));
12358 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359#endif
12360}
12361
12362/*
12363 * "histget()" function
12364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012367 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369{
12370#ifdef FEAT_CMDHIST
12371 int type;
12372 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012373 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012375 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12376 if (str == NULL)
12377 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012379 {
12380 type = get_histtype(str);
12381 if (argvars[1].v_type == VAR_UNKNOWN)
12382 idx = get_history_idx(type);
12383 else
12384 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12385 /* -1 on type error */
12386 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012389 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392}
12393
12394/*
12395 * "histnr()" function
12396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012398f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401{
12402 int i;
12403
12404#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 char_u *history = get_tv_string_chk(&argvars[0]);
12406
12407 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 if (i >= HIST_CMD && i < HIST_COUNT)
12409 i = get_history_idx(i);
12410 else
12411#endif
12412 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012413 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414}
12415
12416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417 * "highlightID(name)" function
12418 */
12419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012421 typval_T *argvars;
12422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012424 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425}
12426
12427/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012428 * "highlight_exists()" function
12429 */
12430 static void
12431f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012432 typval_T *argvars;
12433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012434{
12435 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12436}
12437
12438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439 * "hostname()" function
12440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012442f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012443 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445{
12446 char_u hostname[256];
12447
12448 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012449 rettv->v_type = VAR_STRING;
12450 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451}
12452
12453/*
12454 * iconv() function
12455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012457f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012458 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460{
12461#ifdef FEAT_MBYTE
12462 char_u buf1[NUMBUFLEN];
12463 char_u buf2[NUMBUFLEN];
12464 char_u *from, *to, *str;
12465 vimconv_T vimconv;
12466#endif
12467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468 rettv->v_type = VAR_STRING;
12469 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470
12471#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012472 str = get_tv_string(&argvars[0]);
12473 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12474 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 vimconv.vc_type = CONV_NONE;
12476 convert_setup(&vimconv, from, to);
12477
12478 /* If the encodings are equal, no conversion needed. */
12479 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012480 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012482 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483
12484 convert_setup(&vimconv, NULL, NULL);
12485 vim_free(from);
12486 vim_free(to);
12487#endif
12488}
12489
12490/*
12491 * "indent()" function
12492 */
12493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012495 typval_T *argvars;
12496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497{
12498 linenr_T lnum;
12499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012502 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505}
12506
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012507/*
12508 * "index()" function
12509 */
12510 static void
12511f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012512 typval_T *argvars;
12513 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012514{
Bram Moolenaar33570922005-01-25 22:26:29 +000012515 list_T *l;
12516 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012517 long idx = 0;
12518 int ic = FALSE;
12519
12520 rettv->vval.v_number = -1;
12521 if (argvars[0].v_type != VAR_LIST)
12522 {
12523 EMSG(_(e_listreq));
12524 return;
12525 }
12526 l = argvars[0].vval.v_list;
12527 if (l != NULL)
12528 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012529 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012530 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012531 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012532 int error = FALSE;
12533
Bram Moolenaar758711c2005-02-02 23:11:38 +000012534 /* Start at specified item. Use the cached index that list_find()
12535 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012536 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012537 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012538 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012539 ic = get_tv_number_chk(&argvars[3], &error);
12540 if (error)
12541 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012542 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012543
Bram Moolenaar758711c2005-02-02 23:11:38 +000012544 for ( ; item != NULL; item = item->li_next, ++idx)
12545 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012546 {
12547 rettv->vval.v_number = idx;
12548 break;
12549 }
12550 }
12551}
12552
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553static int inputsecret_flag = 0;
12554
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012555static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12556
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012558 * This function is used by f_input() and f_inputdialog() functions. The third
12559 * argument to f_input() specifies the type of completion to use at the
12560 * prompt. The third argument to f_inputdialog() specifies the value to return
12561 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562 */
12563 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012564get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012565 typval_T *argvars;
12566 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012567 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012568{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012569 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570 char_u *p = NULL;
12571 int c;
12572 char_u buf[NUMBUFLEN];
12573 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012574 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012575 int xp_type = EXPAND_NOTHING;
12576 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012578 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012579 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580
12581#ifdef NO_CONSOLE_INPUT
12582 /* While starting up, there is no place to enter text. */
12583 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585#endif
12586
12587 cmd_silent = FALSE; /* Want to see the prompt. */
12588 if (prompt != NULL)
12589 {
12590 /* Only the part of the message after the last NL is considered as
12591 * prompt for the command line */
12592 p = vim_strrchr(prompt, '\n');
12593 if (p == NULL)
12594 p = prompt;
12595 else
12596 {
12597 ++p;
12598 c = *p;
12599 *p = NUL;
12600 msg_start();
12601 msg_clr_eos();
12602 msg_puts_attr(prompt, echo_attr);
12603 msg_didout = FALSE;
12604 msg_starthere();
12605 *p = c;
12606 }
12607 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012609 if (argvars[1].v_type != VAR_UNKNOWN)
12610 {
12611 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12612 if (defstr != NULL)
12613 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012615 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012616 {
12617 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012618 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012619 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012620
Bram Moolenaar4463f292005-09-25 22:20:24 +000012621 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012622
Bram Moolenaar4463f292005-09-25 22:20:24 +000012623 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12624 if (xp_name == NULL)
12625 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012626
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012627 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012628
Bram Moolenaar4463f292005-09-25 22:20:24 +000012629 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12630 &xp_arg) == FAIL)
12631 return;
12632 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012633 }
12634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012635 if (defstr != NULL)
12636 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012637 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12638 xp_type, xp_arg);
12639
12640 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012642 /* since the user typed this, no need to wait for return */
12643 need_wait_return = FALSE;
12644 msg_didout = FALSE;
12645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646 cmd_silent = cmd_silent_save;
12647}
12648
12649/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012650 * "input()" function
12651 * Also handles inputsecret() when inputsecret is set.
12652 */
12653 static void
12654f_input(argvars, rettv)
12655 typval_T *argvars;
12656 typval_T *rettv;
12657{
12658 get_user_input(argvars, rettv, FALSE);
12659}
12660
12661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 * "inputdialog()" function
12663 */
12664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012665f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012666 typval_T *argvars;
12667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668{
12669#if defined(FEAT_GUI_TEXTDIALOG)
12670 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12671 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12672 {
12673 char_u *message;
12674 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012675 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012677 message = get_tv_string_chk(&argvars[0]);
12678 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012679 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012680 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 else
12682 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012683 if (message != NULL && defstr != NULL
12684 && do_dialog(VIM_QUESTION, NULL, message,
12685 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687 else
12688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 if (message != NULL && defstr != NULL
12690 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012691 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 rettv->vval.v_string = vim_strsave(
12693 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 }
12699 else
12700#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012701 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702}
12703
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012704/*
12705 * "inputlist()" function
12706 */
12707 static void
12708f_inputlist(argvars, rettv)
12709 typval_T *argvars;
12710 typval_T *rettv;
12711{
12712 listitem_T *li;
12713 int selected;
12714 int mouse_used;
12715
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012716#ifdef NO_CONSOLE_INPUT
12717 /* While starting up, there is no place to enter text. */
12718 if (no_console_input())
12719 return;
12720#endif
12721 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12722 {
12723 EMSG2(_(e_listarg), "inputlist()");
12724 return;
12725 }
12726
12727 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012728 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012729 lines_left = Rows; /* avoid more prompt */
12730 msg_scroll = TRUE;
12731 msg_clr_eos();
12732
12733 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12734 {
12735 msg_puts(get_tv_string(&li->li_tv));
12736 msg_putchar('\n');
12737 }
12738
12739 /* Ask for choice. */
12740 selected = prompt_for_number(&mouse_used);
12741 if (mouse_used)
12742 selected -= lines_left;
12743
12744 rettv->vval.v_number = selected;
12745}
12746
12747
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12749
12750/*
12751 * "inputrestore()" function
12752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012755 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757{
12758 if (ga_userinput.ga_len > 0)
12759 {
12760 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12762 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012763 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764 }
12765 else if (p_verbose > 1)
12766 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012767 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 }
12770}
12771
12772/*
12773 * "inputsave()" function
12774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012777 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012780 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 if (ga_grow(&ga_userinput, 1) == OK)
12782 {
12783 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12784 + ga_userinput.ga_len);
12785 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012786 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787 }
12788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012789 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790}
12791
12792/*
12793 * "inputsecret()" function
12794 */
12795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012797 typval_T *argvars;
12798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799{
12800 ++cmdline_star;
12801 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012802 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803 --cmdline_star;
12804 --inputsecret_flag;
12805}
12806
12807/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012808 * "insert()" function
12809 */
12810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012812 typval_T *argvars;
12813 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012814{
12815 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012816 listitem_T *item;
12817 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012818 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012819
12820 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012821 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012822 else if ((l = argvars[0].vval.v_list) != NULL
12823 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012824 {
12825 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826 before = get_tv_number_chk(&argvars[2], &error);
12827 if (error)
12828 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012829
Bram Moolenaar758711c2005-02-02 23:11:38 +000012830 if (before == l->lv_len)
12831 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012832 else
12833 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012834 item = list_find(l, before);
12835 if (item == NULL)
12836 {
12837 EMSGN(_(e_listidx), before);
12838 l = NULL;
12839 }
12840 }
12841 if (l != NULL)
12842 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012843 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012844 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012845 }
12846 }
12847}
12848
12849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850 * "isdirectory()" function
12851 */
12852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012854 typval_T *argvars;
12855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858}
12859
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012860/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012861 * "islocked()" function
12862 */
12863 static void
12864f_islocked(argvars, rettv)
12865 typval_T *argvars;
12866 typval_T *rettv;
12867{
12868 lval_T lv;
12869 char_u *end;
12870 dictitem_T *di;
12871
12872 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012873 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12874 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012875 if (end != NULL && lv.ll_name != NULL)
12876 {
12877 if (*end != NUL)
12878 EMSG(_(e_trailing));
12879 else
12880 {
12881 if (lv.ll_tv == NULL)
12882 {
12883 if (check_changedtick(lv.ll_name))
12884 rettv->vval.v_number = 1; /* always locked */
12885 else
12886 {
12887 di = find_var(lv.ll_name, NULL);
12888 if (di != NULL)
12889 {
12890 /* Consider a variable locked when:
12891 * 1. the variable itself is locked
12892 * 2. the value of the variable is locked.
12893 * 3. the List or Dict value is locked.
12894 */
12895 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12896 || tv_islocked(&di->di_tv));
12897 }
12898 }
12899 }
12900 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012901 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012902 else if (lv.ll_newkey != NULL)
12903 EMSG2(_(e_dictkey), lv.ll_newkey);
12904 else if (lv.ll_list != NULL)
12905 /* List item. */
12906 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12907 else
12908 /* Dictionary item. */
12909 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12910 }
12911 }
12912
12913 clear_lval(&lv);
12914}
12915
Bram Moolenaar33570922005-01-25 22:26:29 +000012916static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012917
12918/*
12919 * Turn a dict into a list:
12920 * "what" == 0: list of keys
12921 * "what" == 1: list of values
12922 * "what" == 2: list of items
12923 */
12924 static void
12925dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012926 typval_T *argvars;
12927 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012928 int what;
12929{
Bram Moolenaar33570922005-01-25 22:26:29 +000012930 list_T *l2;
12931 dictitem_T *di;
12932 hashitem_T *hi;
12933 listitem_T *li;
12934 listitem_T *li2;
12935 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012936 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012937
Bram Moolenaar8c711452005-01-14 21:53:12 +000012938 if (argvars[0].v_type != VAR_DICT)
12939 {
12940 EMSG(_(e_dictreq));
12941 return;
12942 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012943 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012944 return;
12945
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012946 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012947 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012948
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012949 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012950 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012951 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012952 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012954 --todo;
12955 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012956
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012957 li = listitem_alloc();
12958 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012959 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012960 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012961
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012962 if (what == 0)
12963 {
12964 /* keys() */
12965 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012966 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012967 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12968 }
12969 else if (what == 1)
12970 {
12971 /* values() */
12972 copy_tv(&di->di_tv, &li->li_tv);
12973 }
12974 else
12975 {
12976 /* items() */
12977 l2 = list_alloc();
12978 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012979 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012980 li->li_tv.vval.v_list = l2;
12981 if (l2 == NULL)
12982 break;
12983 ++l2->lv_refcount;
12984
12985 li2 = listitem_alloc();
12986 if (li2 == NULL)
12987 break;
12988 list_append(l2, li2);
12989 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012990 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012991 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
12992
12993 li2 = listitem_alloc();
12994 if (li2 == NULL)
12995 break;
12996 list_append(l2, li2);
12997 copy_tv(&di->di_tv, &li2->li_tv);
12998 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012999 }
13000 }
13001}
13002
13003/*
13004 * "items(dict)" function
13005 */
13006 static void
13007f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013008 typval_T *argvars;
13009 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013010{
13011 dict_list(argvars, rettv, 2);
13012}
13013
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013015 * "join()" function
13016 */
13017 static void
13018f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013019 typval_T *argvars;
13020 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013021{
13022 garray_T ga;
13023 char_u *sep;
13024
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013025 if (argvars[0].v_type != VAR_LIST)
13026 {
13027 EMSG(_(e_listreq));
13028 return;
13029 }
13030 if (argvars[0].vval.v_list == NULL)
13031 return;
13032 if (argvars[1].v_type == VAR_UNKNOWN)
13033 sep = (char_u *)" ";
13034 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013035 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013036
13037 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013038
13039 if (sep != NULL)
13040 {
13041 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013042 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 ga_append(&ga, NUL);
13044 rettv->vval.v_string = (char_u *)ga.ga_data;
13045 }
13046 else
13047 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013048}
13049
13050/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013051 * "keys()" function
13052 */
13053 static void
13054f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013055 typval_T *argvars;
13056 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013057{
13058 dict_list(argvars, rettv, 0);
13059}
13060
13061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 * "last_buffer_nr()" function.
13063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013065f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013066 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068{
13069 int n = 0;
13070 buf_T *buf;
13071
13072 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13073 if (n < buf->b_fnum)
13074 n = buf->b_fnum;
13075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013076 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013077}
13078
13079/*
13080 * "len()" function
13081 */
13082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013083f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013084 typval_T *argvars;
13085 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013086{
13087 switch (argvars[0].v_type)
13088 {
13089 case VAR_STRING:
13090 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013091 rettv->vval.v_number = (varnumber_T)STRLEN(
13092 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013093 break;
13094 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013095 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013096 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013097 case VAR_DICT:
13098 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13099 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013100 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013101 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013102 break;
13103 }
13104}
13105
Bram Moolenaar33570922005-01-25 22:26:29 +000013106static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013107
13108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013109libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013110 typval_T *argvars;
13111 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013112 int type;
13113{
13114#ifdef FEAT_LIBCALL
13115 char_u *string_in;
13116 char_u **string_result;
13117 int nr_result;
13118#endif
13119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013120 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013121 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013122 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013123
13124 if (check_restricted() || check_secure())
13125 return;
13126
13127#ifdef FEAT_LIBCALL
13128 /* The first two args must be strings, otherwise its meaningless */
13129 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13130 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013131 string_in = NULL;
13132 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013133 string_in = argvars[2].vval.v_string;
13134 if (type == VAR_NUMBER)
13135 string_result = NULL;
13136 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013137 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013138 if (mch_libcall(argvars[0].vval.v_string,
13139 argvars[1].vval.v_string,
13140 string_in,
13141 argvars[2].vval.v_number,
13142 string_result,
13143 &nr_result) == OK
13144 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013145 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013146 }
13147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148}
13149
13150/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013151 * "libcall()" function
13152 */
13153 static void
13154f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013155 typval_T *argvars;
13156 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013157{
13158 libcall_common(argvars, rettv, VAR_STRING);
13159}
13160
13161/*
13162 * "libcallnr()" function
13163 */
13164 static void
13165f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013166 typval_T *argvars;
13167 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013168{
13169 libcall_common(argvars, rettv, VAR_NUMBER);
13170}
13171
13172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 * "line(string)" function
13174 */
13175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013177 typval_T *argvars;
13178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179{
13180 linenr_T lnum = 0;
13181 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013182 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013184 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185 if (fp != NULL)
13186 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188}
13189
13190/*
13191 * "line2byte(lnum)" function
13192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013194f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013195 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197{
13198#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200#else
13201 linenr_T lnum;
13202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13208 if (rettv->vval.v_number >= 0)
13209 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210#endif
13211}
13212
13213/*
13214 * "lispindent(lnum)" function
13215 */
13216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013217f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013218 typval_T *argvars;
13219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220{
13221#ifdef FEAT_LISP
13222 pos_T pos;
13223 linenr_T lnum;
13224
13225 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013226 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13228 {
13229 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 curwin->w_cursor = pos;
13232 }
13233 else
13234#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236}
13237
13238/*
13239 * "localtime()" function
13240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013242f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013243 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013246 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247}
13248
Bram Moolenaar33570922005-01-25 22:26:29 +000013249static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250
13251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013253 typval_T *argvars;
13254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255 int exact;
13256{
13257 char_u *keys;
13258 char_u *which;
13259 char_u buf[NUMBUFLEN];
13260 char_u *keys_buf = NULL;
13261 char_u *rhs;
13262 int mode;
13263 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013264 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265
13266 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013267 rettv->v_type = VAR_STRING;
13268 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013270 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 if (*keys == NUL)
13272 return;
13273
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013274 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013276 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013277 if (argvars[2].v_type != VAR_UNKNOWN)
13278 abbr = get_tv_number(&argvars[2]);
13279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 else
13281 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013282 if (which == NULL)
13283 return;
13284
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 mode = get_map_mode(&which, 0);
13286
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013287 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013288 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 vim_free(keys_buf);
13290 if (rhs != NULL)
13291 {
13292 ga_init(&ga);
13293 ga.ga_itemsize = 1;
13294 ga.ga_growsize = 40;
13295
13296 while (*rhs != NUL)
13297 ga_concat(&ga, str2special(&rhs, FALSE));
13298
13299 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013300 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301 }
13302}
13303
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013304#ifdef FEAT_FLOAT
13305/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013306 * "log()" function
13307 */
13308 static void
13309f_log(argvars, rettv)
13310 typval_T *argvars;
13311 typval_T *rettv;
13312{
13313 float_T f;
13314
13315 rettv->v_type = VAR_FLOAT;
13316 if (get_float_arg(argvars, &f) == OK)
13317 rettv->vval.v_float = log(f);
13318 else
13319 rettv->vval.v_float = 0.0;
13320}
13321
13322/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013323 * "log10()" function
13324 */
13325 static void
13326f_log10(argvars, rettv)
13327 typval_T *argvars;
13328 typval_T *rettv;
13329{
13330 float_T f;
13331
13332 rettv->v_type = VAR_FLOAT;
13333 if (get_float_arg(argvars, &f) == OK)
13334 rettv->vval.v_float = log10(f);
13335 else
13336 rettv->vval.v_float = 0.0;
13337}
13338#endif
13339
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013341 * "map()" function
13342 */
13343 static void
13344f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 typval_T *argvars;
13346 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013347{
13348 filter_map(argvars, rettv, TRUE);
13349}
13350
13351/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013352 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353 */
13354 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013355f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013356 typval_T *argvars;
13357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013359 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360}
13361
13362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013363 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364 */
13365 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013366f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013367 typval_T *argvars;
13368 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013370 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371}
13372
Bram Moolenaar33570922005-01-25 22:26:29 +000013373static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374
13375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013376find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013377 typval_T *argvars;
13378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013379 int type;
13380{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013381 char_u *str = NULL;
13382 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383 char_u *pat;
13384 regmatch_T regmatch;
13385 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013386 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 char_u *save_cpo;
13388 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013389 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013390 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013391 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013392 list_T *l = NULL;
13393 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013394 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013395 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396
13397 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13398 save_cpo = p_cpo;
13399 p_cpo = (char_u *)"";
13400
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013401 rettv->vval.v_number = -1;
13402 if (type == 3)
13403 {
13404 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013405 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013406 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013407 }
13408 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013410 rettv->v_type = VAR_STRING;
13411 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013414 if (argvars[0].v_type == VAR_LIST)
13415 {
13416 if ((l = argvars[0].vval.v_list) == NULL)
13417 goto theend;
13418 li = l->lv_first;
13419 }
13420 else
13421 expr = str = get_tv_string(&argvars[0]);
13422
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013423 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13424 if (pat == NULL)
13425 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013426
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013427 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013429 int error = FALSE;
13430
13431 start = get_tv_number_chk(&argvars[2], &error);
13432 if (error)
13433 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013434 if (l != NULL)
13435 {
13436 li = list_find(l, start);
13437 if (li == NULL)
13438 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013439 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013440 }
13441 else
13442 {
13443 if (start < 0)
13444 start = 0;
13445 if (start > (long)STRLEN(str))
13446 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013447 /* When "count" argument is there ignore matches before "start",
13448 * otherwise skip part of the string. Differs when pattern is "^"
13449 * or "\<". */
13450 if (argvars[3].v_type != VAR_UNKNOWN)
13451 startcol = start;
13452 else
13453 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013454 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013455
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013456 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013457 nth = get_tv_number_chk(&argvars[3], &error);
13458 if (error)
13459 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 }
13461
13462 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13463 if (regmatch.regprog != NULL)
13464 {
13465 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013466
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013467 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013468 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013469 if (l != NULL)
13470 {
13471 if (li == NULL)
13472 {
13473 match = FALSE;
13474 break;
13475 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013476 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013477 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013478 if (str == NULL)
13479 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013480 }
13481
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013482 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013483
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013484 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013485 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013486 if (l == NULL && !match)
13487 break;
13488
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013489 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013490 if (l != NULL)
13491 {
13492 li = li->li_next;
13493 ++idx;
13494 }
13495 else
13496 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013497#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013498 startcol = (colnr_T)(regmatch.startp[0]
13499 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013500#else
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013501 startcol = regmatch.startp[0] + 1 - str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013502#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013503 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013504 }
13505
13506 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013508 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013509 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013510 int i;
13511
13512 /* return list with matched string and submatches */
13513 for (i = 0; i < NSUBEXP; ++i)
13514 {
13515 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013516 {
13517 if (list_append_string(rettv->vval.v_list,
13518 (char_u *)"", 0) == FAIL)
13519 break;
13520 }
13521 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013522 regmatch.startp[i],
13523 (int)(regmatch.endp[i] - regmatch.startp[i]))
13524 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013525 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013526 }
13527 }
13528 else if (type == 2)
13529 {
13530 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013531 if (l != NULL)
13532 copy_tv(&li->li_tv, rettv);
13533 else
13534 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013536 }
13537 else if (l != NULL)
13538 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539 else
13540 {
13541 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 (varnumber_T)(regmatch.startp[0] - str);
13544 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013545 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013547 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 }
13549 }
13550 vim_free(regmatch.regprog);
13551 }
13552
13553theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013554 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555 p_cpo = save_cpo;
13556}
13557
13558/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013559 * "match()" function
13560 */
13561 static void
13562f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013563 typval_T *argvars;
13564 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013565{
13566 find_some_match(argvars, rettv, 1);
13567}
13568
13569/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013570 * "matchadd()" function
13571 */
13572 static void
13573f_matchadd(argvars, rettv)
13574 typval_T *argvars;
13575 typval_T *rettv;
13576{
13577#ifdef FEAT_SEARCH_EXTRA
13578 char_u buf[NUMBUFLEN];
13579 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13580 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13581 int prio = 10; /* default priority */
13582 int id = -1;
13583 int error = FALSE;
13584
13585 rettv->vval.v_number = -1;
13586
13587 if (grp == NULL || pat == NULL)
13588 return;
13589 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013590 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013591 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013592 if (argvars[3].v_type != VAR_UNKNOWN)
13593 id = get_tv_number_chk(&argvars[3], &error);
13594 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013595 if (error == TRUE)
13596 return;
13597 if (id >= 1 && id <= 3)
13598 {
13599 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13600 return;
13601 }
13602
13603 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13604#endif
13605}
13606
13607/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013608 * "matcharg()" function
13609 */
13610 static void
13611f_matcharg(argvars, rettv)
13612 typval_T *argvars;
13613 typval_T *rettv;
13614{
13615 if (rettv_list_alloc(rettv) == OK)
13616 {
13617#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013618 int id = get_tv_number(&argvars[0]);
13619 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013620
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013621 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013622 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013623 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13624 {
13625 list_append_string(rettv->vval.v_list,
13626 syn_id2name(m->hlg_id), -1);
13627 list_append_string(rettv->vval.v_list, m->pattern, -1);
13628 }
13629 else
13630 {
13631 list_append_string(rettv->vval.v_list, NUL, -1);
13632 list_append_string(rettv->vval.v_list, NUL, -1);
13633 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013634 }
13635#endif
13636 }
13637}
13638
13639/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013640 * "matchdelete()" function
13641 */
13642 static void
13643f_matchdelete(argvars, rettv)
13644 typval_T *argvars;
13645 typval_T *rettv;
13646{
13647#ifdef FEAT_SEARCH_EXTRA
13648 rettv->vval.v_number = match_delete(curwin,
13649 (int)get_tv_number(&argvars[0]), TRUE);
13650#endif
13651}
13652
13653/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013654 * "matchend()" function
13655 */
13656 static void
13657f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013658 typval_T *argvars;
13659 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013660{
13661 find_some_match(argvars, rettv, 0);
13662}
13663
13664/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013665 * "matchlist()" function
13666 */
13667 static void
13668f_matchlist(argvars, rettv)
13669 typval_T *argvars;
13670 typval_T *rettv;
13671{
13672 find_some_match(argvars, rettv, 3);
13673}
13674
13675/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013676 * "matchstr()" function
13677 */
13678 static void
13679f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013680 typval_T *argvars;
13681 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013682{
13683 find_some_match(argvars, rettv, 2);
13684}
13685
Bram Moolenaar33570922005-01-25 22:26:29 +000013686static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013687
13688 static void
13689max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013690 typval_T *argvars;
13691 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013692 int domax;
13693{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013694 long n = 0;
13695 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013697
13698 if (argvars[0].v_type == VAR_LIST)
13699 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013700 list_T *l;
13701 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013702
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013703 l = argvars[0].vval.v_list;
13704 if (l != NULL)
13705 {
13706 li = l->lv_first;
13707 if (li != NULL)
13708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013709 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013710 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013711 {
13712 li = li->li_next;
13713 if (li == NULL)
13714 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013715 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013716 if (domax ? i > n : i < n)
13717 n = i;
13718 }
13719 }
13720 }
13721 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013722 else if (argvars[0].v_type == VAR_DICT)
13723 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013724 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013725 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013726 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013727 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013728
13729 d = argvars[0].vval.v_dict;
13730 if (d != NULL)
13731 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013732 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013733 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013734 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013735 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013736 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013737 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013738 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013739 if (first)
13740 {
13741 n = i;
13742 first = FALSE;
13743 }
13744 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013745 n = i;
13746 }
13747 }
13748 }
13749 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013750 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013751 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013752 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013753}
13754
13755/*
13756 * "max()" function
13757 */
13758 static void
13759f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013760 typval_T *argvars;
13761 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013762{
13763 max_min(argvars, rettv, TRUE);
13764}
13765
13766/*
13767 * "min()" function
13768 */
13769 static void
13770f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013771 typval_T *argvars;
13772 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013773{
13774 max_min(argvars, rettv, FALSE);
13775}
13776
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013777static int mkdir_recurse __ARGS((char_u *dir, int prot));
13778
13779/*
13780 * Create the directory in which "dir" is located, and higher levels when
13781 * needed.
13782 */
13783 static int
13784mkdir_recurse(dir, prot)
13785 char_u *dir;
13786 int prot;
13787{
13788 char_u *p;
13789 char_u *updir;
13790 int r = FAIL;
13791
13792 /* Get end of directory name in "dir".
13793 * We're done when it's "/" or "c:/". */
13794 p = gettail_sep(dir);
13795 if (p <= get_past_head(dir))
13796 return OK;
13797
13798 /* If the directory exists we're done. Otherwise: create it.*/
13799 updir = vim_strnsave(dir, (int)(p - dir));
13800 if (updir == NULL)
13801 return FAIL;
13802 if (mch_isdir(updir))
13803 r = OK;
13804 else if (mkdir_recurse(updir, prot) == OK)
13805 r = vim_mkdir_emsg(updir, prot);
13806 vim_free(updir);
13807 return r;
13808}
13809
13810#ifdef vim_mkdir
13811/*
13812 * "mkdir()" function
13813 */
13814 static void
13815f_mkdir(argvars, rettv)
13816 typval_T *argvars;
13817 typval_T *rettv;
13818{
13819 char_u *dir;
13820 char_u buf[NUMBUFLEN];
13821 int prot = 0755;
13822
13823 rettv->vval.v_number = FAIL;
13824 if (check_restricted() || check_secure())
13825 return;
13826
13827 dir = get_tv_string_buf(&argvars[0], buf);
13828 if (argvars[1].v_type != VAR_UNKNOWN)
13829 {
13830 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013831 prot = get_tv_number_chk(&argvars[2], NULL);
13832 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013833 mkdir_recurse(dir, prot);
13834 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013836}
13837#endif
13838
Bram Moolenaar0d660222005-01-07 21:51:51 +000013839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 * "mode()" function
13841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013843f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013844 typval_T *argvars;
13845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013847 char_u buf[3];
13848
13849 buf[1] = NUL;
13850 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851
13852#ifdef FEAT_VISUAL
13853 if (VIsual_active)
13854 {
13855 if (VIsual_select)
13856 buf[0] = VIsual_mode + 's' - 'v';
13857 else
13858 buf[0] = VIsual_mode;
13859 }
13860 else
13861#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013862 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13863 || State == CONFIRM)
13864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013866 if (State == ASKMORE)
13867 buf[1] = 'm';
13868 else if (State == CONFIRM)
13869 buf[1] = '?';
13870 }
13871 else if (State == EXTERNCMD)
13872 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873 else if (State & INSERT)
13874 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013875#ifdef FEAT_VREPLACE
13876 if (State & VREPLACE_FLAG)
13877 {
13878 buf[0] = 'R';
13879 buf[1] = 'v';
13880 }
13881 else
13882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883 if (State & REPLACE_FLAG)
13884 buf[0] = 'R';
13885 else
13886 buf[0] = 'i';
13887 }
13888 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013891 if (exmode_active)
13892 buf[1] = 'v';
13893 }
13894 else if (exmode_active)
13895 {
13896 buf[0] = 'c';
13897 buf[1] = 'e';
13898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013902 if (finish_op)
13903 buf[1] = 'o';
13904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013906 /* Clear out the minor mode when the argument is not a non-zero number or
13907 * non-empty string. */
13908 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013909 buf[1] = NUL;
13910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013911 rettv->vval.v_string = vim_strsave(buf);
13912 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913}
13914
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013915#ifdef FEAT_MZSCHEME
13916/*
13917 * "mzeval()" function
13918 */
13919 static void
13920f_mzeval(argvars, rettv)
13921 typval_T *argvars;
13922 typval_T *rettv;
13923{
13924 char_u *str;
13925 char_u buf[NUMBUFLEN];
13926
13927 str = get_tv_string_buf(&argvars[0], buf);
13928 do_mzeval(str, rettv);
13929}
13930#endif
13931
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013933 * "nextnonblank()" function
13934 */
13935 static void
13936f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013937 typval_T *argvars;
13938 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013939{
13940 linenr_T lnum;
13941
13942 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013944 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013945 {
13946 lnum = 0;
13947 break;
13948 }
13949 if (*skipwhite(ml_get(lnum)) != NUL)
13950 break;
13951 }
13952 rettv->vval.v_number = lnum;
13953}
13954
13955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 * "nr2char()" function
13957 */
13958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013959f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013960 typval_T *argvars;
13961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962{
13963 char_u buf[NUMBUFLEN];
13964
13965#ifdef FEAT_MBYTE
13966 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013967 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 else
13969#endif
13970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013971 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 buf[1] = NUL;
13973 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013974 rettv->v_type = VAR_STRING;
13975 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013976}
13977
13978/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013979 * "pathshorten()" function
13980 */
13981 static void
13982f_pathshorten(argvars, rettv)
13983 typval_T *argvars;
13984 typval_T *rettv;
13985{
13986 char_u *p;
13987
13988 rettv->v_type = VAR_STRING;
13989 p = get_tv_string_chk(&argvars[0]);
13990 if (p == NULL)
13991 rettv->vval.v_string = NULL;
13992 else
13993 {
13994 p = vim_strsave(p);
13995 rettv->vval.v_string = p;
13996 if (p != NULL)
13997 shorten_dir(p);
13998 }
13999}
14000
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014001#ifdef FEAT_FLOAT
14002/*
14003 * "pow()" function
14004 */
14005 static void
14006f_pow(argvars, rettv)
14007 typval_T *argvars;
14008 typval_T *rettv;
14009{
14010 float_T fx, fy;
14011
14012 rettv->v_type = VAR_FLOAT;
14013 if (get_float_arg(argvars, &fx) == OK
14014 && get_float_arg(&argvars[1], &fy) == OK)
14015 rettv->vval.v_float = pow(fx, fy);
14016 else
14017 rettv->vval.v_float = 0.0;
14018}
14019#endif
14020
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014021/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014022 * "prevnonblank()" function
14023 */
14024 static void
14025f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014026 typval_T *argvars;
14027 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014028{
14029 linenr_T lnum;
14030
14031 lnum = get_tv_lnum(argvars);
14032 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14033 lnum = 0;
14034 else
14035 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14036 --lnum;
14037 rettv->vval.v_number = lnum;
14038}
14039
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014040#ifdef HAVE_STDARG_H
14041/* This dummy va_list is here because:
14042 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14043 * - locally in the function results in a "used before set" warning
14044 * - using va_start() to initialize it gives "function with fixed args" error */
14045static va_list ap;
14046#endif
14047
Bram Moolenaar8c711452005-01-14 21:53:12 +000014048/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014049 * "printf()" function
14050 */
14051 static void
14052f_printf(argvars, rettv)
14053 typval_T *argvars;
14054 typval_T *rettv;
14055{
14056 rettv->v_type = VAR_STRING;
14057 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014058#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014059 {
14060 char_u buf[NUMBUFLEN];
14061 int len;
14062 char_u *s;
14063 int saved_did_emsg = did_emsg;
14064 char *fmt;
14065
14066 /* Get the required length, allocate the buffer and do it for real. */
14067 did_emsg = FALSE;
14068 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014069 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014070 if (!did_emsg)
14071 {
14072 s = alloc(len + 1);
14073 if (s != NULL)
14074 {
14075 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014076 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014077 }
14078 }
14079 did_emsg |= saved_did_emsg;
14080 }
14081#endif
14082}
14083
14084/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014085 * "pumvisible()" function
14086 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014087 static void
14088f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014089 typval_T *argvars UNUSED;
14090 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014091{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014092#ifdef FEAT_INS_EXPAND
14093 if (pum_visible())
14094 rettv->vval.v_number = 1;
14095#endif
14096}
14097
14098/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014099 * "range()" function
14100 */
14101 static void
14102f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014103 typval_T *argvars;
14104 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014105{
14106 long start;
14107 long end;
14108 long stride = 1;
14109 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014110 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014112 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014113 if (argvars[1].v_type == VAR_UNKNOWN)
14114 {
14115 end = start - 1;
14116 start = 0;
14117 }
14118 else
14119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014120 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014121 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014122 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014123 }
14124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014125 if (error)
14126 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014127 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014128 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014129 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014130 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014131 else
14132 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014133 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014134 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014135 if (list_append_number(rettv->vval.v_list,
14136 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014137 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014138 }
14139}
14140
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014141/*
14142 * "readfile()" function
14143 */
14144 static void
14145f_readfile(argvars, rettv)
14146 typval_T *argvars;
14147 typval_T *rettv;
14148{
14149 int binary = FALSE;
14150 char_u *fname;
14151 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014152 listitem_T *li;
14153#define FREAD_SIZE 200 /* optimized for text lines */
14154 char_u buf[FREAD_SIZE];
14155 int readlen; /* size of last fread() */
14156 int buflen; /* nr of valid chars in buf[] */
14157 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14158 int tolist; /* first byte in buf[] still to be put in list */
14159 int chop; /* how many CR to chop off */
14160 char_u *prev = NULL; /* previously read bytes, if any */
14161 int prevlen = 0; /* length of "prev" if not NULL */
14162 char_u *s;
14163 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014164 long maxline = MAXLNUM;
14165 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014166
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014167 if (argvars[1].v_type != VAR_UNKNOWN)
14168 {
14169 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14170 binary = TRUE;
14171 if (argvars[2].v_type != VAR_UNKNOWN)
14172 maxline = get_tv_number(&argvars[2]);
14173 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014174
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014175 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014176 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014177
14178 /* Always open the file in binary mode, library functions have a mind of
14179 * their own about CR-LF conversion. */
14180 fname = get_tv_string(&argvars[0]);
14181 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14182 {
14183 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14184 return;
14185 }
14186
14187 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014188 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014189 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014190 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014191 buflen = filtd + readlen;
14192 tolist = 0;
14193 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14194 {
14195 if (buf[filtd] == '\n' || readlen <= 0)
14196 {
14197 /* Only when in binary mode add an empty list item when the
14198 * last line ends in a '\n'. */
14199 if (!binary && readlen == 0 && filtd == 0)
14200 break;
14201
14202 /* Found end-of-line or end-of-file: add a text line to the
14203 * list. */
14204 chop = 0;
14205 if (!binary)
14206 while (filtd - chop - 1 >= tolist
14207 && buf[filtd - chop - 1] == '\r')
14208 ++chop;
14209 len = filtd - tolist - chop;
14210 if (prev == NULL)
14211 s = vim_strnsave(buf + tolist, len);
14212 else
14213 {
14214 s = alloc((unsigned)(prevlen + len + 1));
14215 if (s != NULL)
14216 {
14217 mch_memmove(s, prev, prevlen);
14218 vim_free(prev);
14219 prev = NULL;
14220 mch_memmove(s + prevlen, buf + tolist, len);
14221 s[prevlen + len] = NUL;
14222 }
14223 }
14224 tolist = filtd + 1;
14225
14226 li = listitem_alloc();
14227 if (li == NULL)
14228 {
14229 vim_free(s);
14230 break;
14231 }
14232 li->li_tv.v_type = VAR_STRING;
14233 li->li_tv.v_lock = 0;
14234 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014235 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014236
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014237 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014238 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014239 if (readlen <= 0)
14240 break;
14241 }
14242 else if (buf[filtd] == NUL)
14243 buf[filtd] = '\n';
14244 }
14245 if (readlen <= 0)
14246 break;
14247
14248 if (tolist == 0)
14249 {
14250 /* "buf" is full, need to move text to an allocated buffer */
14251 if (prev == NULL)
14252 {
14253 prev = vim_strnsave(buf, buflen);
14254 prevlen = buflen;
14255 }
14256 else
14257 {
14258 s = alloc((unsigned)(prevlen + buflen));
14259 if (s != NULL)
14260 {
14261 mch_memmove(s, prev, prevlen);
14262 mch_memmove(s + prevlen, buf, buflen);
14263 vim_free(prev);
14264 prev = s;
14265 prevlen += buflen;
14266 }
14267 }
14268 filtd = 0;
14269 }
14270 else
14271 {
14272 mch_memmove(buf, buf + tolist, buflen - tolist);
14273 filtd -= tolist;
14274 }
14275 }
14276
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014277 /*
14278 * For a negative line count use only the lines at the end of the file,
14279 * free the rest.
14280 */
14281 if (maxline < 0)
14282 while (cnt > -maxline)
14283 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014284 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014285 --cnt;
14286 }
14287
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014288 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014289 fclose(fd);
14290}
14291
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014292#if defined(FEAT_RELTIME)
14293static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14294
14295/*
14296 * Convert a List to proftime_T.
14297 * Return FAIL when there is something wrong.
14298 */
14299 static int
14300list2proftime(arg, tm)
14301 typval_T *arg;
14302 proftime_T *tm;
14303{
14304 long n1, n2;
14305 int error = FALSE;
14306
14307 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14308 || arg->vval.v_list->lv_len != 2)
14309 return FAIL;
14310 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14311 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14312# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014313 tm->HighPart = n1;
14314 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014315# else
14316 tm->tv_sec = n1;
14317 tm->tv_usec = n2;
14318# endif
14319 return error ? FAIL : OK;
14320}
14321#endif /* FEAT_RELTIME */
14322
14323/*
14324 * "reltime()" function
14325 */
14326 static void
14327f_reltime(argvars, rettv)
14328 typval_T *argvars;
14329 typval_T *rettv;
14330{
14331#ifdef FEAT_RELTIME
14332 proftime_T res;
14333 proftime_T start;
14334
14335 if (argvars[0].v_type == VAR_UNKNOWN)
14336 {
14337 /* No arguments: get current time. */
14338 profile_start(&res);
14339 }
14340 else if (argvars[1].v_type == VAR_UNKNOWN)
14341 {
14342 if (list2proftime(&argvars[0], &res) == FAIL)
14343 return;
14344 profile_end(&res);
14345 }
14346 else
14347 {
14348 /* Two arguments: compute the difference. */
14349 if (list2proftime(&argvars[0], &start) == FAIL
14350 || list2proftime(&argvars[1], &res) == FAIL)
14351 return;
14352 profile_sub(&res, &start);
14353 }
14354
14355 if (rettv_list_alloc(rettv) == OK)
14356 {
14357 long n1, n2;
14358
14359# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014360 n1 = res.HighPart;
14361 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014362# else
14363 n1 = res.tv_sec;
14364 n2 = res.tv_usec;
14365# endif
14366 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14367 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14368 }
14369#endif
14370}
14371
14372/*
14373 * "reltimestr()" function
14374 */
14375 static void
14376f_reltimestr(argvars, rettv)
14377 typval_T *argvars;
14378 typval_T *rettv;
14379{
14380#ifdef FEAT_RELTIME
14381 proftime_T tm;
14382#endif
14383
14384 rettv->v_type = VAR_STRING;
14385 rettv->vval.v_string = NULL;
14386#ifdef FEAT_RELTIME
14387 if (list2proftime(&argvars[0], &tm) == OK)
14388 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14389#endif
14390}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014391
Bram Moolenaar0d660222005-01-07 21:51:51 +000014392#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14393static void make_connection __ARGS((void));
14394static int check_connection __ARGS((void));
14395
14396 static void
14397make_connection()
14398{
14399 if (X_DISPLAY == NULL
14400# ifdef FEAT_GUI
14401 && !gui.in_use
14402# endif
14403 )
14404 {
14405 x_force_connect = TRUE;
14406 setup_term_clip();
14407 x_force_connect = FALSE;
14408 }
14409}
14410
14411 static int
14412check_connection()
14413{
14414 make_connection();
14415 if (X_DISPLAY == NULL)
14416 {
14417 EMSG(_("E240: No connection to Vim server"));
14418 return FAIL;
14419 }
14420 return OK;
14421}
14422#endif
14423
14424#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014425static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014426
14427 static void
14428remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014429 typval_T *argvars;
14430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014431 int expr;
14432{
14433 char_u *server_name;
14434 char_u *keys;
14435 char_u *r = NULL;
14436 char_u buf[NUMBUFLEN];
14437# ifdef WIN32
14438 HWND w;
14439# else
14440 Window w;
14441# endif
14442
14443 if (check_restricted() || check_secure())
14444 return;
14445
14446# ifdef FEAT_X11
14447 if (check_connection() == FAIL)
14448 return;
14449# endif
14450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 server_name = get_tv_string_chk(&argvars[0]);
14452 if (server_name == NULL)
14453 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014454 keys = get_tv_string_buf(&argvars[1], buf);
14455# ifdef WIN32
14456 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14457# else
14458 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14459 < 0)
14460# endif
14461 {
14462 if (r != NULL)
14463 EMSG(r); /* sending worked but evaluation failed */
14464 else
14465 EMSG2(_("E241: Unable to send to %s"), server_name);
14466 return;
14467 }
14468
14469 rettv->vval.v_string = r;
14470
14471 if (argvars[2].v_type != VAR_UNKNOWN)
14472 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014473 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014474 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014475 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014476
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014477 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014478 v.di_tv.v_type = VAR_STRING;
14479 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014480 idvar = get_tv_string_chk(&argvars[2]);
14481 if (idvar != NULL)
14482 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014483 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014484 }
14485}
14486#endif
14487
14488/*
14489 * "remote_expr()" function
14490 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014491 static void
14492f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014493 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014494 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495{
14496 rettv->v_type = VAR_STRING;
14497 rettv->vval.v_string = NULL;
14498#ifdef FEAT_CLIENTSERVER
14499 remote_common(argvars, rettv, TRUE);
14500#endif
14501}
14502
14503/*
14504 * "remote_foreground()" function
14505 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014506 static void
14507f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014508 typval_T *argvars UNUSED;
14509 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014510{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014511#ifdef FEAT_CLIENTSERVER
14512# ifdef WIN32
14513 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014514 {
14515 char_u *server_name = get_tv_string_chk(&argvars[0]);
14516
14517 if (server_name != NULL)
14518 serverForeground(server_name);
14519 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014520# else
14521 /* Send a foreground() expression to the server. */
14522 argvars[1].v_type = VAR_STRING;
14523 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14524 argvars[2].v_type = VAR_UNKNOWN;
14525 remote_common(argvars, rettv, TRUE);
14526 vim_free(argvars[1].vval.v_string);
14527# endif
14528#endif
14529}
14530
Bram Moolenaar0d660222005-01-07 21:51:51 +000014531 static void
14532f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014533 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014534 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014535{
14536#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014537 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014538 char_u *s = NULL;
14539# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014540 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014541# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014542 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014543
14544 if (check_restricted() || check_secure())
14545 {
14546 rettv->vval.v_number = -1;
14547 return;
14548 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014549 serverid = get_tv_string_chk(&argvars[0]);
14550 if (serverid == NULL)
14551 {
14552 rettv->vval.v_number = -1;
14553 return; /* type error; errmsg already given */
14554 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014555# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014556 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014557 if (n == 0)
14558 rettv->vval.v_number = -1;
14559 else
14560 {
14561 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14562 rettv->vval.v_number = (s != NULL);
14563 }
14564# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014565 if (check_connection() == FAIL)
14566 return;
14567
14568 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014569 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570# endif
14571
14572 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014574 char_u *retvar;
14575
Bram Moolenaar33570922005-01-25 22:26:29 +000014576 v.di_tv.v_type = VAR_STRING;
14577 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014578 retvar = get_tv_string_chk(&argvars[1]);
14579 if (retvar != NULL)
14580 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014581 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014582 }
14583#else
14584 rettv->vval.v_number = -1;
14585#endif
14586}
14587
Bram Moolenaar0d660222005-01-07 21:51:51 +000014588 static void
14589f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014590 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014591 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014592{
14593 char_u *r = NULL;
14594
14595#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014596 char_u *serverid = get_tv_string_chk(&argvars[0]);
14597
14598 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014599 {
14600# ifdef WIN32
14601 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014602 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014603
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014604 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014605 if (n != 0)
14606 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14607 if (r == NULL)
14608# else
14609 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014610 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014611# endif
14612 EMSG(_("E277: Unable to read a server reply"));
14613 }
14614#endif
14615 rettv->v_type = VAR_STRING;
14616 rettv->vval.v_string = r;
14617}
14618
14619/*
14620 * "remote_send()" function
14621 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014622 static void
14623f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014624 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014625 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014626{
14627 rettv->v_type = VAR_STRING;
14628 rettv->vval.v_string = NULL;
14629#ifdef FEAT_CLIENTSERVER
14630 remote_common(argvars, rettv, FALSE);
14631#endif
14632}
14633
14634/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014635 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014636 */
14637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014638f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014639 typval_T *argvars;
14640 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014641{
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 list_T *l;
14643 listitem_T *item, *item2;
14644 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014645 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014646 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014647 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014648 dict_T *d;
14649 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014650
Bram Moolenaar8c711452005-01-14 21:53:12 +000014651 if (argvars[0].v_type == VAR_DICT)
14652 {
14653 if (argvars[2].v_type != VAR_UNKNOWN)
14654 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014655 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014656 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014658 key = get_tv_string_chk(&argvars[1]);
14659 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014661 di = dict_find(d, key, -1);
14662 if (di == NULL)
14663 EMSG2(_(e_dictkey), key);
14664 else
14665 {
14666 *rettv = di->di_tv;
14667 init_tv(&di->di_tv);
14668 dictitem_remove(d, di);
14669 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014670 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014671 }
14672 }
14673 else if (argvars[0].v_type != VAR_LIST)
14674 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014675 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014676 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014677 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014678 int error = FALSE;
14679
14680 idx = get_tv_number_chk(&argvars[1], &error);
14681 if (error)
14682 ; /* type error: do nothing, errmsg already given */
14683 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014684 EMSGN(_(e_listidx), idx);
14685 else
14686 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014687 if (argvars[2].v_type == VAR_UNKNOWN)
14688 {
14689 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014690 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014691 *rettv = item->li_tv;
14692 vim_free(item);
14693 }
14694 else
14695 {
14696 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014697 end = get_tv_number_chk(&argvars[2], &error);
14698 if (error)
14699 ; /* type error: do nothing */
14700 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014701 EMSGN(_(e_listidx), end);
14702 else
14703 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014704 int cnt = 0;
14705
14706 for (li = item; li != NULL; li = li->li_next)
14707 {
14708 ++cnt;
14709 if (li == item2)
14710 break;
14711 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014712 if (li == NULL) /* didn't find "item2" after "item" */
14713 EMSG(_(e_invrange));
14714 else
14715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014716 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014717 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014718 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014719 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014720 l->lv_first = item;
14721 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014722 item->li_prev = NULL;
14723 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014724 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014725 }
14726 }
14727 }
14728 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014729 }
14730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731}
14732
14733/*
14734 * "rename({from}, {to})" function
14735 */
14736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014737f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014738 typval_T *argvars;
14739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740{
14741 char_u buf[NUMBUFLEN];
14742
14743 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014744 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014746 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14747 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748}
14749
14750/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014751 * "repeat()" function
14752 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014753 static void
14754f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014755 typval_T *argvars;
14756 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014757{
14758 char_u *p;
14759 int n;
14760 int slen;
14761 int len;
14762 char_u *r;
14763 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014764
14765 n = get_tv_number(&argvars[1]);
14766 if (argvars[0].v_type == VAR_LIST)
14767 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014768 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014769 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014770 if (list_extend(rettv->vval.v_list,
14771 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014772 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014773 }
14774 else
14775 {
14776 p = get_tv_string(&argvars[0]);
14777 rettv->v_type = VAR_STRING;
14778 rettv->vval.v_string = NULL;
14779
14780 slen = (int)STRLEN(p);
14781 len = slen * n;
14782 if (len <= 0)
14783 return;
14784
14785 r = alloc(len + 1);
14786 if (r != NULL)
14787 {
14788 for (i = 0; i < n; i++)
14789 mch_memmove(r + i * slen, p, (size_t)slen);
14790 r[len] = NUL;
14791 }
14792
14793 rettv->vval.v_string = r;
14794 }
14795}
14796
14797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 * "resolve()" function
14799 */
14800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014801f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014802 typval_T *argvars;
14803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804{
14805 char_u *p;
14806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014807 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808#ifdef FEAT_SHORTCUT
14809 {
14810 char_u *v = NULL;
14811
14812 v = mch_resolve_shortcut(p);
14813 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014814 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014816 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817 }
14818#else
14819# ifdef HAVE_READLINK
14820 {
14821 char_u buf[MAXPATHL + 1];
14822 char_u *cpy;
14823 int len;
14824 char_u *remain = NULL;
14825 char_u *q;
14826 int is_relative_to_current = FALSE;
14827 int has_trailing_pathsep = FALSE;
14828 int limit = 100;
14829
14830 p = vim_strsave(p);
14831
14832 if (p[0] == '.' && (vim_ispathsep(p[1])
14833 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14834 is_relative_to_current = TRUE;
14835
14836 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014837 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 has_trailing_pathsep = TRUE;
14839
14840 q = getnextcomp(p);
14841 if (*q != NUL)
14842 {
14843 /* Separate the first path component in "p", and keep the
14844 * remainder (beginning with the path separator). */
14845 remain = vim_strsave(q - 1);
14846 q[-1] = NUL;
14847 }
14848
14849 for (;;)
14850 {
14851 for (;;)
14852 {
14853 len = readlink((char *)p, (char *)buf, MAXPATHL);
14854 if (len <= 0)
14855 break;
14856 buf[len] = NUL;
14857
14858 if (limit-- == 0)
14859 {
14860 vim_free(p);
14861 vim_free(remain);
14862 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014863 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864 goto fail;
14865 }
14866
14867 /* Ensure that the result will have a trailing path separator
14868 * if the argument has one. */
14869 if (remain == NULL && has_trailing_pathsep)
14870 add_pathsep(buf);
14871
14872 /* Separate the first path component in the link value and
14873 * concatenate the remainders. */
14874 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14875 if (*q != NUL)
14876 {
14877 if (remain == NULL)
14878 remain = vim_strsave(q - 1);
14879 else
14880 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014881 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882 if (cpy != NULL)
14883 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884 vim_free(remain);
14885 remain = cpy;
14886 }
14887 }
14888 q[-1] = NUL;
14889 }
14890
14891 q = gettail(p);
14892 if (q > p && *q == NUL)
14893 {
14894 /* Ignore trailing path separator. */
14895 q[-1] = NUL;
14896 q = gettail(p);
14897 }
14898 if (q > p && !mch_isFullName(buf))
14899 {
14900 /* symlink is relative to directory of argument */
14901 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14902 if (cpy != NULL)
14903 {
14904 STRCPY(cpy, p);
14905 STRCPY(gettail(cpy), buf);
14906 vim_free(p);
14907 p = cpy;
14908 }
14909 }
14910 else
14911 {
14912 vim_free(p);
14913 p = vim_strsave(buf);
14914 }
14915 }
14916
14917 if (remain == NULL)
14918 break;
14919
14920 /* Append the first path component of "remain" to "p". */
14921 q = getnextcomp(remain + 1);
14922 len = q - remain - (*q != NUL);
14923 cpy = vim_strnsave(p, STRLEN(p) + len);
14924 if (cpy != NULL)
14925 {
14926 STRNCAT(cpy, remain, len);
14927 vim_free(p);
14928 p = cpy;
14929 }
14930 /* Shorten "remain". */
14931 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014932 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 else
14934 {
14935 vim_free(remain);
14936 remain = NULL;
14937 }
14938 }
14939
14940 /* If the result is a relative path name, make it explicitly relative to
14941 * the current directory if and only if the argument had this form. */
14942 if (!vim_ispathsep(*p))
14943 {
14944 if (is_relative_to_current
14945 && *p != NUL
14946 && !(p[0] == '.'
14947 && (p[1] == NUL
14948 || vim_ispathsep(p[1])
14949 || (p[1] == '.'
14950 && (p[2] == NUL
14951 || vim_ispathsep(p[2]))))))
14952 {
14953 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014954 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955 if (cpy != NULL)
14956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957 vim_free(p);
14958 p = cpy;
14959 }
14960 }
14961 else if (!is_relative_to_current)
14962 {
14963 /* Strip leading "./". */
14964 q = p;
14965 while (q[0] == '.' && vim_ispathsep(q[1]))
14966 q += 2;
14967 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014968 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969 }
14970 }
14971
14972 /* Ensure that the result will have no trailing path separator
14973 * if the argument had none. But keep "/" or "//". */
14974 if (!has_trailing_pathsep)
14975 {
14976 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014977 if (after_pathsep(p, q))
14978 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 }
14980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014981 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982 }
14983# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014984 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985# endif
14986#endif
14987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014988 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989
14990#ifdef HAVE_READLINK
14991fail:
14992#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014993 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994}
14995
14996/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014997 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998 */
14999 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015000f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015001 typval_T *argvars;
15002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015003{
Bram Moolenaar33570922005-01-25 22:26:29 +000015004 list_T *l;
15005 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006
Bram Moolenaar0d660222005-01-07 21:51:51 +000015007 if (argvars[0].v_type != VAR_LIST)
15008 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015009 else if ((l = argvars[0].vval.v_list) != NULL
15010 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011 {
15012 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015013 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015014 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015015 while (li != NULL)
15016 {
15017 ni = li->li_prev;
15018 list_append(l, li);
15019 li = ni;
15020 }
15021 rettv->vval.v_list = l;
15022 rettv->v_type = VAR_LIST;
15023 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015024 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026}
15027
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015028#define SP_NOMOVE 0x01 /* don't move cursor */
15029#define SP_REPEAT 0x02 /* repeat to find outer pair */
15030#define SP_RETCOUNT 0x04 /* return matchcount */
15031#define SP_SETPCMARK 0x08 /* set previous context mark */
15032#define SP_START 0x10 /* accept match at start position */
15033#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15034#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015035
Bram Moolenaar33570922005-01-25 22:26:29 +000015036static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015037
15038/*
15039 * Get flags for a search function.
15040 * Possibly sets "p_ws".
15041 * Returns BACKWARD, FORWARD or zero (for an error).
15042 */
15043 static int
15044get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015045 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046 int *flagsp;
15047{
15048 int dir = FORWARD;
15049 char_u *flags;
15050 char_u nbuf[NUMBUFLEN];
15051 int mask;
15052
15053 if (varp->v_type != VAR_UNKNOWN)
15054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015055 flags = get_tv_string_buf_chk(varp, nbuf);
15056 if (flags == NULL)
15057 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015058 while (*flags != NUL)
15059 {
15060 switch (*flags)
15061 {
15062 case 'b': dir = BACKWARD; break;
15063 case 'w': p_ws = TRUE; break;
15064 case 'W': p_ws = FALSE; break;
15065 default: mask = 0;
15066 if (flagsp != NULL)
15067 switch (*flags)
15068 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015069 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015070 case 'e': mask = SP_END; break;
15071 case 'm': mask = SP_RETCOUNT; break;
15072 case 'n': mask = SP_NOMOVE; break;
15073 case 'p': mask = SP_SUBPAT; break;
15074 case 'r': mask = SP_REPEAT; break;
15075 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015076 }
15077 if (mask == 0)
15078 {
15079 EMSG2(_(e_invarg2), flags);
15080 dir = 0;
15081 }
15082 else
15083 *flagsp |= mask;
15084 }
15085 if (dir == 0)
15086 break;
15087 ++flags;
15088 }
15089 }
15090 return dir;
15091}
15092
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015094 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015096 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015097search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015098 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015099 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015100 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015102 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 char_u *pat;
15104 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015105 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015106 int save_p_ws = p_ws;
15107 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015108 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015109 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015110 proftime_T tm;
15111#ifdef FEAT_RELTIME
15112 long time_limit = 0;
15113#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015114 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015115 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015117 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015118 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015119 if (dir == 0)
15120 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015121 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015122 if (flags & SP_START)
15123 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015124 if (flags & SP_END)
15125 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015126
Bram Moolenaar76929292008-01-06 19:07:36 +000015127 /* Optional arguments: line number to stop searching and timeout. */
15128 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015129 {
15130 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15131 if (lnum_stop < 0)
15132 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015133#ifdef FEAT_RELTIME
15134 if (argvars[3].v_type != VAR_UNKNOWN)
15135 {
15136 time_limit = get_tv_number_chk(&argvars[3], NULL);
15137 if (time_limit < 0)
15138 goto theend;
15139 }
15140#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015141 }
15142
Bram Moolenaar76929292008-01-06 19:07:36 +000015143#ifdef FEAT_RELTIME
15144 /* Set the time limit, if there is one. */
15145 profile_setlimit(time_limit, &tm);
15146#endif
15147
Bram Moolenaar231334e2005-07-25 20:46:57 +000015148 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015149 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015150 * Check to make sure only those flags are set.
15151 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15152 * flags cannot be set. Check for that condition also.
15153 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015154 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015155 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015156 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015157 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015158 goto theend;
15159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015161 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015162 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015163 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015164 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015166 if (flags & SP_SUBPAT)
15167 retval = subpatnum;
15168 else
15169 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015170 if (flags & SP_SETPCMARK)
15171 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015173 if (match_pos != NULL)
15174 {
15175 /* Store the match cursor position */
15176 match_pos->lnum = pos.lnum;
15177 match_pos->col = pos.col + 1;
15178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179 /* "/$" will put the cursor after the end of the line, may need to
15180 * correct that here */
15181 check_cursor();
15182 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015183
15184 /* If 'n' flag is used: restore cursor position. */
15185 if (flags & SP_NOMOVE)
15186 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015187 else
15188 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015189theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015191
15192 return retval;
15193}
15194
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015195#ifdef FEAT_FLOAT
15196/*
15197 * "round({float})" function
15198 */
15199 static void
15200f_round(argvars, rettv)
15201 typval_T *argvars;
15202 typval_T *rettv;
15203{
15204 float_T f;
15205
15206 rettv->v_type = VAR_FLOAT;
15207 if (get_float_arg(argvars, &f) == OK)
15208 /* round() is not in C90, use ceil() or floor() instead. */
15209 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15210 else
15211 rettv->vval.v_float = 0.0;
15212}
15213#endif
15214
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015215/*
15216 * "search()" function
15217 */
15218 static void
15219f_search(argvars, rettv)
15220 typval_T *argvars;
15221 typval_T *rettv;
15222{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015223 int flags = 0;
15224
15225 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226}
15227
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015229 * "searchdecl()" function
15230 */
15231 static void
15232f_searchdecl(argvars, rettv)
15233 typval_T *argvars;
15234 typval_T *rettv;
15235{
15236 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015237 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015238 int error = FALSE;
15239 char_u *name;
15240
15241 rettv->vval.v_number = 1; /* default: FAIL */
15242
15243 name = get_tv_string_chk(&argvars[0]);
15244 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015245 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015246 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015247 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15248 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15249 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015250 if (!error && name != NULL)
15251 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015252 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015253}
15254
15255/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015256 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015258 static int
15259searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015260 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015261 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262{
15263 char_u *spat, *mpat, *epat;
15264 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266 int dir;
15267 int flags = 0;
15268 char_u nbuf1[NUMBUFLEN];
15269 char_u nbuf2[NUMBUFLEN];
15270 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015271 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015272 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015273 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015276 spat = get_tv_string_chk(&argvars[0]);
15277 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15278 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15279 if (spat == NULL || mpat == NULL || epat == NULL)
15280 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282 /* Handle the optional fourth argument: flags */
15283 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015284 if (dir == 0)
15285 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015286
15287 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015288 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15289 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015290 if ((flags & (SP_END | SP_SUBPAT)) != 0
15291 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015292 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015293 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015294 goto theend;
15295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015296
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015297 /* Using 'r' implies 'W', otherwise it doesn't work. */
15298 if (flags & SP_REPEAT)
15299 p_ws = FALSE;
15300
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015301 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015302 if (argvars[3].v_type == VAR_UNKNOWN
15303 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304 skip = (char_u *)"";
15305 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015307 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015308 if (argvars[5].v_type != VAR_UNKNOWN)
15309 {
15310 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15311 if (lnum_stop < 0)
15312 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015313#ifdef FEAT_RELTIME
15314 if (argvars[6].v_type != VAR_UNKNOWN)
15315 {
15316 time_limit = get_tv_number_chk(&argvars[6], NULL);
15317 if (time_limit < 0)
15318 goto theend;
15319 }
15320#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015321 }
15322 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015323 if (skip == NULL)
15324 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015326 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015327 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015328
15329theend:
15330 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015331
15332 return retval;
15333}
15334
15335/*
15336 * "searchpair()" function
15337 */
15338 static void
15339f_searchpair(argvars, rettv)
15340 typval_T *argvars;
15341 typval_T *rettv;
15342{
15343 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15344}
15345
15346/*
15347 * "searchpairpos()" function
15348 */
15349 static void
15350f_searchpairpos(argvars, rettv)
15351 typval_T *argvars;
15352 typval_T *rettv;
15353{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015354 pos_T match_pos;
15355 int lnum = 0;
15356 int col = 0;
15357
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015358 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015359 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015360
15361 if (searchpair_cmn(argvars, &match_pos) > 0)
15362 {
15363 lnum = match_pos.lnum;
15364 col = match_pos.col;
15365 }
15366
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015367 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15368 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015369}
15370
15371/*
15372 * Search for a start/middle/end thing.
15373 * Used by searchpair(), see its documentation for the details.
15374 * Returns 0 or -1 for no match,
15375 */
15376 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015377do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15378 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015379 char_u *spat; /* start pattern */
15380 char_u *mpat; /* middle pattern */
15381 char_u *epat; /* end pattern */
15382 int dir; /* BACKWARD or FORWARD */
15383 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015384 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015385 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015386 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015387 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015388{
15389 char_u *save_cpo;
15390 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15391 long retval = 0;
15392 pos_T pos;
15393 pos_T firstpos;
15394 pos_T foundpos;
15395 pos_T save_cursor;
15396 pos_T save_pos;
15397 int n;
15398 int r;
15399 int nest = 1;
15400 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015401 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015402 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015403
15404 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15405 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015406 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015407
Bram Moolenaar76929292008-01-06 19:07:36 +000015408#ifdef FEAT_RELTIME
15409 /* Set the time limit, if there is one. */
15410 profile_setlimit(time_limit, &tm);
15411#endif
15412
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015413 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15414 * start/middle/end (pat3, for the top pair). */
15415 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15416 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15417 if (pat2 == NULL || pat3 == NULL)
15418 goto theend;
15419 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15420 if (*mpat == NUL)
15421 STRCPY(pat3, pat2);
15422 else
15423 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15424 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015425 if (flags & SP_START)
15426 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015427
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428 save_cursor = curwin->w_cursor;
15429 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015430 clearpos(&firstpos);
15431 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432 pat = pat3;
15433 for (;;)
15434 {
15435 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015436 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15438 /* didn't find it or found the first match again: FAIL */
15439 break;
15440
15441 if (firstpos.lnum == 0)
15442 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015443 if (equalpos(pos, foundpos))
15444 {
15445 /* Found the same position again. Can happen with a pattern that
15446 * has "\zs" at the end and searching backwards. Advance one
15447 * character and try again. */
15448 if (dir == BACKWARD)
15449 decl(&pos);
15450 else
15451 incl(&pos);
15452 }
15453 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015455 /* clear the start flag to avoid getting stuck here */
15456 options &= ~SEARCH_START;
15457
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 /* If the skip pattern matches, ignore this match. */
15459 if (*skip != NUL)
15460 {
15461 save_pos = curwin->w_cursor;
15462 curwin->w_cursor = pos;
15463 r = eval_to_bool(skip, &err, NULL, FALSE);
15464 curwin->w_cursor = save_pos;
15465 if (err)
15466 {
15467 /* Evaluating {skip} caused an error, break here. */
15468 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015469 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470 break;
15471 }
15472 if (r)
15473 continue;
15474 }
15475
15476 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15477 {
15478 /* Found end when searching backwards or start when searching
15479 * forward: nested pair. */
15480 ++nest;
15481 pat = pat2; /* nested, don't search for middle */
15482 }
15483 else
15484 {
15485 /* Found end when searching forward or start when searching
15486 * backward: end of (nested) pair; or found middle in outer pair. */
15487 if (--nest == 1)
15488 pat = pat3; /* outer level, search for middle */
15489 }
15490
15491 if (nest == 0)
15492 {
15493 /* Found the match: return matchcount or line number. */
15494 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015495 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015497 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015498 if (flags & SP_SETPCMARK)
15499 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500 curwin->w_cursor = pos;
15501 if (!(flags & SP_REPEAT))
15502 break;
15503 nest = 1; /* search for next unmatched */
15504 }
15505 }
15506
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015507 if (match_pos != NULL)
15508 {
15509 /* Store the match cursor position */
15510 match_pos->lnum = curwin->w_cursor.lnum;
15511 match_pos->col = curwin->w_cursor.col + 1;
15512 }
15513
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015515 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 curwin->w_cursor = save_cursor;
15517
15518theend:
15519 vim_free(pat2);
15520 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015521 if (p_cpo == empty_option)
15522 p_cpo = save_cpo;
15523 else
15524 /* Darn, evaluating the {skip} expression changed the value. */
15525 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015526
15527 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528}
15529
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015530/*
15531 * "searchpos()" function
15532 */
15533 static void
15534f_searchpos(argvars, rettv)
15535 typval_T *argvars;
15536 typval_T *rettv;
15537{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015538 pos_T match_pos;
15539 int lnum = 0;
15540 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015541 int n;
15542 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015543
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015544 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015545 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015546
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015547 n = search_cmn(argvars, &match_pos, &flags);
15548 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015549 {
15550 lnum = match_pos.lnum;
15551 col = match_pos.col;
15552 }
15553
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015554 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15555 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015556 if (flags & SP_SUBPAT)
15557 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015558}
15559
15560
Bram Moolenaar0d660222005-01-07 21:51:51 +000015561 static void
15562f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015563 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015566#ifdef FEAT_CLIENTSERVER
15567 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015568 char_u *server = get_tv_string_chk(&argvars[0]);
15569 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570
Bram Moolenaar0d660222005-01-07 21:51:51 +000015571 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015572 if (server == NULL || reply == NULL)
15573 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015574 if (check_restricted() || check_secure())
15575 return;
15576# ifdef FEAT_X11
15577 if (check_connection() == FAIL)
15578 return;
15579# endif
15580
15581 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015583 EMSG(_("E258: Unable to send to client"));
15584 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015586 rettv->vval.v_number = 0;
15587#else
15588 rettv->vval.v_number = -1;
15589#endif
15590}
15591
Bram Moolenaar0d660222005-01-07 21:51:51 +000015592 static void
15593f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015594 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015595 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015596{
15597 char_u *r = NULL;
15598
15599#ifdef FEAT_CLIENTSERVER
15600# ifdef WIN32
15601 r = serverGetVimNames();
15602# else
15603 make_connection();
15604 if (X_DISPLAY != NULL)
15605 r = serverGetVimNames(X_DISPLAY);
15606# endif
15607#endif
15608 rettv->v_type = VAR_STRING;
15609 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610}
15611
15612/*
15613 * "setbufvar()" function
15614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015616f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015617 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015618 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619{
15620 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015623 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 char_u nbuf[NUMBUFLEN];
15625
15626 if (check_restricted() || check_secure())
15627 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015628 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15629 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015630 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631 varp = &argvars[2];
15632
15633 if (buf != NULL && varname != NULL && varp != NULL)
15634 {
15635 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637
15638 if (*varname == '&')
15639 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015640 long numval;
15641 char_u *strval;
15642 int error = FALSE;
15643
Bram Moolenaar071d4272004-06-13 20:20:40 +000015644 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015645 numval = get_tv_number_chk(varp, &error);
15646 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015647 if (!error && strval != NULL)
15648 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649 }
15650 else
15651 {
15652 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15653 if (bufvarname != NULL)
15654 {
15655 STRCPY(bufvarname, "b:");
15656 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015657 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 vim_free(bufvarname);
15659 }
15660 }
15661
15662 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665}
15666
15667/*
15668 * "setcmdpos()" function
15669 */
15670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015671f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015672 typval_T *argvars;
15673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015675 int pos = (int)get_tv_number(&argvars[0]) - 1;
15676
15677 if (pos >= 0)
15678 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679}
15680
15681/*
15682 * "setline()" function
15683 */
15684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015685f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015686 typval_T *argvars;
15687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688{
15689 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015690 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015691 list_T *l = NULL;
15692 listitem_T *li = NULL;
15693 long added = 0;
15694 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015696 lnum = get_tv_lnum(&argvars[0]);
15697 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015699 l = argvars[1].vval.v_list;
15700 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015702 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015703 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015704
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015705 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015706 for (;;)
15707 {
15708 if (l != NULL)
15709 {
15710 /* list argument, get next string */
15711 if (li == NULL)
15712 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015713 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015714 li = li->li_next;
15715 }
15716
15717 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015718 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015719 break;
15720 if (lnum <= curbuf->b_ml.ml_line_count)
15721 {
15722 /* existing line, replace it */
15723 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15724 {
15725 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015726 if (lnum == curwin->w_cursor.lnum)
15727 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015728 rettv->vval.v_number = 0; /* OK */
15729 }
15730 }
15731 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15732 {
15733 /* lnum is one past the last line, append the line */
15734 ++added;
15735 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15736 rettv->vval.v_number = 0; /* OK */
15737 }
15738
15739 if (l == NULL) /* only one string argument */
15740 break;
15741 ++lnum;
15742 }
15743
15744 if (added > 0)
15745 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746}
15747
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015748static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15749
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015751 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015752 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015753 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015754set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015755 win_T *wp UNUSED;
15756 typval_T *list_arg UNUSED;
15757 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015758 typval_T *rettv;
15759{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015760#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015761 char_u *act;
15762 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015763#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015764
Bram Moolenaar2641f772005-03-25 21:58:17 +000015765 rettv->vval.v_number = -1;
15766
15767#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015768 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015769 EMSG(_(e_listreq));
15770 else
15771 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015772 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015773
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015774 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015775 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015776 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015777 if (act == NULL)
15778 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015779 if (*act == 'a' || *act == 'r')
15780 action = *act;
15781 }
15782
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015783 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015784 rettv->vval.v_number = 0;
15785 }
15786#endif
15787}
15788
15789/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015790 * "setloclist()" function
15791 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015792 static void
15793f_setloclist(argvars, rettv)
15794 typval_T *argvars;
15795 typval_T *rettv;
15796{
15797 win_T *win;
15798
15799 rettv->vval.v_number = -1;
15800
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015801 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015802 if (win != NULL)
15803 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15804}
15805
15806/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015807 * "setmatches()" function
15808 */
15809 static void
15810f_setmatches(argvars, rettv)
15811 typval_T *argvars;
15812 typval_T *rettv;
15813{
15814#ifdef FEAT_SEARCH_EXTRA
15815 list_T *l;
15816 listitem_T *li;
15817 dict_T *d;
15818
15819 rettv->vval.v_number = -1;
15820 if (argvars[0].v_type != VAR_LIST)
15821 {
15822 EMSG(_(e_listreq));
15823 return;
15824 }
15825 if ((l = argvars[0].vval.v_list) != NULL)
15826 {
15827
15828 /* To some extent make sure that we are dealing with a list from
15829 * "getmatches()". */
15830 li = l->lv_first;
15831 while (li != NULL)
15832 {
15833 if (li->li_tv.v_type != VAR_DICT
15834 || (d = li->li_tv.vval.v_dict) == NULL)
15835 {
15836 EMSG(_(e_invarg));
15837 return;
15838 }
15839 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15840 && dict_find(d, (char_u *)"pattern", -1) != NULL
15841 && dict_find(d, (char_u *)"priority", -1) != NULL
15842 && dict_find(d, (char_u *)"id", -1) != NULL))
15843 {
15844 EMSG(_(e_invarg));
15845 return;
15846 }
15847 li = li->li_next;
15848 }
15849
15850 clear_matches(curwin);
15851 li = l->lv_first;
15852 while (li != NULL)
15853 {
15854 d = li->li_tv.vval.v_dict;
15855 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15856 get_dict_string(d, (char_u *)"pattern", FALSE),
15857 (int)get_dict_number(d, (char_u *)"priority"),
15858 (int)get_dict_number(d, (char_u *)"id"));
15859 li = li->li_next;
15860 }
15861 rettv->vval.v_number = 0;
15862 }
15863#endif
15864}
15865
15866/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015867 * "setpos()" function
15868 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015869 static void
15870f_setpos(argvars, rettv)
15871 typval_T *argvars;
15872 typval_T *rettv;
15873{
15874 pos_T pos;
15875 int fnum;
15876 char_u *name;
15877
Bram Moolenaar08250432008-02-13 11:42:46 +000015878 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015879 name = get_tv_string_chk(argvars);
15880 if (name != NULL)
15881 {
15882 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15883 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015884 if (--pos.col < 0)
15885 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015886 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015887 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015888 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015889 if (fnum == curbuf->b_fnum)
15890 {
15891 curwin->w_cursor = pos;
15892 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015893 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015894 }
15895 else
15896 EMSG(_(e_invarg));
15897 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015898 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15899 {
15900 /* set mark */
15901 if (setmark_pos(name[1], &pos, fnum) == OK)
15902 rettv->vval.v_number = 0;
15903 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015904 else
15905 EMSG(_(e_invarg));
15906 }
15907 }
15908}
15909
15910/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015911 * "setqflist()" function
15912 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015913 static void
15914f_setqflist(argvars, rettv)
15915 typval_T *argvars;
15916 typval_T *rettv;
15917{
15918 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15919}
15920
15921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 * "setreg()" function
15923 */
15924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015925f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015926 typval_T *argvars;
15927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928{
15929 int regname;
15930 char_u *strregname;
15931 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015932 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933 int append;
15934 char_u yank_type;
15935 long block_len;
15936
15937 block_len = -1;
15938 yank_type = MAUTO;
15939 append = FALSE;
15940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015941 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015942 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015944 if (strregname == NULL)
15945 return; /* type error; errmsg already given */
15946 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947 if (regname == 0 || regname == '@')
15948 regname = '"';
15949 else if (regname == '=')
15950 return;
15951
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015952 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015954 stropt = get_tv_string_chk(&argvars[2]);
15955 if (stropt == NULL)
15956 return; /* type error */
15957 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015958 switch (*stropt)
15959 {
15960 case 'a': case 'A': /* append */
15961 append = TRUE;
15962 break;
15963 case 'v': case 'c': /* character-wise selection */
15964 yank_type = MCHAR;
15965 break;
15966 case 'V': case 'l': /* line-wise selection */
15967 yank_type = MLINE;
15968 break;
15969#ifdef FEAT_VISUAL
15970 case 'b': case Ctrl_V: /* block-wise selection */
15971 yank_type = MBLOCK;
15972 if (VIM_ISDIGIT(stropt[1]))
15973 {
15974 ++stropt;
15975 block_len = getdigits(&stropt) - 1;
15976 --stropt;
15977 }
15978 break;
15979#endif
15980 }
15981 }
15982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015983 strval = get_tv_string_chk(&argvars[1]);
15984 if (strval != NULL)
15985 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015987 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988}
15989
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015990/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020015991 * "settabvar()" function
15992 */
15993 static void
15994f_settabvar(argvars, rettv)
15995 typval_T *argvars;
15996 typval_T *rettv;
15997{
15998 tabpage_T *save_curtab;
15999 char_u *varname, *tabvarname;
16000 typval_T *varp;
16001 tabpage_T *tp;
16002
16003 rettv->vval.v_number = 0;
16004
16005 if (check_restricted() || check_secure())
16006 return;
16007
16008 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16009 varname = get_tv_string_chk(&argvars[1]);
16010 varp = &argvars[2];
16011
16012 if (tp != NULL && varname != NULL && varp != NULL)
16013 {
16014 save_curtab = curtab;
16015 goto_tabpage_tp(tp);
16016
16017 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16018 if (tabvarname != NULL)
16019 {
16020 STRCPY(tabvarname, "t:");
16021 STRCPY(tabvarname + 2, varname);
16022 set_var(tabvarname, varp, TRUE);
16023 vim_free(tabvarname);
16024 }
16025
16026 /* Restore current tabpage */
16027 if (valid_tabpage(save_curtab))
16028 goto_tabpage_tp(save_curtab);
16029 }
16030}
16031
16032/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016033 * "settabwinvar()" function
16034 */
16035 static void
16036f_settabwinvar(argvars, rettv)
16037 typval_T *argvars;
16038 typval_T *rettv;
16039{
16040 setwinvar(argvars, rettv, 1);
16041}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042
16043/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016044 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016047f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016048 typval_T *argvars;
16049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016051 setwinvar(argvars, rettv, 0);
16052}
16053
16054/*
16055 * "setwinvar()" and "settabwinvar()" functions
16056 */
16057 static void
16058setwinvar(argvars, rettv, off)
16059 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016060 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016061 int off;
16062{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063 win_T *win;
16064#ifdef FEAT_WINDOWS
16065 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016066 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067#endif
16068 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016069 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016071 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072
16073 if (check_restricted() || check_secure())
16074 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016075
16076#ifdef FEAT_WINDOWS
16077 if (off == 1)
16078 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16079 else
16080 tp = curtab;
16081#endif
16082 win = find_win_by_nr(&argvars[off], tp);
16083 varname = get_tv_string_chk(&argvars[off + 1]);
16084 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085
16086 if (win != NULL && varname != NULL && varp != NULL)
16087 {
16088#ifdef FEAT_WINDOWS
16089 /* set curwin to be our win, temporarily */
16090 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016091 save_curtab = curtab;
16092 goto_tabpage_tp(tp);
16093 if (!win_valid(win))
16094 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 curwin = win;
16096 curbuf = curwin->w_buffer;
16097#endif
16098
16099 if (*varname == '&')
16100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016101 long numval;
16102 char_u *strval;
16103 int error = FALSE;
16104
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016106 numval = get_tv_number_chk(varp, &error);
16107 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016108 if (!error && strval != NULL)
16109 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110 }
16111 else
16112 {
16113 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16114 if (winvarname != NULL)
16115 {
16116 STRCPY(winvarname, "w:");
16117 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016118 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 vim_free(winvarname);
16120 }
16121 }
16122
16123#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016124 /* Restore current tabpage and window, if still valid (autocomands can
16125 * make them invalid). */
16126 if (valid_tabpage(save_curtab))
16127 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 if (win_valid(save_curwin))
16129 {
16130 curwin = save_curwin;
16131 curbuf = curwin->w_buffer;
16132 }
16133#endif
16134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135}
16136
16137/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016138 * "shellescape({string})" function
16139 */
16140 static void
16141f_shellescape(argvars, rettv)
16142 typval_T *argvars;
16143 typval_T *rettv;
16144{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016145 rettv->vval.v_string = vim_strsave_shellescape(
16146 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016147 rettv->v_type = VAR_STRING;
16148}
16149
16150/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016151 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016152 */
16153 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016154f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016155 typval_T *argvars;
16156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016158 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160 p = get_tv_string(&argvars[0]);
16161 rettv->vval.v_string = vim_strsave(p);
16162 simplify_filename(rettv->vval.v_string); /* simplify in place */
16163 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164}
16165
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016166#ifdef FEAT_FLOAT
16167/*
16168 * "sin()" function
16169 */
16170 static void
16171f_sin(argvars, rettv)
16172 typval_T *argvars;
16173 typval_T *rettv;
16174{
16175 float_T f;
16176
16177 rettv->v_type = VAR_FLOAT;
16178 if (get_float_arg(argvars, &f) == OK)
16179 rettv->vval.v_float = sin(f);
16180 else
16181 rettv->vval.v_float = 0.0;
16182}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016183
16184/*
16185 * "sinh()" function
16186 */
16187 static void
16188f_sinh(argvars, rettv)
16189 typval_T *argvars;
16190 typval_T *rettv;
16191{
16192 float_T f;
16193
16194 rettv->v_type = VAR_FLOAT;
16195 if (get_float_arg(argvars, &f) == OK)
16196 rettv->vval.v_float = sinh(f);
16197 else
16198 rettv->vval.v_float = 0.0;
16199}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016200#endif
16201
Bram Moolenaar0d660222005-01-07 21:51:51 +000016202static int
16203#ifdef __BORLANDC__
16204 _RTLENTRYF
16205#endif
16206 item_compare __ARGS((const void *s1, const void *s2));
16207static int
16208#ifdef __BORLANDC__
16209 _RTLENTRYF
16210#endif
16211 item_compare2 __ARGS((const void *s1, const void *s2));
16212
16213static int item_compare_ic;
16214static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016215static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016216#define ITEM_COMPARE_FAIL 999
16217
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016221 static int
16222#ifdef __BORLANDC__
16223_RTLENTRYF
16224#endif
16225item_compare(s1, s2)
16226 const void *s1;
16227 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016229 char_u *p1, *p2;
16230 char_u *tofree1, *tofree2;
16231 int res;
16232 char_u numbuf1[NUMBUFLEN];
16233 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016235 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16236 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016237 if (p1 == NULL)
16238 p1 = (char_u *)"";
16239 if (p2 == NULL)
16240 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016241 if (item_compare_ic)
16242 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244 res = STRCMP(p1, p2);
16245 vim_free(tofree1);
16246 vim_free(tofree2);
16247 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248}
16249
16250 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016251#ifdef __BORLANDC__
16252_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016254item_compare2(s1, s2)
16255 const void *s1;
16256 const void *s2;
16257{
16258 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016259 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016260 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016261 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016263 /* shortcut after failure in previous call; compare all items equal */
16264 if (item_compare_func_err)
16265 return 0;
16266
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016267 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16268 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16270 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016271
16272 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016273 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016274 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016275 clear_tv(&argv[0]);
16276 clear_tv(&argv[1]);
16277
16278 if (res == FAIL)
16279 res = ITEM_COMPARE_FAIL;
16280 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016281 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16282 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016283 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016284 clear_tv(&rettv);
16285 return res;
16286}
16287
16288/*
16289 * "sort({list})" function
16290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016292f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016293 typval_T *argvars;
16294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295{
Bram Moolenaar33570922005-01-25 22:26:29 +000016296 list_T *l;
16297 listitem_T *li;
16298 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016299 long len;
16300 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301
Bram Moolenaar0d660222005-01-07 21:51:51 +000016302 if (argvars[0].v_type != VAR_LIST)
16303 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 else
16305 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016306 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016307 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308 return;
16309 rettv->vval.v_list = l;
16310 rettv->v_type = VAR_LIST;
16311 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312
Bram Moolenaar0d660222005-01-07 21:51:51 +000016313 len = list_len(l);
16314 if (len <= 1)
16315 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316
Bram Moolenaar0d660222005-01-07 21:51:51 +000016317 item_compare_ic = FALSE;
16318 item_compare_func = NULL;
16319 if (argvars[1].v_type != VAR_UNKNOWN)
16320 {
16321 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016322 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016323 else
16324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016325 int error = FALSE;
16326
16327 i = get_tv_number_chk(&argvars[1], &error);
16328 if (error)
16329 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016330 if (i == 1)
16331 item_compare_ic = TRUE;
16332 else
16333 item_compare_func = get_tv_string(&argvars[1]);
16334 }
16335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336
Bram Moolenaar0d660222005-01-07 21:51:51 +000016337 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016338 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016339 if (ptrs == NULL)
16340 return;
16341 i = 0;
16342 for (li = l->lv_first; li != NULL; li = li->li_next)
16343 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016345 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016346 /* test the compare function */
16347 if (item_compare_func != NULL
16348 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16349 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016350 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016352 {
16353 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016354 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016355 item_compare_func == NULL ? item_compare : item_compare2);
16356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016357 if (!item_compare_func_err)
16358 {
16359 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016360 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016361 l->lv_len = 0;
16362 for (i = 0; i < len; ++i)
16363 list_append(l, ptrs[i]);
16364 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016365 }
16366
16367 vim_free(ptrs);
16368 }
16369}
16370
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016371/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016372 * "soundfold({word})" function
16373 */
16374 static void
16375f_soundfold(argvars, rettv)
16376 typval_T *argvars;
16377 typval_T *rettv;
16378{
16379 char_u *s;
16380
16381 rettv->v_type = VAR_STRING;
16382 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016383#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016384 rettv->vval.v_string = eval_soundfold(s);
16385#else
16386 rettv->vval.v_string = vim_strsave(s);
16387#endif
16388}
16389
16390/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016391 * "spellbadword()" function
16392 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016393 static void
16394f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016395 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016396 typval_T *rettv;
16397{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016398 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016399 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016400 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016401
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016402 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016403 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016404
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016405#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016406 if (argvars[0].v_type == VAR_UNKNOWN)
16407 {
16408 /* Find the start and length of the badly spelled word. */
16409 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16410 if (len != 0)
16411 word = ml_get_cursor();
16412 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016413 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016414 {
16415 char_u *str = get_tv_string_chk(&argvars[0]);
16416 int capcol = -1;
16417
16418 if (str != NULL)
16419 {
16420 /* Check the argument for spelling. */
16421 while (*str != NUL)
16422 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016423 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016424 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016425 {
16426 word = str;
16427 break;
16428 }
16429 str += len;
16430 }
16431 }
16432 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016433#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016434
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016435 list_append_string(rettv->vval.v_list, word, len);
16436 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016437 attr == HLF_SPB ? "bad" :
16438 attr == HLF_SPR ? "rare" :
16439 attr == HLF_SPL ? "local" :
16440 attr == HLF_SPC ? "caps" :
16441 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016442}
16443
16444/*
16445 * "spellsuggest()" function
16446 */
16447 static void
16448f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016449 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016450 typval_T *rettv;
16451{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016452#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016453 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016454 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016455 int maxcount;
16456 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016457 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016458 listitem_T *li;
16459 int need_capital = FALSE;
16460#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016461
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016462 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016463 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016464
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016465#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016466 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016467 {
16468 str = get_tv_string(&argvars[0]);
16469 if (argvars[1].v_type != VAR_UNKNOWN)
16470 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016471 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016472 if (maxcount <= 0)
16473 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016474 if (argvars[2].v_type != VAR_UNKNOWN)
16475 {
16476 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16477 if (typeerr)
16478 return;
16479 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016480 }
16481 else
16482 maxcount = 25;
16483
Bram Moolenaar4770d092006-01-12 23:22:24 +000016484 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016485
16486 for (i = 0; i < ga.ga_len; ++i)
16487 {
16488 str = ((char_u **)ga.ga_data)[i];
16489
16490 li = listitem_alloc();
16491 if (li == NULL)
16492 vim_free(str);
16493 else
16494 {
16495 li->li_tv.v_type = VAR_STRING;
16496 li->li_tv.v_lock = 0;
16497 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016498 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016499 }
16500 }
16501 ga_clear(&ga);
16502 }
16503#endif
16504}
16505
Bram Moolenaar0d660222005-01-07 21:51:51 +000016506 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016507f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016508 typval_T *argvars;
16509 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016510{
16511 char_u *str;
16512 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016513 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016514 regmatch_T regmatch;
16515 char_u patbuf[NUMBUFLEN];
16516 char_u *save_cpo;
16517 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016518 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016519 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016520 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016521
16522 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16523 save_cpo = p_cpo;
16524 p_cpo = (char_u *)"";
16525
16526 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016527 if (argvars[1].v_type != VAR_UNKNOWN)
16528 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016529 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16530 if (pat == NULL)
16531 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016532 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016533 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016534 }
16535 if (pat == NULL || *pat == NUL)
16536 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016537
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016538 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016540 if (typeerr)
16541 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542
Bram Moolenaar0d660222005-01-07 21:51:51 +000016543 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16544 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016546 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016547 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016548 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016549 if (*str == NUL)
16550 match = FALSE; /* empty item at the end */
16551 else
16552 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016553 if (match)
16554 end = regmatch.startp[0];
16555 else
16556 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016557 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16558 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016559 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016560 if (list_append_string(rettv->vval.v_list, str,
16561 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016562 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016563 }
16564 if (!match)
16565 break;
16566 /* Advance to just after the match. */
16567 if (regmatch.endp[0] > str)
16568 col = 0;
16569 else
16570 {
16571 /* Don't get stuck at the same match. */
16572#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016573 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016574#else
16575 col = 1;
16576#endif
16577 }
16578 str = regmatch.endp[0];
16579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580
Bram Moolenaar0d660222005-01-07 21:51:51 +000016581 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583
Bram Moolenaar0d660222005-01-07 21:51:51 +000016584 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585}
16586
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016587#ifdef FEAT_FLOAT
16588/*
16589 * "sqrt()" function
16590 */
16591 static void
16592f_sqrt(argvars, rettv)
16593 typval_T *argvars;
16594 typval_T *rettv;
16595{
16596 float_T f;
16597
16598 rettv->v_type = VAR_FLOAT;
16599 if (get_float_arg(argvars, &f) == OK)
16600 rettv->vval.v_float = sqrt(f);
16601 else
16602 rettv->vval.v_float = 0.0;
16603}
16604
16605/*
16606 * "str2float()" function
16607 */
16608 static void
16609f_str2float(argvars, rettv)
16610 typval_T *argvars;
16611 typval_T *rettv;
16612{
16613 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16614
16615 if (*p == '+')
16616 p = skipwhite(p + 1);
16617 (void)string2float(p, &rettv->vval.v_float);
16618 rettv->v_type = VAR_FLOAT;
16619}
16620#endif
16621
Bram Moolenaar2c932302006-03-18 21:42:09 +000016622/*
16623 * "str2nr()" function
16624 */
16625 static void
16626f_str2nr(argvars, rettv)
16627 typval_T *argvars;
16628 typval_T *rettv;
16629{
16630 int base = 10;
16631 char_u *p;
16632 long n;
16633
16634 if (argvars[1].v_type != VAR_UNKNOWN)
16635 {
16636 base = get_tv_number(&argvars[1]);
16637 if (base != 8 && base != 10 && base != 16)
16638 {
16639 EMSG(_(e_invarg));
16640 return;
16641 }
16642 }
16643
16644 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016645 if (*p == '+')
16646 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016647 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16648 rettv->vval.v_number = n;
16649}
16650
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651#ifdef HAVE_STRFTIME
16652/*
16653 * "strftime({format}[, {time}])" function
16654 */
16655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016656f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016657 typval_T *argvars;
16658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659{
16660 char_u result_buf[256];
16661 struct tm *curtime;
16662 time_t seconds;
16663 char_u *p;
16664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016665 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016667 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016668 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669 seconds = time(NULL);
16670 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016671 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672 curtime = localtime(&seconds);
16673 /* MSVC returns NULL for an invalid value of seconds. */
16674 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016675 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016676 else
16677 {
16678# ifdef FEAT_MBYTE
16679 vimconv_T conv;
16680 char_u *enc;
16681
16682 conv.vc_type = CONV_NONE;
16683 enc = enc_locale();
16684 convert_setup(&conv, p_enc, enc);
16685 if (conv.vc_type != CONV_NONE)
16686 p = string_convert(&conv, p, NULL);
16687# endif
16688 if (p != NULL)
16689 (void)strftime((char *)result_buf, sizeof(result_buf),
16690 (char *)p, curtime);
16691 else
16692 result_buf[0] = NUL;
16693
16694# ifdef FEAT_MBYTE
16695 if (conv.vc_type != CONV_NONE)
16696 vim_free(p);
16697 convert_setup(&conv, enc, p_enc);
16698 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016699 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700 else
16701# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016702 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703
16704# ifdef FEAT_MBYTE
16705 /* Release conversion descriptors */
16706 convert_setup(&conv, NULL, NULL);
16707 vim_free(enc);
16708# endif
16709 }
16710}
16711#endif
16712
16713/*
16714 * "stridx()" function
16715 */
16716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016717f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016718 typval_T *argvars;
16719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720{
16721 char_u buf[NUMBUFLEN];
16722 char_u *needle;
16723 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016724 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016726 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016728 needle = get_tv_string_chk(&argvars[1]);
16729 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016730 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016731 if (needle == NULL || haystack == NULL)
16732 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733
Bram Moolenaar33570922005-01-25 22:26:29 +000016734 if (argvars[2].v_type != VAR_UNKNOWN)
16735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016736 int error = FALSE;
16737
16738 start_idx = get_tv_number_chk(&argvars[2], &error);
16739 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016740 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016741 if (start_idx >= 0)
16742 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016743 }
16744
16745 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16746 if (pos != NULL)
16747 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748}
16749
16750/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016751 * "string()" function
16752 */
16753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016754f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016755 typval_T *argvars;
16756 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016757{
16758 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016759 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016761 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016762 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016763 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016764 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016765 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766}
16767
16768/*
16769 * "strlen()" function
16770 */
16771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016772f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016773 typval_T *argvars;
16774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016776 rettv->vval.v_number = (varnumber_T)(STRLEN(
16777 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778}
16779
16780/*
16781 * "strpart()" function
16782 */
16783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016784f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016785 typval_T *argvars;
16786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787{
16788 char_u *p;
16789 int n;
16790 int len;
16791 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016792 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016794 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795 slen = (int)STRLEN(p);
16796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016797 n = get_tv_number_chk(&argvars[1], &error);
16798 if (error)
16799 len = 0;
16800 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016801 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 else
16803 len = slen - n; /* default len: all bytes that are available. */
16804
16805 /*
16806 * Only return the overlap between the specified part and the actual
16807 * string.
16808 */
16809 if (n < 0)
16810 {
16811 len += n;
16812 n = 0;
16813 }
16814 else if (n > slen)
16815 n = slen;
16816 if (len < 0)
16817 len = 0;
16818 else if (n + len > slen)
16819 len = slen - n;
16820
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016821 rettv->v_type = VAR_STRING;
16822 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823}
16824
16825/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016826 * "strridx()" function
16827 */
16828 static void
16829f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016830 typval_T *argvars;
16831 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016832{
16833 char_u buf[NUMBUFLEN];
16834 char_u *needle;
16835 char_u *haystack;
16836 char_u *rest;
16837 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016838 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016840 needle = get_tv_string_chk(&argvars[1]);
16841 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016842
16843 rettv->vval.v_number = -1;
16844 if (needle == NULL || haystack == NULL)
16845 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016846
16847 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016848 if (argvars[2].v_type != VAR_UNKNOWN)
16849 {
16850 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016851 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016852 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016853 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016854 }
16855 else
16856 end_idx = haystack_len;
16857
Bram Moolenaar0d660222005-01-07 21:51:51 +000016858 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016859 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016860 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016861 lastmatch = haystack + end_idx;
16862 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016863 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016864 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865 for (rest = haystack; *rest != '\0'; ++rest)
16866 {
16867 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016868 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016869 break;
16870 lastmatch = rest;
16871 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016872 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873
16874 if (lastmatch == NULL)
16875 rettv->vval.v_number = -1;
16876 else
16877 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16878}
16879
16880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881 * "strtrans()" function
16882 */
16883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016884f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016885 typval_T *argvars;
16886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016888 rettv->v_type = VAR_STRING;
16889 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890}
16891
16892/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016893 * "submatch()" function
16894 */
16895 static void
16896f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016897 typval_T *argvars;
16898 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016899{
16900 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016901 rettv->vval.v_string =
16902 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016903}
16904
16905/*
16906 * "substitute()" function
16907 */
16908 static void
16909f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016910 typval_T *argvars;
16911 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016912{
16913 char_u patbuf[NUMBUFLEN];
16914 char_u subbuf[NUMBUFLEN];
16915 char_u flagsbuf[NUMBUFLEN];
16916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016917 char_u *str = get_tv_string_chk(&argvars[0]);
16918 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16919 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
16920 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
16921
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016923 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
16924 rettv->vval.v_string = NULL;
16925 else
16926 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016927}
16928
16929/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016930 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016933f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016934 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936{
16937 int id = 0;
16938#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016939 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940 long col;
16941 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016942 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016944 lnum = get_tv_lnum(argvars); /* -1 on type error */
16945 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
16946 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016948 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000016949 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000016950 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951#endif
16952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016953 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954}
16955
16956/*
16957 * "synIDattr(id, what [, mode])" function
16958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016960f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016961 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963{
16964 char_u *p = NULL;
16965#ifdef FEAT_SYN_HL
16966 int id;
16967 char_u *what;
16968 char_u *mode;
16969 char_u modebuf[NUMBUFLEN];
16970 int modec;
16971
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016972 id = get_tv_number(&argvars[0]);
16973 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016974 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016976 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020016978 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 modec = 0; /* replace invalid with current */
16980 }
16981 else
16982 {
16983#ifdef FEAT_GUI
16984 if (gui.in_use)
16985 modec = 'g';
16986 else
16987#endif
16988 if (t_colors > 1)
16989 modec = 'c';
16990 else
16991 modec = 't';
16992 }
16993
16994
16995 switch (TOLOWER_ASC(what[0]))
16996 {
16997 case 'b':
16998 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
16999 p = highlight_color(id, what, modec);
17000 else /* bold */
17001 p = highlight_has_attr(id, HL_BOLD, modec);
17002 break;
17003
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017004 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 p = highlight_color(id, what, modec);
17006 break;
17007
17008 case 'i':
17009 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17010 p = highlight_has_attr(id, HL_INVERSE, modec);
17011 else /* italic */
17012 p = highlight_has_attr(id, HL_ITALIC, modec);
17013 break;
17014
17015 case 'n': /* name */
17016 p = get_highlight_name(NULL, id - 1);
17017 break;
17018
17019 case 'r': /* reverse */
17020 p = highlight_has_attr(id, HL_INVERSE, modec);
17021 break;
17022
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017023 case 's':
17024 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17025 p = highlight_color(id, what, modec);
17026 else /* standout */
17027 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 break;
17029
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017030 case 'u':
17031 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17032 /* underline */
17033 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17034 else
17035 /* undercurl */
17036 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 break;
17038 }
17039
17040 if (p != NULL)
17041 p = vim_strsave(p);
17042#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017043 rettv->v_type = VAR_STRING;
17044 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045}
17046
17047/*
17048 * "synIDtrans(id)" function
17049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017051f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017052 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054{
17055 int id;
17056
17057#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017058 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059
17060 if (id > 0)
17061 id = syn_get_final_id(id);
17062 else
17063#endif
17064 id = 0;
17065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017066 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067}
17068
17069/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017070 * "synstack(lnum, col)" function
17071 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017072 static void
17073f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017074 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017075 typval_T *rettv;
17076{
17077#ifdef FEAT_SYN_HL
17078 long lnum;
17079 long col;
17080 int i;
17081 int id;
17082#endif
17083
17084 rettv->v_type = VAR_LIST;
17085 rettv->vval.v_list = NULL;
17086
17087#ifdef FEAT_SYN_HL
17088 lnum = get_tv_lnum(argvars); /* -1 on type error */
17089 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17090
17091 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017092 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017093 && rettv_list_alloc(rettv) != FAIL)
17094 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017095 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017096 for (i = 0; ; ++i)
17097 {
17098 id = syn_get_stack_item(i);
17099 if (id < 0)
17100 break;
17101 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17102 break;
17103 }
17104 }
17105#endif
17106}
17107
17108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 * "system()" function
17110 */
17111 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017112f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017113 typval_T *argvars;
17114 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017116 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017118 char_u *infile = NULL;
17119 char_u buf[NUMBUFLEN];
17120 int err = FALSE;
17121 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017123 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017124 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017125
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017126 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017127 {
17128 /*
17129 * Write the string to a temp file, to be used for input of the shell
17130 * command.
17131 */
17132 if ((infile = vim_tempname('i')) == NULL)
17133 {
17134 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017135 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017136 }
17137
17138 fd = mch_fopen((char *)infile, WRITEBIN);
17139 if (fd == NULL)
17140 {
17141 EMSG2(_(e_notopen), infile);
17142 goto done;
17143 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017144 p = get_tv_string_buf_chk(&argvars[1], buf);
17145 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017146 {
17147 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017148 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017149 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017150 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17151 err = TRUE;
17152 if (fclose(fd) != 0)
17153 err = TRUE;
17154 if (err)
17155 {
17156 EMSG(_("E677: Error writing temp file"));
17157 goto done;
17158 }
17159 }
17160
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017161 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17162 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017163
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164#ifdef USE_CR
17165 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017166 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 {
17168 char_u *s;
17169
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017170 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 {
17172 if (*s == CAR)
17173 *s = NL;
17174 }
17175 }
17176#else
17177# ifdef USE_CRNL
17178 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017179 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 {
17181 char_u *s, *d;
17182
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017183 d = res;
17184 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 {
17186 if (s[0] == CAR && s[1] == NL)
17187 ++s;
17188 *d++ = *s;
17189 }
17190 *d = NUL;
17191 }
17192# endif
17193#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017194
17195done:
17196 if (infile != NULL)
17197 {
17198 mch_remove(infile);
17199 vim_free(infile);
17200 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017201 rettv->v_type = VAR_STRING;
17202 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203}
17204
17205/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017206 * "tabpagebuflist()" function
17207 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017208 static void
17209f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017210 typval_T *argvars UNUSED;
17211 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017212{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017213#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017214 tabpage_T *tp;
17215 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017216
17217 if (argvars[0].v_type == VAR_UNKNOWN)
17218 wp = firstwin;
17219 else
17220 {
17221 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17222 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017223 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017224 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017225 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017226 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017227 for (; wp != NULL; wp = wp->w_next)
17228 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017229 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017230 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017231 }
17232#endif
17233}
17234
17235
17236/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017237 * "tabpagenr()" function
17238 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017239 static void
17240f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017241 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017242 typval_T *rettv;
17243{
17244 int nr = 1;
17245#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017246 char_u *arg;
17247
17248 if (argvars[0].v_type != VAR_UNKNOWN)
17249 {
17250 arg = get_tv_string_chk(&argvars[0]);
17251 nr = 0;
17252 if (arg != NULL)
17253 {
17254 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017255 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017256 else
17257 EMSG2(_(e_invexpr2), arg);
17258 }
17259 }
17260 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017261 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017262#endif
17263 rettv->vval.v_number = nr;
17264}
17265
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017266
17267#ifdef FEAT_WINDOWS
17268static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17269
17270/*
17271 * Common code for tabpagewinnr() and winnr().
17272 */
17273 static int
17274get_winnr(tp, argvar)
17275 tabpage_T *tp;
17276 typval_T *argvar;
17277{
17278 win_T *twin;
17279 int nr = 1;
17280 win_T *wp;
17281 char_u *arg;
17282
17283 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17284 if (argvar->v_type != VAR_UNKNOWN)
17285 {
17286 arg = get_tv_string_chk(argvar);
17287 if (arg == NULL)
17288 nr = 0; /* type error; errmsg already given */
17289 else if (STRCMP(arg, "$") == 0)
17290 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17291 else if (STRCMP(arg, "#") == 0)
17292 {
17293 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17294 if (twin == NULL)
17295 nr = 0;
17296 }
17297 else
17298 {
17299 EMSG2(_(e_invexpr2), arg);
17300 nr = 0;
17301 }
17302 }
17303
17304 if (nr > 0)
17305 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17306 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017307 {
17308 if (wp == NULL)
17309 {
17310 /* didn't find it in this tabpage */
17311 nr = 0;
17312 break;
17313 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017314 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017315 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017316 return nr;
17317}
17318#endif
17319
17320/*
17321 * "tabpagewinnr()" function
17322 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017323 static void
17324f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017325 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017326 typval_T *rettv;
17327{
17328 int nr = 1;
17329#ifdef FEAT_WINDOWS
17330 tabpage_T *tp;
17331
17332 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17333 if (tp == NULL)
17334 nr = 0;
17335 else
17336 nr = get_winnr(tp, &argvars[1]);
17337#endif
17338 rettv->vval.v_number = nr;
17339}
17340
17341
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017342/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017343 * "tagfiles()" function
17344 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017345 static void
17346f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017347 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017348 typval_T *rettv;
17349{
17350 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017351 tagname_T tn;
17352 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017353
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017354 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017355 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017356
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017357 for (first = TRUE; ; first = FALSE)
17358 if (get_tagfname(&tn, first, fname) == FAIL
17359 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017360 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017361 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017362}
17363
17364/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017365 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017366 */
17367 static void
17368f_taglist(argvars, rettv)
17369 typval_T *argvars;
17370 typval_T *rettv;
17371{
17372 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017373
17374 tag_pattern = get_tv_string(&argvars[0]);
17375
17376 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017377 if (*tag_pattern == NUL)
17378 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017379
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017380 if (rettv_list_alloc(rettv) == OK)
17381 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017382}
17383
17384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 * "tempname()" function
17386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017389 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391{
17392 static int x = 'A';
17393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017394 rettv->v_type = VAR_STRING;
17395 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396
17397 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17398 * names. Skip 'I' and 'O', they are used for shell redirection. */
17399 do
17400 {
17401 if (x == 'Z')
17402 x = '0';
17403 else if (x == '9')
17404 x = 'A';
17405 else
17406 {
17407#ifdef EBCDIC
17408 if (x == 'I')
17409 x = 'J';
17410 else if (x == 'R')
17411 x = 'S';
17412 else
17413#endif
17414 ++x;
17415 }
17416 } while (x == 'I' || x == 'O');
17417}
17418
17419/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017420 * "test(list)" function: Just checking the walls...
17421 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017422 static void
17423f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017424 typval_T *argvars UNUSED;
17425 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017426{
17427 /* Used for unit testing. Change the code below to your liking. */
17428#if 0
17429 listitem_T *li;
17430 list_T *l;
17431 char_u *bad, *good;
17432
17433 if (argvars[0].v_type != VAR_LIST)
17434 return;
17435 l = argvars[0].vval.v_list;
17436 if (l == NULL)
17437 return;
17438 li = l->lv_first;
17439 if (li == NULL)
17440 return;
17441 bad = get_tv_string(&li->li_tv);
17442 li = li->li_next;
17443 if (li == NULL)
17444 return;
17445 good = get_tv_string(&li->li_tv);
17446 rettv->vval.v_number = test_edit_score(bad, good);
17447#endif
17448}
17449
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017450#ifdef FEAT_FLOAT
17451/*
17452 * "tan()" function
17453 */
17454 static void
17455f_tan(argvars, rettv)
17456 typval_T *argvars;
17457 typval_T *rettv;
17458{
17459 float_T f;
17460
17461 rettv->v_type = VAR_FLOAT;
17462 if (get_float_arg(argvars, &f) == OK)
17463 rettv->vval.v_float = tan(f);
17464 else
17465 rettv->vval.v_float = 0.0;
17466}
17467
17468/*
17469 * "tanh()" function
17470 */
17471 static void
17472f_tanh(argvars, rettv)
17473 typval_T *argvars;
17474 typval_T *rettv;
17475{
17476 float_T f;
17477
17478 rettv->v_type = VAR_FLOAT;
17479 if (get_float_arg(argvars, &f) == OK)
17480 rettv->vval.v_float = tanh(f);
17481 else
17482 rettv->vval.v_float = 0.0;
17483}
17484#endif
17485
Bram Moolenaard52d9742005-08-21 22:20:28 +000017486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 * "tolower(string)" function
17488 */
17489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017491 typval_T *argvars;
17492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493{
17494 char_u *p;
17495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017496 p = vim_strsave(get_tv_string(&argvars[0]));
17497 rettv->v_type = VAR_STRING;
17498 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499
17500 if (p != NULL)
17501 while (*p != NUL)
17502 {
17503#ifdef FEAT_MBYTE
17504 int l;
17505
17506 if (enc_utf8)
17507 {
17508 int c, lc;
17509
17510 c = utf_ptr2char(p);
17511 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017512 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513 /* TODO: reallocate string when byte count changes. */
17514 if (utf_char2len(lc) == l)
17515 utf_char2bytes(lc, p);
17516 p += l;
17517 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017518 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 p += l; /* skip multi-byte character */
17520 else
17521#endif
17522 {
17523 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17524 ++p;
17525 }
17526 }
17527}
17528
17529/*
17530 * "toupper(string)" function
17531 */
17532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017533f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 typval_T *argvars;
17535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017537 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017538 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539}
17540
17541/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017542 * "tr(string, fromstr, tostr)" function
17543 */
17544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017545f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017546 typval_T *argvars;
17547 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017548{
17549 char_u *instr;
17550 char_u *fromstr;
17551 char_u *tostr;
17552 char_u *p;
17553#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017554 int inlen;
17555 int fromlen;
17556 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017557 int idx;
17558 char_u *cpstr;
17559 int cplen;
17560 int first = TRUE;
17561#endif
17562 char_u buf[NUMBUFLEN];
17563 char_u buf2[NUMBUFLEN];
17564 garray_T ga;
17565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017566 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017567 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17568 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017569
17570 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017571 rettv->v_type = VAR_STRING;
17572 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017573 if (fromstr == NULL || tostr == NULL)
17574 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017575 ga_init2(&ga, (int)sizeof(char), 80);
17576
17577#ifdef FEAT_MBYTE
17578 if (!has_mbyte)
17579#endif
17580 /* not multi-byte: fromstr and tostr must be the same length */
17581 if (STRLEN(fromstr) != STRLEN(tostr))
17582 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017583#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017584error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017585#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017586 EMSG2(_(e_invarg2), fromstr);
17587 ga_clear(&ga);
17588 return;
17589 }
17590
17591 /* fromstr and tostr have to contain the same number of chars */
17592 while (*instr != NUL)
17593 {
17594#ifdef FEAT_MBYTE
17595 if (has_mbyte)
17596 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017597 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017598 cpstr = instr;
17599 cplen = inlen;
17600 idx = 0;
17601 for (p = fromstr; *p != NUL; p += fromlen)
17602 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017603 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017604 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17605 {
17606 for (p = tostr; *p != NUL; p += tolen)
17607 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017608 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017609 if (idx-- == 0)
17610 {
17611 cplen = tolen;
17612 cpstr = p;
17613 break;
17614 }
17615 }
17616 if (*p == NUL) /* tostr is shorter than fromstr */
17617 goto error;
17618 break;
17619 }
17620 ++idx;
17621 }
17622
17623 if (first && cpstr == instr)
17624 {
17625 /* Check that fromstr and tostr have the same number of
17626 * (multi-byte) characters. Done only once when a character
17627 * of instr doesn't appear in fromstr. */
17628 first = FALSE;
17629 for (p = tostr; *p != NUL; p += tolen)
17630 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017631 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017632 --idx;
17633 }
17634 if (idx != 0)
17635 goto error;
17636 }
17637
17638 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017639 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017640 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017641
17642 instr += inlen;
17643 }
17644 else
17645#endif
17646 {
17647 /* When not using multi-byte chars we can do it faster. */
17648 p = vim_strchr(fromstr, *instr);
17649 if (p != NULL)
17650 ga_append(&ga, tostr[p - fromstr]);
17651 else
17652 ga_append(&ga, *instr);
17653 ++instr;
17654 }
17655 }
17656
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017657 /* add a terminating NUL */
17658 ga_grow(&ga, 1);
17659 ga_append(&ga, NUL);
17660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017661 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017662}
17663
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017664#ifdef FEAT_FLOAT
17665/*
17666 * "trunc({float})" function
17667 */
17668 static void
17669f_trunc(argvars, rettv)
17670 typval_T *argvars;
17671 typval_T *rettv;
17672{
17673 float_T f;
17674
17675 rettv->v_type = VAR_FLOAT;
17676 if (get_float_arg(argvars, &f) == OK)
17677 /* trunc() is not in C90, use floor() or ceil() instead. */
17678 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17679 else
17680 rettv->vval.v_float = 0.0;
17681}
17682#endif
17683
Bram Moolenaar8299df92004-07-10 09:47:34 +000017684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 * "type(expr)" function
17686 */
17687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017688f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017689 typval_T *argvars;
17690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017692 int n;
17693
17694 switch (argvars[0].v_type)
17695 {
17696 case VAR_NUMBER: n = 0; break;
17697 case VAR_STRING: n = 1; break;
17698 case VAR_FUNC: n = 2; break;
17699 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017700 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017701#ifdef FEAT_FLOAT
17702 case VAR_FLOAT: n = 5; break;
17703#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017704 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17705 }
17706 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707}
17708
17709/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017710 * "undofile(name)" function
17711 */
17712 static void
17713f_undofile(argvars, rettv)
17714 typval_T *argvars;
17715 typval_T *rettv;
17716{
17717 rettv->v_type = VAR_STRING;
17718#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017719 {
17720 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17721
17722 if (ffname != NULL)
17723 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17724 vim_free(ffname);
17725 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017726#else
17727 rettv->vval.v_string = NULL;
17728#endif
17729}
17730
17731/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017732 * "undotree()" function
17733 */
17734 static void
17735f_undotree(argvars, rettv)
17736 typval_T *argvars UNUSED;
17737 typval_T *rettv;
17738{
17739 if (rettv_dict_alloc(rettv) == OK)
17740 {
17741 dict_T *dict = rettv->vval.v_dict;
17742 list_T *list;
17743
Bram Moolenaar730cde92010-06-27 05:18:54 +020017744 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017745 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017746 dict_add_nr_str(dict, "save_last",
17747 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017748 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17749 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017750 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017751
17752 list = list_alloc();
17753 if (list != NULL)
17754 {
17755 u_eval_tree(curbuf->b_u_oldhead, list);
17756 dict_add_list(dict, "entries", list);
17757 }
17758 }
17759}
17760
17761/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017762 * "values(dict)" function
17763 */
17764 static void
17765f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017766 typval_T *argvars;
17767 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017768{
17769 dict_list(argvars, rettv, 1);
17770}
17771
17772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 * "virtcol(string)" function
17774 */
17775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017776f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017777 typval_T *argvars;
17778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779{
17780 colnr_T vcol = 0;
17781 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017782 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017784 fp = var2fpos(&argvars[0], FALSE, &fnum);
17785 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17786 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 {
17788 getvvcol(curwin, fp, NULL, NULL, &vcol);
17789 ++vcol;
17790 }
17791
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017792 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793}
17794
17795/*
17796 * "visualmode()" function
17797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017799f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017800 typval_T *argvars UNUSED;
17801 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802{
17803#ifdef FEAT_VISUAL
17804 char_u str[2];
17805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017806 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 str[0] = curbuf->b_visual_mode_eval;
17808 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017809 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810
17811 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017812 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814#endif
17815}
17816
17817/*
17818 * "winbufnr(nr)" function
17819 */
17820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017821f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017822 typval_T *argvars;
17823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824{
17825 win_T *wp;
17826
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017827 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017829 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017831 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832}
17833
17834/*
17835 * "wincol()" function
17836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017838f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017839 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841{
17842 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017843 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844}
17845
17846/*
17847 * "winheight(nr)" function
17848 */
17849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017850f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017851 typval_T *argvars;
17852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853{
17854 win_T *wp;
17855
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017856 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017858 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017860 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861}
17862
17863/*
17864 * "winline()" function
17865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017867f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017868 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870{
17871 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017872 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873}
17874
17875/*
17876 * "winnr()" function
17877 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017879f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017880 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882{
17883 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017884
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017886 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017888 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889}
17890
17891/*
17892 * "winrestcmd()" function
17893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017895f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017896 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898{
17899#ifdef FEAT_WINDOWS
17900 win_T *wp;
17901 int winnr = 1;
17902 garray_T ga;
17903 char_u buf[50];
17904
17905 ga_init2(&ga, (int)sizeof(char), 70);
17906 for (wp = firstwin; wp != NULL; wp = wp->w_next)
17907 {
17908 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
17909 ga_concat(&ga, buf);
17910# ifdef FEAT_VERTSPLIT
17911 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
17912 ga_concat(&ga, buf);
17913# endif
17914 ++winnr;
17915 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000017916 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017918 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017920 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017922 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923}
17924
17925/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017926 * "winrestview()" function
17927 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017928 static void
17929f_winrestview(argvars, rettv)
17930 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017931 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017932{
17933 dict_T *dict;
17934
17935 if (argvars[0].v_type != VAR_DICT
17936 || (dict = argvars[0].vval.v_dict) == NULL)
17937 EMSG(_(e_invarg));
17938 else
17939 {
17940 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
17941 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
17942#ifdef FEAT_VIRTUALEDIT
17943 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
17944#endif
17945 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017946 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017947
Bram Moolenaar6f11a412006-09-06 20:16:42 +000017948 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017949#ifdef FEAT_DIFF
17950 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
17951#endif
17952 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
17953 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
17954
17955 check_cursor();
17956 changed_cline_bef_curs();
17957 invalidate_botline();
17958 redraw_later(VALID);
17959
17960 if (curwin->w_topline == 0)
17961 curwin->w_topline = 1;
17962 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
17963 curwin->w_topline = curbuf->b_ml.ml_line_count;
17964#ifdef FEAT_DIFF
17965 check_topfill(curwin, TRUE);
17966#endif
17967 }
17968}
17969
17970/*
17971 * "winsaveview()" function
17972 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017973 static void
17974f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017975 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017976 typval_T *rettv;
17977{
17978 dict_T *dict;
17979
Bram Moolenaara800b422010-06-27 01:15:55 +020017980 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017981 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020017982 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017983
17984 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
17985 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
17986#ifdef FEAT_VIRTUALEDIT
17987 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
17988#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000017989 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017990 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
17991
17992 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
17993#ifdef FEAT_DIFF
17994 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
17995#endif
17996 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
17997 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
17998}
17999
18000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001 * "winwidth(nr)" function
18002 */
18003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018004f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018005 typval_T *argvars;
18006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007{
18008 win_T *wp;
18009
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018010 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018012 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013 else
18014#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018015 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018017 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018#endif
18019}
18020
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018022 * "writefile()" function
18023 */
18024 static void
18025f_writefile(argvars, rettv)
18026 typval_T *argvars;
18027 typval_T *rettv;
18028{
18029 int binary = FALSE;
18030 char_u *fname;
18031 FILE *fd;
18032 listitem_T *li;
18033 char_u *s;
18034 int ret = 0;
18035 int c;
18036
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018037 if (check_restricted() || check_secure())
18038 return;
18039
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018040 if (argvars[0].v_type != VAR_LIST)
18041 {
18042 EMSG2(_(e_listarg), "writefile()");
18043 return;
18044 }
18045 if (argvars[0].vval.v_list == NULL)
18046 return;
18047
18048 if (argvars[2].v_type != VAR_UNKNOWN
18049 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18050 binary = TRUE;
18051
18052 /* Always open the file in binary mode, library functions have a mind of
18053 * their own about CR-LF conversion. */
18054 fname = get_tv_string(&argvars[1]);
18055 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18056 {
18057 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18058 ret = -1;
18059 }
18060 else
18061 {
18062 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18063 li = li->li_next)
18064 {
18065 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18066 {
18067 if (*s == '\n')
18068 c = putc(NUL, fd);
18069 else
18070 c = putc(*s, fd);
18071 if (c == EOF)
18072 {
18073 ret = -1;
18074 break;
18075 }
18076 }
18077 if (!binary || li->li_next != NULL)
18078 if (putc('\n', fd) == EOF)
18079 {
18080 ret = -1;
18081 break;
18082 }
18083 if (ret < 0)
18084 {
18085 EMSG(_(e_write));
18086 break;
18087 }
18088 }
18089 fclose(fd);
18090 }
18091
18092 rettv->vval.v_number = ret;
18093}
18094
18095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018097 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 */
18099 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018100var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018101 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018102 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018103 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018105 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018107 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108
Bram Moolenaara5525202006-03-02 22:52:09 +000018109 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018110 if (varp->v_type == VAR_LIST)
18111 {
18112 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018113 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018114 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018115 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018116
18117 l = varp->vval.v_list;
18118 if (l == NULL)
18119 return NULL;
18120
18121 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018122 pos.lnum = list_find_nr(l, 0L, &error);
18123 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018124 return NULL; /* invalid line number */
18125
18126 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018127 pos.col = list_find_nr(l, 1L, &error);
18128 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018129 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018130 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018131
18132 /* We accept "$" for the column number: last column. */
18133 li = list_find(l, 1L);
18134 if (li != NULL && li->li_tv.v_type == VAR_STRING
18135 && li->li_tv.vval.v_string != NULL
18136 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18137 pos.col = len + 1;
18138
Bram Moolenaara5525202006-03-02 22:52:09 +000018139 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018140 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018141 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018142 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018143
Bram Moolenaara5525202006-03-02 22:52:09 +000018144#ifdef FEAT_VIRTUALEDIT
18145 /* Get the virtual offset. Defaults to zero. */
18146 pos.coladd = list_find_nr(l, 2L, &error);
18147 if (error)
18148 pos.coladd = 0;
18149#endif
18150
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018151 return &pos;
18152 }
18153
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018154 name = get_tv_string_chk(varp);
18155 if (name == NULL)
18156 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018157 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018159#ifdef FEAT_VISUAL
18160 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18161 {
18162 if (VIsual_active)
18163 return &VIsual;
18164 return &curwin->w_cursor;
18165 }
18166#endif
18167 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018169 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18171 return NULL;
18172 return pp;
18173 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018174
18175#ifdef FEAT_VIRTUALEDIT
18176 pos.coladd = 0;
18177#endif
18178
Bram Moolenaar477933c2007-07-17 14:32:23 +000018179 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018180 {
18181 pos.col = 0;
18182 if (name[1] == '0') /* "w0": first visible line */
18183 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018184 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018185 pos.lnum = curwin->w_topline;
18186 return &pos;
18187 }
18188 else if (name[1] == '$') /* "w$": last visible line */
18189 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018190 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018191 pos.lnum = curwin->w_botline - 1;
18192 return &pos;
18193 }
18194 }
18195 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018197 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198 {
18199 pos.lnum = curbuf->b_ml.ml_line_count;
18200 pos.col = 0;
18201 }
18202 else
18203 {
18204 pos.lnum = curwin->w_cursor.lnum;
18205 pos.col = (colnr_T)STRLEN(ml_get_curline());
18206 }
18207 return &pos;
18208 }
18209 return NULL;
18210}
18211
18212/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018213 * Convert list in "arg" into a position and optional file number.
18214 * When "fnump" is NULL there is no file number, only 3 items.
18215 * Note that the column is passed on as-is, the caller may want to decrement
18216 * it to use 1 for the first column.
18217 * Return FAIL when conversion is not possible, doesn't check the position for
18218 * validity.
18219 */
18220 static int
18221list2fpos(arg, posp, fnump)
18222 typval_T *arg;
18223 pos_T *posp;
18224 int *fnump;
18225{
18226 list_T *l = arg->vval.v_list;
18227 long i = 0;
18228 long n;
18229
Bram Moolenaarbde35262006-07-23 20:12:24 +000018230 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18231 * when "fnump" isn't NULL and "coladd" is optional. */
18232 if (arg->v_type != VAR_LIST
18233 || l == NULL
18234 || l->lv_len < (fnump == NULL ? 2 : 3)
18235 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018236 return FAIL;
18237
18238 if (fnump != NULL)
18239 {
18240 n = list_find_nr(l, i++, NULL); /* fnum */
18241 if (n < 0)
18242 return FAIL;
18243 if (n == 0)
18244 n = curbuf->b_fnum; /* current buffer */
18245 *fnump = n;
18246 }
18247
18248 n = list_find_nr(l, i++, NULL); /* lnum */
18249 if (n < 0)
18250 return FAIL;
18251 posp->lnum = n;
18252
18253 n = list_find_nr(l, i++, NULL); /* col */
18254 if (n < 0)
18255 return FAIL;
18256 posp->col = n;
18257
18258#ifdef FEAT_VIRTUALEDIT
18259 n = list_find_nr(l, i, NULL);
18260 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018261 posp->coladd = 0;
18262 else
18263 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018264#endif
18265
18266 return OK;
18267}
18268
18269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270 * Get the length of an environment variable name.
18271 * Advance "arg" to the first character after the name.
18272 * Return 0 for error.
18273 */
18274 static int
18275get_env_len(arg)
18276 char_u **arg;
18277{
18278 char_u *p;
18279 int len;
18280
18281 for (p = *arg; vim_isIDc(*p); ++p)
18282 ;
18283 if (p == *arg) /* no name found */
18284 return 0;
18285
18286 len = (int)(p - *arg);
18287 *arg = p;
18288 return len;
18289}
18290
18291/*
18292 * Get the length of the name of a function or internal variable.
18293 * "arg" is advanced to the first non-white character after the name.
18294 * Return 0 if something is wrong.
18295 */
18296 static int
18297get_id_len(arg)
18298 char_u **arg;
18299{
18300 char_u *p;
18301 int len;
18302
18303 /* Find the end of the name. */
18304 for (p = *arg; eval_isnamec(*p); ++p)
18305 ;
18306 if (p == *arg) /* no name found */
18307 return 0;
18308
18309 len = (int)(p - *arg);
18310 *arg = skipwhite(p);
18311
18312 return len;
18313}
18314
18315/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018316 * Get the length of the name of a variable or function.
18317 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018319 * Return -1 if curly braces expansion failed.
18320 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 * If the name contains 'magic' {}'s, expand them and return the
18322 * expanded name in an allocated string via 'alias' - caller must free.
18323 */
18324 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018325get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 char_u **arg;
18327 char_u **alias;
18328 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018329 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330{
18331 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 char_u *p;
18333 char_u *expr_start;
18334 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335
18336 *alias = NULL; /* default to no alias */
18337
18338 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18339 && (*arg)[2] == (int)KE_SNR)
18340 {
18341 /* hard coded <SNR>, already translated */
18342 *arg += 3;
18343 return get_id_len(arg) + 3;
18344 }
18345 len = eval_fname_script(*arg);
18346 if (len > 0)
18347 {
18348 /* literal "<SID>", "s:" or "<SNR>" */
18349 *arg += len;
18350 }
18351
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018353 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018355 p = find_name_end(*arg, &expr_start, &expr_end,
18356 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 if (expr_start != NULL)
18358 {
18359 char_u *temp_string;
18360
18361 if (!evaluate)
18362 {
18363 len += (int)(p - *arg);
18364 *arg = skipwhite(p);
18365 return len;
18366 }
18367
18368 /*
18369 * Include any <SID> etc in the expanded string:
18370 * Thus the -len here.
18371 */
18372 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18373 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018374 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 *alias = temp_string;
18376 *arg = skipwhite(p);
18377 return (int)STRLEN(temp_string);
18378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379
18380 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018381 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 EMSG2(_(e_invexpr2), *arg);
18383
18384 return len;
18385}
18386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018387/*
18388 * Find the end of a variable or function name, taking care of magic braces.
18389 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18390 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018391 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018392 * Return a pointer to just after the name. Equal to "arg" if there is no
18393 * valid name.
18394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018396find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 char_u *arg;
18398 char_u **expr_start;
18399 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018400 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018402 int mb_nest = 0;
18403 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404 char_u *p;
18405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018406 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018408 *expr_start = NULL;
18409 *expr_end = NULL;
18410 }
18411
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018412 /* Quick check for valid starting character. */
18413 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18414 return arg;
18415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018416 for (p = arg; *p != NUL
18417 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018418 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018419 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018420 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018421 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018422 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018423 if (*p == '\'')
18424 {
18425 /* skip over 'string' to avoid counting [ and ] inside it. */
18426 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18427 ;
18428 if (*p == NUL)
18429 break;
18430 }
18431 else if (*p == '"')
18432 {
18433 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18434 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18435 if (*p == '\\' && p[1] != NUL)
18436 ++p;
18437 if (*p == NUL)
18438 break;
18439 }
18440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018441 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018443 if (*p == '[')
18444 ++br_nest;
18445 else if (*p == ']')
18446 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018449 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018451 if (*p == '{')
18452 {
18453 mb_nest++;
18454 if (expr_start != NULL && *expr_start == NULL)
18455 *expr_start = p;
18456 }
18457 else if (*p == '}')
18458 {
18459 mb_nest--;
18460 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18461 *expr_end = p;
18462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 }
18465
18466 return p;
18467}
18468
18469/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018470 * Expands out the 'magic' {}'s in a variable/function name.
18471 * Note that this can call itself recursively, to deal with
18472 * constructs like foo{bar}{baz}{bam}
18473 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18474 * "in_start" ^
18475 * "expr_start" ^
18476 * "expr_end" ^
18477 * "in_end" ^
18478 *
18479 * Returns a new allocated string, which the caller must free.
18480 * Returns NULL for failure.
18481 */
18482 static char_u *
18483make_expanded_name(in_start, expr_start, expr_end, in_end)
18484 char_u *in_start;
18485 char_u *expr_start;
18486 char_u *expr_end;
18487 char_u *in_end;
18488{
18489 char_u c1;
18490 char_u *retval = NULL;
18491 char_u *temp_result;
18492 char_u *nextcmd = NULL;
18493
18494 if (expr_end == NULL || in_end == NULL)
18495 return NULL;
18496 *expr_start = NUL;
18497 *expr_end = NUL;
18498 c1 = *in_end;
18499 *in_end = NUL;
18500
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018501 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018502 if (temp_result != NULL && nextcmd == NULL)
18503 {
18504 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18505 + (in_end - expr_end) + 1));
18506 if (retval != NULL)
18507 {
18508 STRCPY(retval, in_start);
18509 STRCAT(retval, temp_result);
18510 STRCAT(retval, expr_end + 1);
18511 }
18512 }
18513 vim_free(temp_result);
18514
18515 *in_end = c1; /* put char back for error messages */
18516 *expr_start = '{';
18517 *expr_end = '}';
18518
18519 if (retval != NULL)
18520 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018521 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018522 if (expr_start != NULL)
18523 {
18524 /* Further expansion! */
18525 temp_result = make_expanded_name(retval, expr_start,
18526 expr_end, temp_result);
18527 vim_free(retval);
18528 retval = temp_result;
18529 }
18530 }
18531
18532 return retval;
18533}
18534
18535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018537 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 */
18539 static int
18540eval_isnamec(c)
18541 int c;
18542{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018543 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18544}
18545
18546/*
18547 * Return TRUE if character "c" can be used as the first character in a
18548 * variable or function name (excluding '{' and '}').
18549 */
18550 static int
18551eval_isnamec1(c)
18552 int c;
18553{
18554 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555}
18556
18557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 * Set number v: variable to "val".
18559 */
18560 void
18561set_vim_var_nr(idx, val)
18562 int idx;
18563 long val;
18564{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018565 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566}
18567
18568/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018569 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 */
18571 long
18572get_vim_var_nr(idx)
18573 int idx;
18574{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018575 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576}
18577
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018578/*
18579 * Get string v: variable value. Uses a static buffer, can only be used once.
18580 */
18581 char_u *
18582get_vim_var_str(idx)
18583 int idx;
18584{
18585 return get_tv_string(&vimvars[idx].vv_tv);
18586}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018587
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018589 * Get List v: variable value. Caller must take care of reference count when
18590 * needed.
18591 */
18592 list_T *
18593get_vim_var_list(idx)
18594 int idx;
18595{
18596 return vimvars[idx].vv_list;
18597}
18598
18599/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018600 * Set v:char to character "c".
18601 */
18602 void
18603set_vim_var_char(c)
18604 int c;
18605{
18606#ifdef FEAT_MBYTE
18607 char_u buf[MB_MAXBYTES];
18608#else
18609 char_u buf[2];
18610#endif
18611
18612#ifdef FEAT_MBYTE
18613 if (has_mbyte)
18614 buf[(*mb_char2bytes)(c, buf)] = NUL;
18615 else
18616#endif
18617 {
18618 buf[0] = c;
18619 buf[1] = NUL;
18620 }
18621 set_vim_var_string(VV_CHAR, buf, -1);
18622}
18623
18624/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018625 * Set v:count to "count" and v:count1 to "count1".
18626 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 */
18628 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018629set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 long count;
18631 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018632 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018634 if (set_prevcount)
18635 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018636 vimvars[VV_COUNT].vv_nr = count;
18637 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638}
18639
18640/*
18641 * Set string v: variable to a copy of "val".
18642 */
18643 void
18644set_vim_var_string(idx, val, len)
18645 int idx;
18646 char_u *val;
18647 int len; /* length of "val" to use or -1 (whole string) */
18648{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018649 /* Need to do this (at least) once, since we can't initialize a union.
18650 * Will always be invoked when "v:progname" is set. */
18651 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18652
Bram Moolenaare9a41262005-01-15 22:18:47 +000018653 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018655 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018657 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018659 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660}
18661
18662/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018663 * Set List v: variable to "val".
18664 */
18665 void
18666set_vim_var_list(idx, val)
18667 int idx;
18668 list_T *val;
18669{
18670 list_unref(vimvars[idx].vv_list);
18671 vimvars[idx].vv_list = val;
18672 if (val != NULL)
18673 ++val->lv_refcount;
18674}
18675
18676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 * Set v:register if needed.
18678 */
18679 void
18680set_reg_var(c)
18681 int c;
18682{
18683 char_u regname;
18684
18685 if (c == 0 || c == ' ')
18686 regname = '"';
18687 else
18688 regname = c;
18689 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018690 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691 set_vim_var_string(VV_REG, &regname, 1);
18692}
18693
18694/*
18695 * Get or set v:exception. If "oldval" == NULL, return the current value.
18696 * Otherwise, restore the value to "oldval" and return NULL.
18697 * Must always be called in pairs to save and restore v:exception! Does not
18698 * take care of memory allocations.
18699 */
18700 char_u *
18701v_exception(oldval)
18702 char_u *oldval;
18703{
18704 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018705 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706
Bram Moolenaare9a41262005-01-15 22:18:47 +000018707 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 return NULL;
18709}
18710
18711/*
18712 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18713 * Otherwise, restore the value to "oldval" and return NULL.
18714 * Must always be called in pairs to save and restore v:throwpoint! Does not
18715 * take care of memory allocations.
18716 */
18717 char_u *
18718v_throwpoint(oldval)
18719 char_u *oldval;
18720{
18721 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018722 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723
Bram Moolenaare9a41262005-01-15 22:18:47 +000018724 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 return NULL;
18726}
18727
18728#if defined(FEAT_AUTOCMD) || defined(PROTO)
18729/*
18730 * Set v:cmdarg.
18731 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18732 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18733 * Must always be called in pairs!
18734 */
18735 char_u *
18736set_cmdarg(eap, oldarg)
18737 exarg_T *eap;
18738 char_u *oldarg;
18739{
18740 char_u *oldval;
18741 char_u *newval;
18742 unsigned len;
18743
Bram Moolenaare9a41262005-01-15 22:18:47 +000018744 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018745 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018747 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018748 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018749 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 }
18751
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018752 if (eap->force_bin == FORCE_BIN)
18753 len = 6;
18754 else if (eap->force_bin == FORCE_NOBIN)
18755 len = 8;
18756 else
18757 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018758
18759 if (eap->read_edit)
18760 len += 7;
18761
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018762 if (eap->force_ff != 0)
18763 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18764# ifdef FEAT_MBYTE
18765 if (eap->force_enc != 0)
18766 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018767 if (eap->bad_char != 0)
18768 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018769# endif
18770
18771 newval = alloc(len + 1);
18772 if (newval == NULL)
18773 return NULL;
18774
18775 if (eap->force_bin == FORCE_BIN)
18776 sprintf((char *)newval, " ++bin");
18777 else if (eap->force_bin == FORCE_NOBIN)
18778 sprintf((char *)newval, " ++nobin");
18779 else
18780 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018781
18782 if (eap->read_edit)
18783 STRCAT(newval, " ++edit");
18784
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018785 if (eap->force_ff != 0)
18786 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18787 eap->cmd + eap->force_ff);
18788# ifdef FEAT_MBYTE
18789 if (eap->force_enc != 0)
18790 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18791 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018792 if (eap->bad_char == BAD_KEEP)
18793 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18794 else if (eap->bad_char == BAD_DROP)
18795 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18796 else if (eap->bad_char != 0)
18797 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018798# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018799 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018800 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801}
18802#endif
18803
18804/*
18805 * Get the value of internal variable "name".
18806 * Return OK or FAIL.
18807 */
18808 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018809get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810 char_u *name;
18811 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018812 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018813 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814{
18815 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018816 typval_T *tv = NULL;
18817 typval_T atv;
18818 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820
18821 /* truncate the name, so that we can use strcmp() */
18822 cc = name[len];
18823 name[len] = NUL;
18824
18825 /*
18826 * Check for "b:changedtick".
18827 */
18828 if (STRCMP(name, "b:changedtick") == 0)
18829 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018830 atv.v_type = VAR_NUMBER;
18831 atv.vval.v_number = curbuf->b_changedtick;
18832 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833 }
18834
18835 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 * Check for user-defined variables.
18837 */
18838 else
18839 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018840 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018842 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843 }
18844
Bram Moolenaare9a41262005-01-15 22:18:47 +000018845 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018847 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018848 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849 ret = FAIL;
18850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018851 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018852 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853
18854 name[len] = cc;
18855
18856 return ret;
18857}
18858
18859/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018860 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18861 * Also handle function call with Funcref variable: func(expr)
18862 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18863 */
18864 static int
18865handle_subscript(arg, rettv, evaluate, verbose)
18866 char_u **arg;
18867 typval_T *rettv;
18868 int evaluate; /* do more than finding the end */
18869 int verbose; /* give error messages */
18870{
18871 int ret = OK;
18872 dict_T *selfdict = NULL;
18873 char_u *s;
18874 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000018875 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018876
18877 while (ret == OK
18878 && (**arg == '['
18879 || (**arg == '.' && rettv->v_type == VAR_DICT)
18880 || (**arg == '(' && rettv->v_type == VAR_FUNC))
18881 && !vim_iswhite(*(*arg - 1)))
18882 {
18883 if (**arg == '(')
18884 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000018885 /* need to copy the funcref so that we can clear rettv */
18886 functv = *rettv;
18887 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018888
18889 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000018890 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018891 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000018892 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
18893 &len, evaluate, selfdict);
18894
18895 /* Clear the funcref afterwards, so that deleting it while
18896 * evaluating the arguments is possible (see test55). */
18897 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018898
18899 /* Stop the expression evaluation when immediately aborting on
18900 * error, or when an interrupt occurred or an exception was thrown
18901 * but not caught. */
18902 if (aborting())
18903 {
18904 if (ret == OK)
18905 clear_tv(rettv);
18906 ret = FAIL;
18907 }
18908 dict_unref(selfdict);
18909 selfdict = NULL;
18910 }
18911 else /* **arg == '[' || **arg == '.' */
18912 {
18913 dict_unref(selfdict);
18914 if (rettv->v_type == VAR_DICT)
18915 {
18916 selfdict = rettv->vval.v_dict;
18917 if (selfdict != NULL)
18918 ++selfdict->dv_refcount;
18919 }
18920 else
18921 selfdict = NULL;
18922 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
18923 {
18924 clear_tv(rettv);
18925 ret = FAIL;
18926 }
18927 }
18928 }
18929 dict_unref(selfdict);
18930 return ret;
18931}
18932
18933/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018934 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018935 * value).
18936 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018937 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018938alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018939{
Bram Moolenaar33570922005-01-25 22:26:29 +000018940 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018941}
18942
18943/*
18944 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945 * The string "s" must have been allocated, it is consumed.
18946 * Return NULL for out of memory, the variable otherwise.
18947 */
Bram Moolenaar33570922005-01-25 22:26:29 +000018948 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018949alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950 char_u *s;
18951{
Bram Moolenaar33570922005-01-25 22:26:29 +000018952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018954 rettv = alloc_tv();
18955 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018957 rettv->v_type = VAR_STRING;
18958 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 }
18960 else
18961 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018962 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963}
18964
18965/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018966 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000018968 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018969free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000018970 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971{
18972 if (varp != NULL)
18973 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018974 switch (varp->v_type)
18975 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018976 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018977 func_unref(varp->vval.v_string);
18978 /*FALLTHROUGH*/
18979 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018980 vim_free(varp->vval.v_string);
18981 break;
18982 case VAR_LIST:
18983 list_unref(varp->vval.v_list);
18984 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018985 case VAR_DICT:
18986 dict_unref(varp->vval.v_dict);
18987 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018988 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018989#ifdef FEAT_FLOAT
18990 case VAR_FLOAT:
18991#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000018992 case VAR_UNKNOWN:
18993 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018994 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000018995 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018996 break;
18997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 vim_free(varp);
18999 }
19000}
19001
19002/*
19003 * Free the memory for a variable value and set the value to NULL or 0.
19004 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019005 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019006clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019007 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008{
19009 if (varp != NULL)
19010 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019011 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019013 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019014 func_unref(varp->vval.v_string);
19015 /*FALLTHROUGH*/
19016 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019017 vim_free(varp->vval.v_string);
19018 varp->vval.v_string = NULL;
19019 break;
19020 case VAR_LIST:
19021 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019022 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019023 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019024 case VAR_DICT:
19025 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019026 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019027 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019028 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019029 varp->vval.v_number = 0;
19030 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019031#ifdef FEAT_FLOAT
19032 case VAR_FLOAT:
19033 varp->vval.v_float = 0.0;
19034 break;
19035#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019036 case VAR_UNKNOWN:
19037 break;
19038 default:
19039 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019041 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 }
19043}
19044
19045/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019046 * Set the value of a variable to NULL without freeing items.
19047 */
19048 static void
19049init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019050 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019051{
19052 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019053 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019054}
19055
19056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 * Get the number value of a variable.
19058 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019059 * For incompatible types, return 0.
19060 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19061 * caller of incompatible types: it sets *denote to TRUE if "denote"
19062 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063 */
19064 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019065get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019066 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019068 int error = FALSE;
19069
19070 return get_tv_number_chk(varp, &error); /* return 0L on error */
19071}
19072
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019073 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019074get_tv_number_chk(varp, denote)
19075 typval_T *varp;
19076 int *denote;
19077{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019078 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019080 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019082 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019083 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019084#ifdef FEAT_FLOAT
19085 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019086 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019087 break;
19088#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019089 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019090 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019091 break;
19092 case VAR_STRING:
19093 if (varp->vval.v_string != NULL)
19094 vim_str2nr(varp->vval.v_string, NULL, NULL,
19095 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019096 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019097 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019098 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019099 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019100 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019101 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019102 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019103 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019104 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019105 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019107 if (denote == NULL) /* useful for values that must be unsigned */
19108 n = -1;
19109 else
19110 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019111 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112}
19113
19114/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019115 * Get the lnum from the first argument.
19116 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019117 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 */
19119 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019120get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019121 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122{
Bram Moolenaar33570922005-01-25 22:26:29 +000019123 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124 linenr_T lnum;
19125
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019126 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127 if (lnum == 0) /* no valid number, try using line() */
19128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019129 rettv.v_type = VAR_NUMBER;
19130 f_line(argvars, &rettv);
19131 lnum = rettv.vval.v_number;
19132 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 }
19134 return lnum;
19135}
19136
19137/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019138 * Get the lnum from the first argument.
19139 * Also accepts "$", then "buf" is used.
19140 * Returns 0 on error.
19141 */
19142 static linenr_T
19143get_tv_lnum_buf(argvars, buf)
19144 typval_T *argvars;
19145 buf_T *buf;
19146{
19147 if (argvars[0].v_type == VAR_STRING
19148 && argvars[0].vval.v_string != NULL
19149 && argvars[0].vval.v_string[0] == '$'
19150 && buf != NULL)
19151 return buf->b_ml.ml_line_count;
19152 return get_tv_number_chk(&argvars[0], NULL);
19153}
19154
19155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 * Get the string value of a variable.
19157 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019158 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19159 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 * If the String variable has never been set, return an empty string.
19161 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019162 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19163 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 */
19165 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019166get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019167 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168{
19169 static char_u mybuf[NUMBUFLEN];
19170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019171 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172}
19173
19174 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019175get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019176 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019177 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019179 char_u *res = get_tv_string_buf_chk(varp, buf);
19180
19181 return res != NULL ? res : (char_u *)"";
19182}
19183
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019184 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019185get_tv_string_chk(varp)
19186 typval_T *varp;
19187{
19188 static char_u mybuf[NUMBUFLEN];
19189
19190 return get_tv_string_buf_chk(varp, mybuf);
19191}
19192
19193 static char_u *
19194get_tv_string_buf_chk(varp, buf)
19195 typval_T *varp;
19196 char_u *buf;
19197{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019198 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019200 case VAR_NUMBER:
19201 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19202 return buf;
19203 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019204 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019205 break;
19206 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019207 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019208 break;
19209 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019210 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019211 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019212#ifdef FEAT_FLOAT
19213 case VAR_FLOAT:
19214 EMSG(_("E806: using Float as a String"));
19215 break;
19216#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019217 case VAR_STRING:
19218 if (varp->vval.v_string != NULL)
19219 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019220 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019221 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019222 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019223 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019225 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226}
19227
19228/*
19229 * Find variable "name" in the list of variables.
19230 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019231 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019232 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019233 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019235 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019236find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019238 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019241 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242
Bram Moolenaara7043832005-01-21 11:56:39 +000019243 ht = find_var_ht(name, &varname);
19244 if (htp != NULL)
19245 *htp = ht;
19246 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019248 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249}
19250
19251/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019252 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019253 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019255 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019256find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019257 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019258 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019259 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019260{
Bram Moolenaar33570922005-01-25 22:26:29 +000019261 hashitem_T *hi;
19262
19263 if (*varname == NUL)
19264 {
19265 /* Must be something like "s:", otherwise "ht" would be NULL. */
19266 switch (varname[-2])
19267 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019268 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019269 case 'g': return &globvars_var;
19270 case 'v': return &vimvars_var;
19271 case 'b': return &curbuf->b_bufvar;
19272 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019273#ifdef FEAT_WINDOWS
19274 case 't': return &curtab->tp_winvar;
19275#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019276 case 'l': return current_funccal == NULL
19277 ? NULL : &current_funccal->l_vars_var;
19278 case 'a': return current_funccal == NULL
19279 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019280 }
19281 return NULL;
19282 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019283
19284 hi = hash_find(ht, varname);
19285 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019286 {
19287 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019288 * worked find the variable again. Don't auto-load a script if it was
19289 * loaded already, otherwise it would be loaded every time when
19290 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019291 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019292 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019293 hi = hash_find(ht, varname);
19294 if (HASHITEM_EMPTY(hi))
19295 return NULL;
19296 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019297 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019298}
19299
19300/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019301 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019302 * Set "varname" to the start of name without ':'.
19303 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019304 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019305find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306 char_u *name;
19307 char_u **varname;
19308{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019309 hashitem_T *hi;
19310
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 if (name[1] != ':')
19312 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019313 /* The name must not start with a colon or #. */
19314 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315 return NULL;
19316 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019317
19318 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019319 hi = hash_find(&compat_hashtab, name);
19320 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019321 return &compat_hashtab;
19322
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019324 return &globvarht; /* global variable */
19325 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 }
19327 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019328 if (*name == 'g') /* global variable */
19329 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019330 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19331 */
19332 if (vim_strchr(name + 2, ':') != NULL
19333 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019334 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019336 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019338 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019339#ifdef FEAT_WINDOWS
19340 if (*name == 't') /* tab page variable */
19341 return &curtab->tp_vars.dv_hashtab;
19342#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019343 if (*name == 'v') /* v: variable */
19344 return &vimvarht;
19345 if (*name == 'a' && current_funccal != NULL) /* function argument */
19346 return &current_funccal->l_avars.dv_hashtab;
19347 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19348 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349 if (*name == 's' /* script variable */
19350 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19351 return &SCRIPT_VARS(current_SID);
19352 return NULL;
19353}
19354
19355/*
19356 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019357 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358 * Returns NULL when it doesn't exist.
19359 */
19360 char_u *
19361get_var_value(name)
19362 char_u *name;
19363{
Bram Moolenaar33570922005-01-25 22:26:29 +000019364 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365
Bram Moolenaara7043832005-01-21 11:56:39 +000019366 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 if (v == NULL)
19368 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019369 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370}
19371
19372/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019373 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374 * sourcing this script and when executing functions defined in the script.
19375 */
19376 void
19377new_script_vars(id)
19378 scid_T id;
19379{
Bram Moolenaara7043832005-01-21 11:56:39 +000019380 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019381 hashtab_T *ht;
19382 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019383
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19385 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019386 /* Re-allocating ga_data means that an ht_array pointing to
19387 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019388 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019389 for (i = 1; i <= ga_scripts.ga_len; ++i)
19390 {
19391 ht = &SCRIPT_VARS(i);
19392 if (ht->ht_mask == HT_INIT_SIZE - 1)
19393 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019394 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019395 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019396 }
19397
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 while (ga_scripts.ga_len < id)
19399 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019400 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019401 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019402 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 }
19405 }
19406}
19407
19408/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019409 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19410 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 */
19412 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019413init_var_dict(dict, dict_var)
19414 dict_T *dict;
19415 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416{
Bram Moolenaar33570922005-01-25 22:26:29 +000019417 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019418 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019419 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019420 dict_var->di_tv.vval.v_dict = dict;
19421 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019422 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019423 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19424 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425}
19426
19427/*
19428 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019429 * Frees all allocated variables and the value they contain.
19430 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431 */
19432 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019433vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019434 hashtab_T *ht;
19435{
19436 vars_clear_ext(ht, TRUE);
19437}
19438
19439/*
19440 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19441 */
19442 static void
19443vars_clear_ext(ht, free_val)
19444 hashtab_T *ht;
19445 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446{
Bram Moolenaara7043832005-01-21 11:56:39 +000019447 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019448 hashitem_T *hi;
19449 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450
Bram Moolenaar33570922005-01-25 22:26:29 +000019451 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019452 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019453 for (hi = ht->ht_array; todo > 0; ++hi)
19454 {
19455 if (!HASHITEM_EMPTY(hi))
19456 {
19457 --todo;
19458
Bram Moolenaar33570922005-01-25 22:26:29 +000019459 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019460 * ht_array might change then. hash_clear() takes care of it
19461 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019462 v = HI2DI(hi);
19463 if (free_val)
19464 clear_tv(&v->di_tv);
19465 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19466 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019467 }
19468 }
19469 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019470 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471}
19472
Bram Moolenaara7043832005-01-21 11:56:39 +000019473/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019474 * Delete a variable from hashtab "ht" at item "hi".
19475 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019478delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019479 hashtab_T *ht;
19480 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481{
Bram Moolenaar33570922005-01-25 22:26:29 +000019482 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019483
19484 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019485 clear_tv(&di->di_tv);
19486 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487}
19488
19489/*
19490 * List the value of one internal variable.
19491 */
19492 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019493list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019494 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019496 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019498 char_u *tofree;
19499 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019500 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019501
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019502 current_copyID += COPYID_INC;
19503 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019504 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019505 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019506 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507}
19508
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019510list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 char_u *prefix;
19512 char_u *name;
19513 int type;
19514 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019515 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516{
Bram Moolenaar31859182007-08-14 20:41:13 +000019517 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19518 msg_start();
19519 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 if (name != NULL) /* "a:" vars don't have a name stored */
19521 msg_puts(name);
19522 msg_putchar(' ');
19523 msg_advance(22);
19524 if (type == VAR_NUMBER)
19525 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019526 else if (type == VAR_FUNC)
19527 msg_putchar('*');
19528 else if (type == VAR_LIST)
19529 {
19530 msg_putchar('[');
19531 if (*string == '[')
19532 ++string;
19533 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019534 else if (type == VAR_DICT)
19535 {
19536 msg_putchar('{');
19537 if (*string == '{')
19538 ++string;
19539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540 else
19541 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019542
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019544
19545 if (type == VAR_FUNC)
19546 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019547 if (*first)
19548 {
19549 msg_clr_eos();
19550 *first = FALSE;
19551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552}
19553
19554/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019555 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556 * If the variable already exists, the value is updated.
19557 * Otherwise the variable is created.
19558 */
19559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019560set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019562 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019563 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564{
Bram Moolenaar33570922005-01-25 22:26:29 +000019565 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019567 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019568 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019570 ht = find_var_ht(name, &varname);
19571 if (ht == NULL || *varname == NUL)
19572 {
19573 EMSG2(_(e_illvar), name);
19574 return;
19575 }
19576 v = find_var_in_ht(ht, varname, TRUE);
19577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019578 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019579 {
19580 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19581 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19582 ? name[2] : name[0]))
19583 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019584 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019585 return;
19586 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019587 /* Don't allow hiding a function. When "v" is not NULL we migth be
19588 * assigning another function to the same var, the type is checked
19589 * below. */
19590 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019591 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019592 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019593 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019594 return;
19595 }
19596 }
19597
Bram Moolenaar33570922005-01-25 22:26:29 +000019598 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019600 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019601 if (var_check_ro(v->di_flags, name)
19602 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019603 return;
19604 if (v->di_tv.v_type != tv->v_type
19605 && !((v->di_tv.v_type == VAR_STRING
19606 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019607 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019608 || tv->v_type == VAR_NUMBER))
19609#ifdef FEAT_FLOAT
19610 && !((v->di_tv.v_type == VAR_NUMBER
19611 || v->di_tv.v_type == VAR_FLOAT)
19612 && (tv->v_type == VAR_NUMBER
19613 || tv->v_type == VAR_FLOAT))
19614#endif
19615 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019616 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019617 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019618 return;
19619 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019620
19621 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019622 * Handle setting internal v: variables separately: we don't change
19623 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019624 */
19625 if (ht == &vimvarht)
19626 {
19627 if (v->di_tv.v_type == VAR_STRING)
19628 {
19629 vim_free(v->di_tv.vval.v_string);
19630 if (copy || tv->v_type != VAR_STRING)
19631 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19632 else
19633 {
19634 /* Take over the string to avoid an extra alloc/free. */
19635 v->di_tv.vval.v_string = tv->vval.v_string;
19636 tv->vval.v_string = NULL;
19637 }
19638 }
19639 else if (v->di_tv.v_type != VAR_NUMBER)
19640 EMSG2(_(e_intern2), "set_var()");
19641 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019642 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019643 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019644 if (STRCMP(varname, "searchforward") == 0)
19645 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19646 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 return;
19648 }
19649
19650 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 }
19652 else /* add a new variable */
19653 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019654 /* Can't add "v:" variable. */
19655 if (ht == &vimvarht)
19656 {
19657 EMSG2(_(e_illvar), name);
19658 return;
19659 }
19660
Bram Moolenaar92124a32005-06-17 22:03:40 +000019661 /* Make sure the variable name is valid. */
19662 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019663 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19664 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019665 {
19666 EMSG2(_(e_illvar), varname);
19667 return;
19668 }
19669
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019670 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19671 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019672 if (v == NULL)
19673 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019674 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019675 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019677 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 return;
19679 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019680 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019682
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019683 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019684 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019685 else
19686 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019687 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019688 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019689 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691}
19692
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019693/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019694 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019695 * Also give an error message.
19696 */
19697 static int
19698var_check_ro(flags, name)
19699 int flags;
19700 char_u *name;
19701{
19702 if (flags & DI_FLAGS_RO)
19703 {
19704 EMSG2(_(e_readonlyvar), name);
19705 return TRUE;
19706 }
19707 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19708 {
19709 EMSG2(_(e_readonlysbx), name);
19710 return TRUE;
19711 }
19712 return FALSE;
19713}
19714
19715/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019716 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19717 * Also give an error message.
19718 */
19719 static int
19720var_check_fixed(flags, name)
19721 int flags;
19722 char_u *name;
19723{
19724 if (flags & DI_FLAGS_FIX)
19725 {
19726 EMSG2(_("E795: Cannot delete variable %s"), name);
19727 return TRUE;
19728 }
19729 return FALSE;
19730}
19731
19732/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019733 * Return TRUE if typeval "tv" is set to be locked (immutable).
19734 * Also give an error message, using "name".
19735 */
19736 static int
19737tv_check_lock(lock, name)
19738 int lock;
19739 char_u *name;
19740{
19741 if (lock & VAR_LOCKED)
19742 {
19743 EMSG2(_("E741: Value is locked: %s"),
19744 name == NULL ? (char_u *)_("Unknown") : name);
19745 return TRUE;
19746 }
19747 if (lock & VAR_FIXED)
19748 {
19749 EMSG2(_("E742: Cannot change value of %s"),
19750 name == NULL ? (char_u *)_("Unknown") : name);
19751 return TRUE;
19752 }
19753 return FALSE;
19754}
19755
19756/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019757 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019758 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019759 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019760 * It is OK for "from" and "to" to point to the same item. This is used to
19761 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019762 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019763 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019764copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019765 typval_T *from;
19766 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019768 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019769 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019770 switch (from->v_type)
19771 {
19772 case VAR_NUMBER:
19773 to->vval.v_number = from->vval.v_number;
19774 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019775#ifdef FEAT_FLOAT
19776 case VAR_FLOAT:
19777 to->vval.v_float = from->vval.v_float;
19778 break;
19779#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019780 case VAR_STRING:
19781 case VAR_FUNC:
19782 if (from->vval.v_string == NULL)
19783 to->vval.v_string = NULL;
19784 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019785 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019786 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019787 if (from->v_type == VAR_FUNC)
19788 func_ref(to->vval.v_string);
19789 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019790 break;
19791 case VAR_LIST:
19792 if (from->vval.v_list == NULL)
19793 to->vval.v_list = NULL;
19794 else
19795 {
19796 to->vval.v_list = from->vval.v_list;
19797 ++to->vval.v_list->lv_refcount;
19798 }
19799 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019800 case VAR_DICT:
19801 if (from->vval.v_dict == NULL)
19802 to->vval.v_dict = NULL;
19803 else
19804 {
19805 to->vval.v_dict = from->vval.v_dict;
19806 ++to->vval.v_dict->dv_refcount;
19807 }
19808 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019809 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019810 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019811 break;
19812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813}
19814
19815/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019816 * Make a copy of an item.
19817 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019818 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19819 * reference to an already copied list/dict can be used.
19820 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019821 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019822 static int
19823item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 typval_T *from;
19825 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019826 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019827 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019828{
19829 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019830 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019831
Bram Moolenaar33570922005-01-25 22:26:29 +000019832 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019833 {
19834 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019835 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019836 }
19837 ++recurse;
19838
19839 switch (from->v_type)
19840 {
19841 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019842#ifdef FEAT_FLOAT
19843 case VAR_FLOAT:
19844#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019845 case VAR_STRING:
19846 case VAR_FUNC:
19847 copy_tv(from, to);
19848 break;
19849 case VAR_LIST:
19850 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019851 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019852 if (from->vval.v_list == NULL)
19853 to->vval.v_list = NULL;
19854 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
19855 {
19856 /* use the copy made earlier */
19857 to->vval.v_list = from->vval.v_list->lv_copylist;
19858 ++to->vval.v_list->lv_refcount;
19859 }
19860 else
19861 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
19862 if (to->vval.v_list == NULL)
19863 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019864 break;
19865 case VAR_DICT:
19866 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019867 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019868 if (from->vval.v_dict == NULL)
19869 to->vval.v_dict = NULL;
19870 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
19871 {
19872 /* use the copy made earlier */
19873 to->vval.v_dict = from->vval.v_dict->dv_copydict;
19874 ++to->vval.v_dict->dv_refcount;
19875 }
19876 else
19877 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
19878 if (to->vval.v_dict == NULL)
19879 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019880 break;
19881 default:
19882 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019883 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019884 }
19885 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019886 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019887}
19888
19889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 * ":echo expr1 ..." print each argument separated with a space, add a
19891 * newline at the end.
19892 * ":echon expr1 ..." print each argument plain.
19893 */
19894 void
19895ex_echo(eap)
19896 exarg_T *eap;
19897{
19898 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000019899 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019900 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 char_u *p;
19902 int needclr = TRUE;
19903 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019904 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905
19906 if (eap->skip)
19907 ++emsg_skip;
19908 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
19909 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019910 /* If eval1() causes an error message the text from the command may
19911 * still need to be cleared. E.g., "echo 22,44". */
19912 need_clr_eos = needclr;
19913
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019915 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 {
19917 /*
19918 * Report the invalid expression unless the expression evaluation
19919 * has been cancelled due to an aborting error, an interrupt, or an
19920 * exception.
19921 */
19922 if (!aborting())
19923 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019924 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 break;
19926 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019927 need_clr_eos = FALSE;
19928
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 if (!eap->skip)
19930 {
19931 if (atstart)
19932 {
19933 atstart = FALSE;
19934 /* Call msg_start() after eval1(), evaluating the expression
19935 * may cause a message to appear. */
19936 if (eap->cmdidx == CMD_echo)
19937 msg_start();
19938 }
19939 else if (eap->cmdidx == CMD_echo)
19940 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019941 current_copyID += COPYID_INC;
19942 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019943 if (p != NULL)
19944 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019946 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019948 if (*p != TAB && needclr)
19949 {
19950 /* remove any text still there from the command */
19951 msg_clr_eos();
19952 needclr = FALSE;
19953 }
19954 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 }
19956 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019957 {
19958#ifdef FEAT_MBYTE
19959 if (has_mbyte)
19960 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019961 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019962
19963 (void)msg_outtrans_len_attr(p, i, echo_attr);
19964 p += i - 1;
19965 }
19966 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019968 (void)msg_outtrans_len_attr(p, 1, echo_attr);
19969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019971 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019973 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974 arg = skipwhite(arg);
19975 }
19976 eap->nextcmd = check_nextcmd(arg);
19977
19978 if (eap->skip)
19979 --emsg_skip;
19980 else
19981 {
19982 /* remove text that may still be there from the command */
19983 if (needclr)
19984 msg_clr_eos();
19985 if (eap->cmdidx == CMD_echo)
19986 msg_end();
19987 }
19988}
19989
19990/*
19991 * ":echohl {name}".
19992 */
19993 void
19994ex_echohl(eap)
19995 exarg_T *eap;
19996{
19997 int id;
19998
19999 id = syn_name2id(eap->arg);
20000 if (id == 0)
20001 echo_attr = 0;
20002 else
20003 echo_attr = syn_id2attr(id);
20004}
20005
20006/*
20007 * ":execute expr1 ..." execute the result of an expression.
20008 * ":echomsg expr1 ..." Print a message
20009 * ":echoerr expr1 ..." Print an error
20010 * Each gets spaces around each argument and a newline at the end for
20011 * echo commands
20012 */
20013 void
20014ex_execute(eap)
20015 exarg_T *eap;
20016{
20017 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020018 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 int ret = OK;
20020 char_u *p;
20021 garray_T ga;
20022 int len;
20023 int save_did_emsg;
20024
20025 ga_init2(&ga, 1, 80);
20026
20027 if (eap->skip)
20028 ++emsg_skip;
20029 while (*arg != NUL && *arg != '|' && *arg != '\n')
20030 {
20031 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020032 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 {
20034 /*
20035 * Report the invalid expression unless the expression evaluation
20036 * has been cancelled due to an aborting error, an interrupt, or an
20037 * exception.
20038 */
20039 if (!aborting())
20040 EMSG2(_(e_invexpr2), p);
20041 ret = FAIL;
20042 break;
20043 }
20044
20045 if (!eap->skip)
20046 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 len = (int)STRLEN(p);
20049 if (ga_grow(&ga, len + 2) == FAIL)
20050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020051 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 ret = FAIL;
20053 break;
20054 }
20055 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 ga.ga_len += len;
20059 }
20060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020061 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 arg = skipwhite(arg);
20063 }
20064
20065 if (ret != FAIL && ga.ga_data != NULL)
20066 {
20067 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020068 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020070 out_flush();
20071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 else if (eap->cmdidx == CMD_echoerr)
20073 {
20074 /* We don't want to abort following commands, restore did_emsg. */
20075 save_did_emsg = did_emsg;
20076 EMSG((char_u *)ga.ga_data);
20077 if (!force_abort)
20078 did_emsg = save_did_emsg;
20079 }
20080 else if (eap->cmdidx == CMD_execute)
20081 do_cmdline((char_u *)ga.ga_data,
20082 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20083 }
20084
20085 ga_clear(&ga);
20086
20087 if (eap->skip)
20088 --emsg_skip;
20089
20090 eap->nextcmd = check_nextcmd(arg);
20091}
20092
20093/*
20094 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20095 * "arg" points to the "&" or '+' when called, to "option" when returning.
20096 * Returns NULL when no option name found. Otherwise pointer to the char
20097 * after the option name.
20098 */
20099 static char_u *
20100find_option_end(arg, opt_flags)
20101 char_u **arg;
20102 int *opt_flags;
20103{
20104 char_u *p = *arg;
20105
20106 ++p;
20107 if (*p == 'g' && p[1] == ':')
20108 {
20109 *opt_flags = OPT_GLOBAL;
20110 p += 2;
20111 }
20112 else if (*p == 'l' && p[1] == ':')
20113 {
20114 *opt_flags = OPT_LOCAL;
20115 p += 2;
20116 }
20117 else
20118 *opt_flags = 0;
20119
20120 if (!ASCII_ISALPHA(*p))
20121 return NULL;
20122 *arg = p;
20123
20124 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20125 p += 4; /* termcap option */
20126 else
20127 while (ASCII_ISALPHA(*p))
20128 ++p;
20129 return p;
20130}
20131
20132/*
20133 * ":function"
20134 */
20135 void
20136ex_function(eap)
20137 exarg_T *eap;
20138{
20139 char_u *theline;
20140 int j;
20141 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 char_u *name = NULL;
20144 char_u *p;
20145 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020146 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 garray_T newargs;
20148 garray_T newlines;
20149 int varargs = FALSE;
20150 int mustend = FALSE;
20151 int flags = 0;
20152 ufunc_T *fp;
20153 int indent;
20154 int nesting;
20155 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020156 dictitem_T *v;
20157 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020158 static int func_nr = 0; /* number for nameless function */
20159 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020160 hashtab_T *ht;
20161 int todo;
20162 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020163 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164
20165 /*
20166 * ":function" without argument: list functions.
20167 */
20168 if (ends_excmd(*eap->arg))
20169 {
20170 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020171 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020172 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020173 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020174 {
20175 if (!HASHITEM_EMPTY(hi))
20176 {
20177 --todo;
20178 fp = HI2UF(hi);
20179 if (!isdigit(*fp->uf_name))
20180 list_func_head(fp, FALSE);
20181 }
20182 }
20183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 eap->nextcmd = check_nextcmd(eap->arg);
20185 return;
20186 }
20187
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020188 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020189 * ":function /pat": list functions matching pattern.
20190 */
20191 if (*eap->arg == '/')
20192 {
20193 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20194 if (!eap->skip)
20195 {
20196 regmatch_T regmatch;
20197
20198 c = *p;
20199 *p = NUL;
20200 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20201 *p = c;
20202 if (regmatch.regprog != NULL)
20203 {
20204 regmatch.rm_ic = p_ic;
20205
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020206 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020207 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20208 {
20209 if (!HASHITEM_EMPTY(hi))
20210 {
20211 --todo;
20212 fp = HI2UF(hi);
20213 if (!isdigit(*fp->uf_name)
20214 && vim_regexec(&regmatch, fp->uf_name, 0))
20215 list_func_head(fp, FALSE);
20216 }
20217 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020218 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020219 }
20220 }
20221 if (*p == '/')
20222 ++p;
20223 eap->nextcmd = check_nextcmd(p);
20224 return;
20225 }
20226
20227 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020228 * Get the function name. There are these situations:
20229 * func normal function name
20230 * "name" == func, "fudi.fd_dict" == NULL
20231 * dict.func new dictionary entry
20232 * "name" == NULL, "fudi.fd_dict" set,
20233 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20234 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020235 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020236 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20237 * dict.func existing dict entry that's not a Funcref
20238 * "name" == NULL, "fudi.fd_dict" set,
20239 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020242 name = trans_function_name(&p, eap->skip, 0, &fudi);
20243 paren = (vim_strchr(p, '(') != NULL);
20244 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 {
20246 /*
20247 * Return on an invalid expression in braces, unless the expression
20248 * evaluation has been cancelled due to an aborting error, an
20249 * interrupt, or an exception.
20250 */
20251 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020252 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020253 if (!eap->skip && fudi.fd_newkey != NULL)
20254 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020255 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258 else
20259 eap->skip = TRUE;
20260 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020261
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 /* An error in a function call during evaluation of an expression in magic
20263 * braces should not cause the function not to be defined. */
20264 saved_did_emsg = did_emsg;
20265 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266
20267 /*
20268 * ":function func" with only function name: list function.
20269 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020270 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271 {
20272 if (!ends_excmd(*skipwhite(p)))
20273 {
20274 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020275 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 }
20277 eap->nextcmd = check_nextcmd(p);
20278 if (eap->nextcmd != NULL)
20279 *p = NUL;
20280 if (!eap->skip && !got_int)
20281 {
20282 fp = find_func(name);
20283 if (fp != NULL)
20284 {
20285 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020286 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020288 if (FUNCLINE(fp, j) == NULL)
20289 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290 msg_putchar('\n');
20291 msg_outnum((long)(j + 1));
20292 if (j < 9)
20293 msg_putchar(' ');
20294 if (j < 99)
20295 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020296 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020297 out_flush(); /* show a line at a time */
20298 ui_breakcheck();
20299 }
20300 if (!got_int)
20301 {
20302 msg_putchar('\n');
20303 msg_puts((char_u *)" endfunction");
20304 }
20305 }
20306 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020307 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020309 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 }
20311
20312 /*
20313 * ":function name(arg1, arg2)" Define function.
20314 */
20315 p = skipwhite(p);
20316 if (*p != '(')
20317 {
20318 if (!eap->skip)
20319 {
20320 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020321 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 }
20323 /* attempt to continue by skipping some text */
20324 if (vim_strchr(p, '(') != NULL)
20325 p = vim_strchr(p, '(');
20326 }
20327 p = skipwhite(p + 1);
20328
20329 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20330 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20331
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020332 if (!eap->skip)
20333 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020334 /* Check the name of the function. Unless it's a dictionary function
20335 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020336 if (name != NULL)
20337 arg = name;
20338 else
20339 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020340 if (arg != NULL && (fudi.fd_di == NULL
20341 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020342 {
20343 if (*arg == K_SPECIAL)
20344 j = 3;
20345 else
20346 j = 0;
20347 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20348 : eval_isnamec(arg[j])))
20349 ++j;
20350 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020351 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020352 }
20353 }
20354
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 /*
20356 * Isolate the arguments: "arg1, arg2, ...)"
20357 */
20358 while (*p != ')')
20359 {
20360 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20361 {
20362 varargs = TRUE;
20363 p += 3;
20364 mustend = TRUE;
20365 }
20366 else
20367 {
20368 arg = p;
20369 while (ASCII_ISALNUM(*p) || *p == '_')
20370 ++p;
20371 if (arg == p || isdigit(*arg)
20372 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20373 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20374 {
20375 if (!eap->skip)
20376 EMSG2(_("E125: Illegal argument: %s"), arg);
20377 break;
20378 }
20379 if (ga_grow(&newargs, 1) == FAIL)
20380 goto erret;
20381 c = *p;
20382 *p = NUL;
20383 arg = vim_strsave(arg);
20384 if (arg == NULL)
20385 goto erret;
20386 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20387 *p = c;
20388 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389 if (*p == ',')
20390 ++p;
20391 else
20392 mustend = TRUE;
20393 }
20394 p = skipwhite(p);
20395 if (mustend && *p != ')')
20396 {
20397 if (!eap->skip)
20398 EMSG2(_(e_invarg2), eap->arg);
20399 break;
20400 }
20401 }
20402 ++p; /* skip the ')' */
20403
Bram Moolenaare9a41262005-01-15 22:18:47 +000020404 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 for (;;)
20406 {
20407 p = skipwhite(p);
20408 if (STRNCMP(p, "range", 5) == 0)
20409 {
20410 flags |= FC_RANGE;
20411 p += 5;
20412 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020413 else if (STRNCMP(p, "dict", 4) == 0)
20414 {
20415 flags |= FC_DICT;
20416 p += 4;
20417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 else if (STRNCMP(p, "abort", 5) == 0)
20419 {
20420 flags |= FC_ABORT;
20421 p += 5;
20422 }
20423 else
20424 break;
20425 }
20426
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020427 /* When there is a line break use what follows for the function body.
20428 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20429 if (*p == '\n')
20430 line_arg = p + 1;
20431 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 EMSG(_(e_trailing));
20433
20434 /*
20435 * Read the body of the function, until ":endfunction" is found.
20436 */
20437 if (KeyTyped)
20438 {
20439 /* Check if the function already exists, don't let the user type the
20440 * whole function before telling him it doesn't work! For a script we
20441 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020442 if (!eap->skip && !eap->forceit)
20443 {
20444 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20445 EMSG(_(e_funcdict));
20446 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020447 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020450 if (!eap->skip && did_emsg)
20451 goto erret;
20452
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 msg_putchar('\n'); /* don't overwrite the function name */
20454 cmdline_row = msg_row;
20455 }
20456
20457 indent = 2;
20458 nesting = 0;
20459 for (;;)
20460 {
20461 msg_scroll = TRUE;
20462 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020463 sourcing_lnum_off = sourcing_lnum;
20464
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020465 if (line_arg != NULL)
20466 {
20467 /* Use eap->arg, split up in parts by line breaks. */
20468 theline = line_arg;
20469 p = vim_strchr(theline, '\n');
20470 if (p == NULL)
20471 line_arg += STRLEN(line_arg);
20472 else
20473 {
20474 *p = NUL;
20475 line_arg = p + 1;
20476 }
20477 }
20478 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 theline = getcmdline(':', 0L, indent);
20480 else
20481 theline = eap->getline(':', eap->cookie, indent);
20482 if (KeyTyped)
20483 lines_left = Rows - 1;
20484 if (theline == NULL)
20485 {
20486 EMSG(_("E126: Missing :endfunction"));
20487 goto erret;
20488 }
20489
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020490 /* Detect line continuation: sourcing_lnum increased more than one. */
20491 if (sourcing_lnum > sourcing_lnum_off + 1)
20492 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20493 else
20494 sourcing_lnum_off = 0;
20495
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 if (skip_until != NULL)
20497 {
20498 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20499 * don't check for ":endfunc". */
20500 if (STRCMP(theline, skip_until) == 0)
20501 {
20502 vim_free(skip_until);
20503 skip_until = NULL;
20504 }
20505 }
20506 else
20507 {
20508 /* skip ':' and blanks*/
20509 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20510 ;
20511
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020512 /* Check for "endfunction". */
20513 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020515 if (line_arg == NULL)
20516 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 break;
20518 }
20519
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020520 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521 * at "end". */
20522 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20523 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020524 else if (STRNCMP(p, "if", 2) == 0
20525 || STRNCMP(p, "wh", 2) == 0
20526 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 || STRNCMP(p, "try", 3) == 0)
20528 indent += 2;
20529
20530 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020531 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020533 if (*p == '!')
20534 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 p += eval_fname_script(p);
20536 if (ASCII_ISALPHA(*p))
20537 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020538 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 if (*skipwhite(p) == '(')
20540 {
20541 ++nesting;
20542 indent += 2;
20543 }
20544 }
20545 }
20546
20547 /* Check for ":append" or ":insert". */
20548 p = skip_range(p, NULL);
20549 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20550 || (p[0] == 'i'
20551 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20552 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20553 skip_until = vim_strsave((char_u *)".");
20554
20555 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20556 arg = skipwhite(skiptowhite(p));
20557 if (arg[0] == '<' && arg[1] =='<'
20558 && ((p[0] == 'p' && p[1] == 'y'
20559 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20560 || (p[0] == 'p' && p[1] == 'e'
20561 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20562 || (p[0] == 't' && p[1] == 'c'
20563 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20564 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20565 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020566 || (p[0] == 'm' && p[1] == 'z'
20567 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568 ))
20569 {
20570 /* ":python <<" continues until a dot, like ":append" */
20571 p = skipwhite(arg + 2);
20572 if (*p == NUL)
20573 skip_until = vim_strsave((char_u *)".");
20574 else
20575 skip_until = vim_strsave(p);
20576 }
20577 }
20578
20579 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020580 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020581 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020582 if (line_arg == NULL)
20583 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020585 }
20586
20587 /* Copy the line to newly allocated memory. get_one_sourceline()
20588 * allocates 250 bytes per line, this saves 80% on average. The cost
20589 * is an extra alloc/free. */
20590 p = vim_strsave(theline);
20591 if (p != NULL)
20592 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020593 if (line_arg == NULL)
20594 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020595 theline = p;
20596 }
20597
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020598 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20599
20600 /* Add NULL lines for continuation lines, so that the line count is
20601 * equal to the index in the growarray. */
20602 while (sourcing_lnum_off-- > 0)
20603 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020604
20605 /* Check for end of eap->arg. */
20606 if (line_arg != NULL && *line_arg == NUL)
20607 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608 }
20609
20610 /* Don't define the function when skipping commands or when an error was
20611 * detected. */
20612 if (eap->skip || did_emsg)
20613 goto erret;
20614
20615 /*
20616 * If there are no errors, add the function
20617 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020618 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020619 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020620 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020621 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020622 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020623 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020624 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020625 goto erret;
20626 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020627
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020628 fp = find_func(name);
20629 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020631 if (!eap->forceit)
20632 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020633 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020634 goto erret;
20635 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020636 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020637 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020638 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020639 name);
20640 goto erret;
20641 }
20642 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020643 ga_clear_strings(&(fp->uf_args));
20644 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020645 vim_free(name);
20646 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020648 }
20649 else
20650 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020651 char numbuf[20];
20652
20653 fp = NULL;
20654 if (fudi.fd_newkey == NULL && !eap->forceit)
20655 {
20656 EMSG(_(e_funcdict));
20657 goto erret;
20658 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020659 if (fudi.fd_di == NULL)
20660 {
20661 /* Can't add a function to a locked dictionary */
20662 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20663 goto erret;
20664 }
20665 /* Can't change an existing function if it is locked */
20666 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20667 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020668
20669 /* Give the function a sequential number. Can only be used with a
20670 * Funcref! */
20671 vim_free(name);
20672 sprintf(numbuf, "%d", ++func_nr);
20673 name = vim_strsave((char_u *)numbuf);
20674 if (name == NULL)
20675 goto erret;
20676 }
20677
20678 if (fp == NULL)
20679 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020680 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020681 {
20682 int slen, plen;
20683 char_u *scriptname;
20684
20685 /* Check that the autoload name matches the script name. */
20686 j = FAIL;
20687 if (sourcing_name != NULL)
20688 {
20689 scriptname = autoload_name(name);
20690 if (scriptname != NULL)
20691 {
20692 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020693 plen = (int)STRLEN(p);
20694 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020695 if (slen > plen && fnamecmp(p,
20696 sourcing_name + slen - plen) == 0)
20697 j = OK;
20698 vim_free(scriptname);
20699 }
20700 }
20701 if (j == FAIL)
20702 {
20703 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20704 goto erret;
20705 }
20706 }
20707
20708 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709 if (fp == NULL)
20710 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020711
20712 if (fudi.fd_dict != NULL)
20713 {
20714 if (fudi.fd_di == NULL)
20715 {
20716 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020717 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020718 if (fudi.fd_di == NULL)
20719 {
20720 vim_free(fp);
20721 goto erret;
20722 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020723 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20724 {
20725 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020726 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020727 goto erret;
20728 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020729 }
20730 else
20731 /* overwrite existing dict entry */
20732 clear_tv(&fudi.fd_di->di_tv);
20733 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020734 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020735 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020736 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020737
20738 /* behave like "dict" was used */
20739 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020740 }
20741
Bram Moolenaar071d4272004-06-13 20:20:40 +000020742 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020743 STRCPY(fp->uf_name, name);
20744 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020746 fp->uf_args = newargs;
20747 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020748#ifdef FEAT_PROFILE
20749 fp->uf_tml_count = NULL;
20750 fp->uf_tml_total = NULL;
20751 fp->uf_tml_self = NULL;
20752 fp->uf_profiling = FALSE;
20753 if (prof_def_func())
20754 func_do_profile(fp);
20755#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020756 fp->uf_varargs = varargs;
20757 fp->uf_flags = flags;
20758 fp->uf_calls = 0;
20759 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020760 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761
20762erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763 ga_clear_strings(&newargs);
20764 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020765ret_free:
20766 vim_free(skip_until);
20767 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770}
20771
20772/*
20773 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020774 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020776 * flags:
20777 * TFN_INT: internal function name OK
20778 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779 * Advances "pp" to just after the function name (if no error).
20780 */
20781 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020782trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 char_u **pp;
20784 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020785 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020786 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020788 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789 char_u *start;
20790 char_u *end;
20791 int lead;
20792 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020794 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020795
20796 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020797 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020799
20800 /* Check for hard coded <SNR>: already translated function ID (from a user
20801 * command). */
20802 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20803 && (*pp)[2] == (int)KE_SNR)
20804 {
20805 *pp += 3;
20806 len = get_id_len(pp) + 3;
20807 return vim_strnsave(start, len);
20808 }
20809
20810 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20811 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020813 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020815
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020816 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20817 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020818 if (end == start)
20819 {
20820 if (!skip)
20821 EMSG(_("E129: Function name required"));
20822 goto theend;
20823 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020824 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020825 {
20826 /*
20827 * Report an invalid expression in braces, unless the expression
20828 * evaluation has been cancelled due to an aborting error, an
20829 * interrupt, or an exception.
20830 */
20831 if (!aborting())
20832 {
20833 if (end != NULL)
20834 EMSG2(_(e_invarg2), start);
20835 }
20836 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020837 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020838 goto theend;
20839 }
20840
20841 if (lv.ll_tv != NULL)
20842 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020843 if (fdp != NULL)
20844 {
20845 fdp->fd_dict = lv.ll_dict;
20846 fdp->fd_newkey = lv.ll_newkey;
20847 lv.ll_newkey = NULL;
20848 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020849 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020850 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
20851 {
20852 name = vim_strsave(lv.ll_tv->vval.v_string);
20853 *pp = end;
20854 }
20855 else
20856 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020857 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
20858 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020859 EMSG(_(e_funcref));
20860 else
20861 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020862 name = NULL;
20863 }
20864 goto theend;
20865 }
20866
20867 if (lv.ll_name == NULL)
20868 {
20869 /* Error found, but continue after the function name. */
20870 *pp = end;
20871 goto theend;
20872 }
20873
Bram Moolenaar33e1a802007-09-06 12:26:44 +000020874 /* Check if the name is a Funcref. If so, use the value. */
20875 if (lv.ll_exp_name != NULL)
20876 {
20877 len = (int)STRLEN(lv.ll_exp_name);
20878 name = deref_func_name(lv.ll_exp_name, &len);
20879 if (name == lv.ll_exp_name)
20880 name = NULL;
20881 }
20882 else
20883 {
20884 len = (int)(end - *pp);
20885 name = deref_func_name(*pp, &len);
20886 if (name == *pp)
20887 name = NULL;
20888 }
20889 if (name != NULL)
20890 {
20891 name = vim_strsave(name);
20892 *pp = end;
20893 goto theend;
20894 }
20895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020896 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020897 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020898 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000020899 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
20900 && STRNCMP(lv.ll_name, "s:", 2) == 0)
20901 {
20902 /* When there was "s:" already or the name expanded to get a
20903 * leading "s:" then remove it. */
20904 lv.ll_name += 2;
20905 len -= 2;
20906 lead = 2;
20907 }
20908 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020909 else
Bram Moolenaara7043832005-01-21 11:56:39 +000020910 {
20911 if (lead == 2) /* skip over "s:" */
20912 lv.ll_name += 2;
20913 len = (int)(end - lv.ll_name);
20914 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020915
20916 /*
20917 * Copy the function name to allocated memory.
20918 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20919 * Accept <SNR>123_name() outside a script.
20920 */
20921 if (skip)
20922 lead = 0; /* do nothing */
20923 else if (lead > 0)
20924 {
20925 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000020926 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
20927 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020928 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000020929 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020930 if (current_SID <= 0)
20931 {
20932 EMSG(_(e_usingsid));
20933 goto theend;
20934 }
20935 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
20936 lead += (int)STRLEN(sid_buf);
20937 }
20938 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020939 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020940 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000020941 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020942 goto theend;
20943 }
20944 name = alloc((unsigned)(len + lead + 1));
20945 if (name != NULL)
20946 {
20947 if (lead > 0)
20948 {
20949 name[0] = K_SPECIAL;
20950 name[1] = KS_EXTRA;
20951 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000020952 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020953 STRCPY(name + 3, sid_buf);
20954 }
20955 mch_memmove(name + lead, lv.ll_name, (size_t)len);
20956 name[len + lead] = NUL;
20957 }
20958 *pp = end;
20959
20960theend:
20961 clear_lval(&lv);
20962 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963}
20964
20965/*
20966 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20967 * Return 2 if "p" starts with "s:".
20968 * Return 0 otherwise.
20969 */
20970 static int
20971eval_fname_script(p)
20972 char_u *p;
20973{
20974 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
20975 || STRNICMP(p + 1, "SNR>", 4) == 0))
20976 return 5;
20977 if (p[0] == 's' && p[1] == ':')
20978 return 2;
20979 return 0;
20980}
20981
20982/*
20983 * Return TRUE if "p" starts with "<SID>" or "s:".
20984 * Only works if eval_fname_script() returned non-zero for "p"!
20985 */
20986 static int
20987eval_fname_sid(p)
20988 char_u *p;
20989{
20990 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
20991}
20992
20993/*
20994 * List the head of the function: "name(arg1, arg2)".
20995 */
20996 static void
20997list_func_head(fp, indent)
20998 ufunc_T *fp;
20999 int indent;
21000{
21001 int j;
21002
21003 msg_start();
21004 if (indent)
21005 MSG_PUTS(" ");
21006 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021007 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 {
21009 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021010 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 }
21012 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021013 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021015 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 {
21017 if (j)
21018 MSG_PUTS(", ");
21019 msg_puts(FUNCARG(fp, j));
21020 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021021 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 {
21023 if (j)
21024 MSG_PUTS(", ");
21025 MSG_PUTS("...");
21026 }
21027 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021028 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021029 if (p_verbose > 0)
21030 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031}
21032
21033/*
21034 * Find a function by name, return pointer to it in ufuncs.
21035 * Return NULL for unknown function.
21036 */
21037 static ufunc_T *
21038find_func(name)
21039 char_u *name;
21040{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021041 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021043 hi = hash_find(&func_hashtab, name);
21044 if (!HASHITEM_EMPTY(hi))
21045 return HI2UF(hi);
21046 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047}
21048
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021049#if defined(EXITFREE) || defined(PROTO)
21050 void
21051free_all_functions()
21052{
21053 hashitem_T *hi;
21054
21055 /* Need to start all over every time, because func_free() may change the
21056 * hash table. */
21057 while (func_hashtab.ht_used > 0)
21058 for (hi = func_hashtab.ht_array; ; ++hi)
21059 if (!HASHITEM_EMPTY(hi))
21060 {
21061 func_free(HI2UF(hi));
21062 break;
21063 }
21064}
21065#endif
21066
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021067/*
21068 * Return TRUE if a function "name" exists.
21069 */
21070 static int
21071function_exists(name)
21072 char_u *name;
21073{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021074 char_u *nm = name;
21075 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021076 int n = FALSE;
21077
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021078 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021079 nm = skipwhite(nm);
21080
21081 /* Only accept "funcname", "funcname ", "funcname (..." and
21082 * "funcname(...", not "funcname!...". */
21083 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021084 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021085 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021086 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021087 else
21088 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021089 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021090 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021091 return n;
21092}
21093
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021094/*
21095 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021096 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021097 */
21098 static int
21099builtin_function(name)
21100 char_u *name;
21101{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021102 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21103 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021104}
21105
Bram Moolenaar05159a02005-02-26 23:04:13 +000021106#if defined(FEAT_PROFILE) || defined(PROTO)
21107/*
21108 * Start profiling function "fp".
21109 */
21110 static void
21111func_do_profile(fp)
21112 ufunc_T *fp;
21113{
21114 fp->uf_tm_count = 0;
21115 profile_zero(&fp->uf_tm_self);
21116 profile_zero(&fp->uf_tm_total);
21117 if (fp->uf_tml_count == NULL)
21118 fp->uf_tml_count = (int *)alloc_clear((unsigned)
21119 (sizeof(int) * fp->uf_lines.ga_len));
21120 if (fp->uf_tml_total == NULL)
21121 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
21122 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21123 if (fp->uf_tml_self == NULL)
21124 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
21125 (sizeof(proftime_T) * fp->uf_lines.ga_len));
21126 fp->uf_tml_idx = -1;
21127 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21128 || fp->uf_tml_self == NULL)
21129 return; /* out of memory */
21130
21131 fp->uf_profiling = TRUE;
21132}
21133
21134/*
21135 * Dump the profiling results for all functions in file "fd".
21136 */
21137 void
21138func_dump_profile(fd)
21139 FILE *fd;
21140{
21141 hashitem_T *hi;
21142 int todo;
21143 ufunc_T *fp;
21144 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021145 ufunc_T **sorttab;
21146 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021148 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021149 if (todo == 0)
21150 return; /* nothing to dump */
21151
Bram Moolenaar73830342005-02-28 22:48:19 +000021152 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21153
Bram Moolenaar05159a02005-02-26 23:04:13 +000021154 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21155 {
21156 if (!HASHITEM_EMPTY(hi))
21157 {
21158 --todo;
21159 fp = HI2UF(hi);
21160 if (fp->uf_profiling)
21161 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021162 if (sorttab != NULL)
21163 sorttab[st_len++] = fp;
21164
Bram Moolenaar05159a02005-02-26 23:04:13 +000021165 if (fp->uf_name[0] == K_SPECIAL)
21166 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21167 else
21168 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21169 if (fp->uf_tm_count == 1)
21170 fprintf(fd, "Called 1 time\n");
21171 else
21172 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21173 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21174 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21175 fprintf(fd, "\n");
21176 fprintf(fd, "count total (s) self (s)\n");
21177
21178 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21179 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021180 if (FUNCLINE(fp, i) == NULL)
21181 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021182 prof_func_line(fd, fp->uf_tml_count[i],
21183 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021184 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21185 }
21186 fprintf(fd, "\n");
21187 }
21188 }
21189 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021190
21191 if (sorttab != NULL && st_len > 0)
21192 {
21193 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21194 prof_total_cmp);
21195 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21196 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21197 prof_self_cmp);
21198 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21199 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021200
21201 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021202}
Bram Moolenaar73830342005-02-28 22:48:19 +000021203
21204 static void
21205prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21206 FILE *fd;
21207 ufunc_T **sorttab;
21208 int st_len;
21209 char *title;
21210 int prefer_self; /* when equal print only self time */
21211{
21212 int i;
21213 ufunc_T *fp;
21214
21215 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21216 fprintf(fd, "count total (s) self (s) function\n");
21217 for (i = 0; i < 20 && i < st_len; ++i)
21218 {
21219 fp = sorttab[i];
21220 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21221 prefer_self);
21222 if (fp->uf_name[0] == K_SPECIAL)
21223 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21224 else
21225 fprintf(fd, " %s()\n", fp->uf_name);
21226 }
21227 fprintf(fd, "\n");
21228}
21229
21230/*
21231 * Print the count and times for one function or function line.
21232 */
21233 static void
21234prof_func_line(fd, count, total, self, prefer_self)
21235 FILE *fd;
21236 int count;
21237 proftime_T *total;
21238 proftime_T *self;
21239 int prefer_self; /* when equal print only self time */
21240{
21241 if (count > 0)
21242 {
21243 fprintf(fd, "%5d ", count);
21244 if (prefer_self && profile_equal(total, self))
21245 fprintf(fd, " ");
21246 else
21247 fprintf(fd, "%s ", profile_msg(total));
21248 if (!prefer_self && profile_equal(total, self))
21249 fprintf(fd, " ");
21250 else
21251 fprintf(fd, "%s ", profile_msg(self));
21252 }
21253 else
21254 fprintf(fd, " ");
21255}
21256
21257/*
21258 * Compare function for total time sorting.
21259 */
21260 static int
21261#ifdef __BORLANDC__
21262_RTLENTRYF
21263#endif
21264prof_total_cmp(s1, s2)
21265 const void *s1;
21266 const void *s2;
21267{
21268 ufunc_T *p1, *p2;
21269
21270 p1 = *(ufunc_T **)s1;
21271 p2 = *(ufunc_T **)s2;
21272 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21273}
21274
21275/*
21276 * Compare function for self time sorting.
21277 */
21278 static int
21279#ifdef __BORLANDC__
21280_RTLENTRYF
21281#endif
21282prof_self_cmp(s1, s2)
21283 const void *s1;
21284 const void *s2;
21285{
21286 ufunc_T *p1, *p2;
21287
21288 p1 = *(ufunc_T **)s1;
21289 p2 = *(ufunc_T **)s2;
21290 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21291}
21292
Bram Moolenaar05159a02005-02-26 23:04:13 +000021293#endif
21294
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021295/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021296 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021297 * Return TRUE if a package was loaded.
21298 */
21299 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021300script_autoload(name, reload)
21301 char_u *name;
21302 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021303{
21304 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021305 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021306 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021307 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021308
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021309 /* Return quickly when autoload disabled. */
21310 if (no_autoload)
21311 return FALSE;
21312
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021313 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021314 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021315 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021316 return FALSE;
21317
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021318 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021319
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021320 /* Find the name in the list of previously loaded package names. Skip
21321 * "autoload/", it's always the same. */
21322 for (i = 0; i < ga_loaded.ga_len; ++i)
21323 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21324 break;
21325 if (!reload && i < ga_loaded.ga_len)
21326 ret = FALSE; /* was loaded already */
21327 else
21328 {
21329 /* Remember the name if it wasn't loaded already. */
21330 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21331 {
21332 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21333 tofree = NULL;
21334 }
21335
21336 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021337 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021338 ret = TRUE;
21339 }
21340
21341 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021342 return ret;
21343}
21344
21345/*
21346 * Return the autoload script name for a function or variable name.
21347 * Returns NULL when out of memory.
21348 */
21349 static char_u *
21350autoload_name(name)
21351 char_u *name;
21352{
21353 char_u *p;
21354 char_u *scriptname;
21355
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021356 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021357 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21358 if (scriptname == NULL)
21359 return FALSE;
21360 STRCPY(scriptname, "autoload/");
21361 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021362 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021363 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021364 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021365 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021366 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021367}
21368
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21370
21371/*
21372 * Function given to ExpandGeneric() to obtain the list of user defined
21373 * function names.
21374 */
21375 char_u *
21376get_user_func_name(xp, idx)
21377 expand_T *xp;
21378 int idx;
21379{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021380 static long_u done;
21381 static hashitem_T *hi;
21382 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383
21384 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021386 done = 0;
21387 hi = func_hashtab.ht_array;
21388 }
21389 if (done < func_hashtab.ht_used)
21390 {
21391 if (done++ > 0)
21392 ++hi;
21393 while (HASHITEM_EMPTY(hi))
21394 ++hi;
21395 fp = HI2UF(hi);
21396
21397 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21398 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399
21400 cat_func_name(IObuff, fp);
21401 if (xp->xp_context != EXPAND_USER_FUNC)
21402 {
21403 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021404 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 STRCAT(IObuff, ")");
21406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407 return IObuff;
21408 }
21409 return NULL;
21410}
21411
21412#endif /* FEAT_CMDL_COMPL */
21413
21414/*
21415 * Copy the function name of "fp" to buffer "buf".
21416 * "buf" must be able to hold the function name plus three bytes.
21417 * Takes care of script-local function names.
21418 */
21419 static void
21420cat_func_name(buf, fp)
21421 char_u *buf;
21422 ufunc_T *fp;
21423{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021424 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 {
21426 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021427 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428 }
21429 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021430 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431}
21432
21433/*
21434 * ":delfunction {name}"
21435 */
21436 void
21437ex_delfunction(eap)
21438 exarg_T *eap;
21439{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021440 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 char_u *p;
21442 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021443 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444
21445 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021446 name = trans_function_name(&p, eap->skip, 0, &fudi);
21447 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021449 {
21450 if (fudi.fd_dict != NULL && !eap->skip)
21451 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 if (!ends_excmd(*skipwhite(p)))
21455 {
21456 vim_free(name);
21457 EMSG(_(e_trailing));
21458 return;
21459 }
21460 eap->nextcmd = check_nextcmd(p);
21461 if (eap->nextcmd != NULL)
21462 *p = NUL;
21463
21464 if (!eap->skip)
21465 fp = find_func(name);
21466 vim_free(name);
21467
21468 if (!eap->skip)
21469 {
21470 if (fp == NULL)
21471 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021472 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473 return;
21474 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021475 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 {
21477 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21478 return;
21479 }
21480
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021481 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021483 /* Delete the dict item that refers to the function, it will
21484 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021485 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021487 else
21488 func_free(fp);
21489 }
21490}
21491
21492/*
21493 * Free a function and remove it from the list of functions.
21494 */
21495 static void
21496func_free(fp)
21497 ufunc_T *fp;
21498{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021499 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021500
21501 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021502 ga_clear_strings(&(fp->uf_args));
21503 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021504#ifdef FEAT_PROFILE
21505 vim_free(fp->uf_tml_count);
21506 vim_free(fp->uf_tml_total);
21507 vim_free(fp->uf_tml_self);
21508#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021509
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021510 /* remove the function from the function hashtable */
21511 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21512 if (HASHITEM_EMPTY(hi))
21513 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021514 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021515 hash_remove(&func_hashtab, hi);
21516
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021517 vim_free(fp);
21518}
21519
21520/*
21521 * Unreference a Function: decrement the reference count and free it when it
21522 * becomes zero. Only for numbered functions.
21523 */
21524 static void
21525func_unref(name)
21526 char_u *name;
21527{
21528 ufunc_T *fp;
21529
21530 if (name != NULL && isdigit(*name))
21531 {
21532 fp = find_func(name);
21533 if (fp == NULL)
21534 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021535 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021536 {
21537 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021538 * when "uf_calls" becomes zero. */
21539 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021540 func_free(fp);
21541 }
21542 }
21543}
21544
21545/*
21546 * Count a reference to a Function.
21547 */
21548 static void
21549func_ref(name)
21550 char_u *name;
21551{
21552 ufunc_T *fp;
21553
21554 if (name != NULL && isdigit(*name))
21555 {
21556 fp = find_func(name);
21557 if (fp == NULL)
21558 EMSG2(_(e_intern2), "func_ref()");
21559 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021560 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561 }
21562}
21563
21564/*
21565 * Call a user function.
21566 */
21567 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021568call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569 ufunc_T *fp; /* pointer to function */
21570 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021571 typval_T *argvars; /* arguments */
21572 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 linenr_T firstline; /* first line of range */
21574 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021575 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576{
Bram Moolenaar33570922005-01-25 22:26:29 +000021577 char_u *save_sourcing_name;
21578 linenr_T save_sourcing_lnum;
21579 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021580 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021581 int save_did_emsg;
21582 static int depth = 0;
21583 dictitem_T *v;
21584 int fixvar_idx = 0; /* index in fixvar[] */
21585 int i;
21586 int ai;
21587 char_u numbuf[NUMBUFLEN];
21588 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021589#ifdef FEAT_PROFILE
21590 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021591 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593
21594 /* If depth of calling is getting too high, don't execute the function */
21595 if (depth >= p_mfd)
21596 {
21597 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021598 rettv->v_type = VAR_NUMBER;
21599 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 return;
21601 }
21602 ++depth;
21603
21604 line_breakcheck(); /* check for CTRL-C hit */
21605
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021606 fc = (funccall_T *)alloc(sizeof(funccall_T));
21607 fc->caller = current_funccal;
21608 current_funccal = fc;
21609 fc->func = fp;
21610 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021611 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021612 fc->linenr = 0;
21613 fc->returned = FALSE;
21614 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021616 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21617 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618
Bram Moolenaar33570922005-01-25 22:26:29 +000021619 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021620 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021621 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21622 * each argument variable and saves a lot of time.
21623 */
21624 /*
21625 * Init l: variables.
21626 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021627 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021628 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021629 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021630 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21631 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021632 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021633 name = v->di_key;
21634 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021635 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021636 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021637 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021638 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021639 v->di_tv.vval.v_dict = selfdict;
21640 ++selfdict->dv_refcount;
21641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021642
Bram Moolenaar33570922005-01-25 22:26:29 +000021643 /*
21644 * Init a: variables.
21645 * Set a:0 to "argcount".
21646 * Set a:000 to a list with room for the "..." arguments.
21647 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021648 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21649 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021650 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021651 /* Use "name" to avoid a warning from some compiler that checks the
21652 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021653 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021654 name = v->di_key;
21655 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021656 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021657 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021658 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021659 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021660 v->di_tv.vval.v_list = &fc->l_varlist;
21661 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21662 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21663 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021664
21665 /*
21666 * Set a:firstline to "firstline" and a:lastline to "lastline".
21667 * Set a:name to named arguments.
21668 * Set a:N to the "..." arguments.
21669 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021670 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021671 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021672 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021673 (varnumber_T)lastline);
21674 for (i = 0; i < argcount; ++i)
21675 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021676 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021677 if (ai < 0)
21678 /* named argument a:name */
21679 name = FUNCARG(fp, i);
21680 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021681 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021682 /* "..." argument a:1, a:2, etc. */
21683 sprintf((char *)numbuf, "%d", ai + 1);
21684 name = numbuf;
21685 }
21686 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21687 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021688 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021689 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21690 }
21691 else
21692 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021693 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21694 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021695 if (v == NULL)
21696 break;
21697 v->di_flags = DI_FLAGS_RO;
21698 }
21699 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021700 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021701
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021702 /* Note: the values are copied directly to avoid alloc/free.
21703 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021704 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021705 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021706
21707 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21708 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021709 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21710 fc->l_listitems[ai].li_tv = argvars[i];
21711 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021712 }
21713 }
21714
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715 /* Don't redraw while executing the function. */
21716 ++RedrawingDisabled;
21717 save_sourcing_name = sourcing_name;
21718 save_sourcing_lnum = sourcing_lnum;
21719 sourcing_lnum = 1;
21720 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021721 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 if (sourcing_name != NULL)
21723 {
21724 if (save_sourcing_name != NULL
21725 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21726 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21727 else
21728 STRCPY(sourcing_name, "function ");
21729 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21730
21731 if (p_verbose >= 12)
21732 {
21733 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021734 verbose_enter_scroll();
21735
Bram Moolenaar555b2802005-05-19 21:08:39 +000021736 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737 if (p_verbose >= 14)
21738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021740 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021741 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021742 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743
21744 msg_puts((char_u *)"(");
21745 for (i = 0; i < argcount; ++i)
21746 {
21747 if (i > 0)
21748 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021749 if (argvars[i].v_type == VAR_NUMBER)
21750 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751 else
21752 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021753 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21754 if (s != NULL)
21755 {
21756 trunc_string(s, buf, MSG_BUF_CLEN);
21757 msg_puts(buf);
21758 vim_free(tofree);
21759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760 }
21761 }
21762 msg_puts((char_u *)")");
21763 }
21764 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021765
21766 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 --no_wait_return;
21768 }
21769 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021770#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021771 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021772 {
21773 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21774 func_do_profile(fp);
21775 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021776 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021777 {
21778 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021779 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021780 profile_zero(&fp->uf_tm_children);
21781 }
21782 script_prof_save(&wait_start);
21783 }
21784#endif
21785
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021787 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021788 save_did_emsg = did_emsg;
21789 did_emsg = FALSE;
21790
21791 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021792 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21794
21795 --RedrawingDisabled;
21796
21797 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021798 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021800 clear_tv(rettv);
21801 rettv->v_type = VAR_NUMBER;
21802 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 }
21804
Bram Moolenaar05159a02005-02-26 23:04:13 +000021805#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021806 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021807 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021808 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021809 profile_end(&call_start);
21810 profile_sub_wait(&wait_start, &call_start);
21811 profile_add(&fp->uf_tm_total, &call_start);
21812 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021813 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021814 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021815 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21816 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021817 }
21818 }
21819#endif
21820
Bram Moolenaar071d4272004-06-13 20:20:40 +000021821 /* when being verbose, mention the return value */
21822 if (p_verbose >= 12)
21823 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021825 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021828 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021829 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021830 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021831 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000021832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000021834 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021835 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021836 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021837 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021838
Bram Moolenaar555b2802005-05-19 21:08:39 +000021839 /* The value may be very long. Skip the middle part, so that we
21840 * have some idea how it starts and ends. smsg() would always
21841 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021842 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021843 if (s != NULL)
21844 {
21845 trunc_string(s, buf, MSG_BUF_CLEN);
21846 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
21847 vim_free(tofree);
21848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849 }
21850 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021851
21852 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 --no_wait_return;
21854 }
21855
21856 vim_free(sourcing_name);
21857 sourcing_name = save_sourcing_name;
21858 sourcing_lnum = save_sourcing_lnum;
21859 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021860#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021861 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021862 script_prof_restore(&wait_start);
21863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864
21865 if (p_verbose >= 12 && sourcing_name != NULL)
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 *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021872
21873 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 --no_wait_return;
21875 }
21876
21877 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021878 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021880
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021881 /* If the a:000 list and the l: and a: dicts are not referenced we can
21882 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021883 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
21884 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
21885 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
21886 {
21887 free_funccal(fc, FALSE);
21888 }
21889 else
21890 {
21891 hashitem_T *hi;
21892 listitem_T *li;
21893 int todo;
21894
21895 /* "fc" is still in use. This can happen when returning "a:000" or
21896 * assigning "l:" to a global variable.
21897 * Link "fc" in the list for garbage collection later. */
21898 fc->caller = previous_funccal;
21899 previous_funccal = fc;
21900
21901 /* Make a copy of the a: variables, since we didn't do that above. */
21902 todo = (int)fc->l_avars.dv_hashtab.ht_used;
21903 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
21904 {
21905 if (!HASHITEM_EMPTY(hi))
21906 {
21907 --todo;
21908 v = HI2DI(hi);
21909 copy_tv(&v->di_tv, &v->di_tv);
21910 }
21911 }
21912
21913 /* Make a copy of the a:000 items, since we didn't do that above. */
21914 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21915 copy_tv(&li->li_tv, &li->li_tv);
21916 }
21917}
21918
21919/*
21920 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021921 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021922 */
21923 static int
21924can_free_funccal(fc, copyID)
21925 funccall_T *fc;
21926 int copyID;
21927{
21928 return (fc->l_varlist.lv_copyID != copyID
21929 && fc->l_vars.dv_copyID != copyID
21930 && fc->l_avars.dv_copyID != copyID);
21931}
21932
21933/*
21934 * Free "fc" and what it contains.
21935 */
21936 static void
21937free_funccal(fc, free_val)
21938 funccall_T *fc;
21939 int free_val; /* a: vars were allocated */
21940{
21941 listitem_T *li;
21942
21943 /* The a: variables typevals may not have been allocated, only free the
21944 * allocated variables. */
21945 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
21946
21947 /* free all l: variables */
21948 vars_clear(&fc->l_vars.dv_hashtab);
21949
21950 /* Free the a:000 variables if they were allocated. */
21951 if (free_val)
21952 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
21953 clear_tv(&li->li_tv);
21954
21955 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956}
21957
21958/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021959 * Add a number variable "name" to dict "dp" with value "nr".
21960 */
21961 static void
21962add_nr_var(dp, v, name, nr)
21963 dict_T *dp;
21964 dictitem_T *v;
21965 char *name;
21966 varnumber_T nr;
21967{
21968 STRCPY(v->di_key, name);
21969 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21970 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
21971 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021972 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021973 v->di_tv.vval.v_number = nr;
21974}
21975
21976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 * ":return [expr]"
21978 */
21979 void
21980ex_return(eap)
21981 exarg_T *eap;
21982{
21983 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021984 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 int returning = FALSE;
21986
21987 if (current_funccal == NULL)
21988 {
21989 EMSG(_("E133: :return not inside a function"));
21990 return;
21991 }
21992
21993 if (eap->skip)
21994 ++emsg_skip;
21995
21996 eap->nextcmd = NULL;
21997 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021998 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 {
22000 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022001 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022003 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004 }
22005 /* It's safer to return also on error. */
22006 else if (!eap->skip)
22007 {
22008 /*
22009 * Return unless the expression evaluation has been cancelled due to an
22010 * aborting error, an interrupt, or an exception.
22011 */
22012 if (!aborting())
22013 returning = do_return(eap, FALSE, TRUE, NULL);
22014 }
22015
22016 /* When skipping or the return gets pending, advance to the next command
22017 * in this line (!returning). Otherwise, ignore the rest of the line.
22018 * Following lines will be ignored by get_func_line(). */
22019 if (returning)
22020 eap->nextcmd = NULL;
22021 else if (eap->nextcmd == NULL) /* no argument */
22022 eap->nextcmd = check_nextcmd(arg);
22023
22024 if (eap->skip)
22025 --emsg_skip;
22026}
22027
22028/*
22029 * Return from a function. Possibly makes the return pending. Also called
22030 * for a pending return at the ":endtry" or after returning from an extra
22031 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022032 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022033 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034 * FALSE when the return gets pending.
22035 */
22036 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022037do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 exarg_T *eap;
22039 int reanimate;
22040 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022041 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042{
22043 int idx;
22044 struct condstack *cstack = eap->cstack;
22045
22046 if (reanimate)
22047 /* Undo the return. */
22048 current_funccal->returned = FALSE;
22049
22050 /*
22051 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22052 * not in its finally clause (which then is to be executed next) is found.
22053 * In this case, make the ":return" pending for execution at the ":endtry".
22054 * Otherwise, return normally.
22055 */
22056 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22057 if (idx >= 0)
22058 {
22059 cstack->cs_pending[idx] = CSTP_RETURN;
22060
22061 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022062 /* A pending return again gets pending. "rettv" points to an
22063 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022065 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 else
22067 {
22068 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022069 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022071 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022073 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 {
22075 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022076 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022077 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078 else
22079 EMSG(_(e_outofmem));
22080 }
22081 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022082 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083
22084 if (reanimate)
22085 {
22086 /* The pending return value could be overwritten by a ":return"
22087 * without argument in a finally clause; reset the default
22088 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022089 current_funccal->rettv->v_type = VAR_NUMBER;
22090 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 }
22092 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022093 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 }
22095 else
22096 {
22097 current_funccal->returned = TRUE;
22098
22099 /* If the return is carried out now, store the return value. For
22100 * a return immediately after reanimation, the value is already
22101 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022102 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022104 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022105 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022107 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 }
22109 }
22110
22111 return idx < 0;
22112}
22113
22114/*
22115 * Free the variable with a pending return value.
22116 */
22117 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022118discard_pending_return(rettv)
22119 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120{
Bram Moolenaar33570922005-01-25 22:26:29 +000022121 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122}
22123
22124/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022125 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126 * is an allocated string. Used by report_pending() for verbose messages.
22127 */
22128 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022129get_return_cmd(rettv)
22130 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022132 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022133 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022134 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022136 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022137 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022138 if (s == NULL)
22139 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022140
22141 STRCPY(IObuff, ":return ");
22142 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22143 if (STRLEN(s) + 8 >= IOSIZE)
22144 STRCPY(IObuff + IOSIZE - 4, "...");
22145 vim_free(tofree);
22146 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147}
22148
22149/*
22150 * Get next function line.
22151 * Called by do_cmdline() to get the next line.
22152 * Returns allocated string, or NULL for end of function.
22153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 char_u *
22155get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022156 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022158 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159{
Bram Moolenaar33570922005-01-25 22:26:29 +000022160 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022161 ufunc_T *fp = fcp->func;
22162 char_u *retval;
22163 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164
22165 /* If breakpoints have been added/deleted need to check for it. */
22166 if (fcp->dbg_tick != debug_tick)
22167 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022168 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 sourcing_lnum);
22170 fcp->dbg_tick = debug_tick;
22171 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022172#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022173 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022174 func_line_end(cookie);
22175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176
Bram Moolenaar05159a02005-02-26 23:04:13 +000022177 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022178 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22179 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 retval = NULL;
22181 else
22182 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022183 /* Skip NULL lines (continuation lines). */
22184 while (fcp->linenr < gap->ga_len
22185 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22186 ++fcp->linenr;
22187 if (fcp->linenr >= gap->ga_len)
22188 retval = NULL;
22189 else
22190 {
22191 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22192 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022193#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022194 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022195 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022196#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 }
22199
22200 /* Did we encounter a breakpoint? */
22201 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22202 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022203 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022205 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 sourcing_lnum);
22207 fcp->dbg_tick = debug_tick;
22208 }
22209
22210 return retval;
22211}
22212
Bram Moolenaar05159a02005-02-26 23:04:13 +000022213#if defined(FEAT_PROFILE) || defined(PROTO)
22214/*
22215 * Called when starting to read a function line.
22216 * "sourcing_lnum" must be correct!
22217 * When skipping lines it may not actually be executed, but we won't find out
22218 * until later and we need to store the time now.
22219 */
22220 void
22221func_line_start(cookie)
22222 void *cookie;
22223{
22224 funccall_T *fcp = (funccall_T *)cookie;
22225 ufunc_T *fp = fcp->func;
22226
22227 if (fp->uf_profiling && sourcing_lnum >= 1
22228 && sourcing_lnum <= fp->uf_lines.ga_len)
22229 {
22230 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022231 /* Skip continuation lines. */
22232 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22233 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022234 fp->uf_tml_execed = FALSE;
22235 profile_start(&fp->uf_tml_start);
22236 profile_zero(&fp->uf_tml_children);
22237 profile_get_wait(&fp->uf_tml_wait);
22238 }
22239}
22240
22241/*
22242 * Called when actually executing a function line.
22243 */
22244 void
22245func_line_exec(cookie)
22246 void *cookie;
22247{
22248 funccall_T *fcp = (funccall_T *)cookie;
22249 ufunc_T *fp = fcp->func;
22250
22251 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22252 fp->uf_tml_execed = TRUE;
22253}
22254
22255/*
22256 * Called when done with a function line.
22257 */
22258 void
22259func_line_end(cookie)
22260 void *cookie;
22261{
22262 funccall_T *fcp = (funccall_T *)cookie;
22263 ufunc_T *fp = fcp->func;
22264
22265 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22266 {
22267 if (fp->uf_tml_execed)
22268 {
22269 ++fp->uf_tml_count[fp->uf_tml_idx];
22270 profile_end(&fp->uf_tml_start);
22271 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022272 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022273 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22274 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022275 }
22276 fp->uf_tml_idx = -1;
22277 }
22278}
22279#endif
22280
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281/*
22282 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022283 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 */
22285 int
22286func_has_ended(cookie)
22287 void *cookie;
22288{
Bram Moolenaar33570922005-01-25 22:26:29 +000022289 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290
22291 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22292 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022293 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 || fcp->returned);
22295}
22296
22297/*
22298 * return TRUE if cookie indicates a function which "abort"s on errors.
22299 */
22300 int
22301func_has_abort(cookie)
22302 void *cookie;
22303{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022304 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305}
22306
22307#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22308typedef enum
22309{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022310 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22311 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22312 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313} var_flavour_T;
22314
22315static var_flavour_T var_flavour __ARGS((char_u *varname));
22316
22317 static var_flavour_T
22318var_flavour(varname)
22319 char_u *varname;
22320{
22321 char_u *p = varname;
22322
22323 if (ASCII_ISUPPER(*p))
22324 {
22325 while (*(++p))
22326 if (ASCII_ISLOWER(*p))
22327 return VAR_FLAVOUR_SESSION;
22328 return VAR_FLAVOUR_VIMINFO;
22329 }
22330 else
22331 return VAR_FLAVOUR_DEFAULT;
22332}
22333#endif
22334
22335#if defined(FEAT_VIMINFO) || defined(PROTO)
22336/*
22337 * Restore global vars that start with a capital from the viminfo file
22338 */
22339 int
22340read_viminfo_varlist(virp, writing)
22341 vir_T *virp;
22342 int writing;
22343{
22344 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022345 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022346 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347
22348 if (!writing && (find_viminfo_parameter('!') != NULL))
22349 {
22350 tab = vim_strchr(virp->vir_line + 1, '\t');
22351 if (tab != NULL)
22352 {
22353 *tab++ = '\0'; /* isolate the variable name */
22354 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022355 type = VAR_STRING;
22356#ifdef FEAT_FLOAT
22357 else if (*tab == 'F')
22358 type = VAR_FLOAT;
22359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022360
22361 tab = vim_strchr(tab, '\t');
22362 if (tab != NULL)
22363 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022364 tv.v_type = type;
22365 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022366 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022368#ifdef FEAT_FLOAT
22369 else if (type == VAR_FLOAT)
22370 (void)string2float(tab + 1, &tv.vval.v_float);
22371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022373 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022374 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022375 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022376 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 }
22378 }
22379 }
22380
22381 return viminfo_readline(virp);
22382}
22383
22384/*
22385 * Write global vars that start with a capital to the viminfo file
22386 */
22387 void
22388write_viminfo_varlist(fp)
22389 FILE *fp;
22390{
Bram Moolenaar33570922005-01-25 22:26:29 +000022391 hashitem_T *hi;
22392 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022393 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022394 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022395 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022396 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022397 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398
22399 if (find_viminfo_parameter('!') == NULL)
22400 return;
22401
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022402 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022403
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022404 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022405 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022407 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022409 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022410 this_var = HI2DI(hi);
22411 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022412 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022413 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022414 {
22415 case VAR_STRING: s = "STR"; break;
22416 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022417#ifdef FEAT_FLOAT
22418 case VAR_FLOAT: s = "FLO"; break;
22419#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022420 default: continue;
22421 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022422 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022423 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022424 if (p != NULL)
22425 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022426 vim_free(tofree);
22427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 }
22429 }
22430}
22431#endif
22432
22433#if defined(FEAT_SESSION) || defined(PROTO)
22434 int
22435store_session_globals(fd)
22436 FILE *fd;
22437{
Bram Moolenaar33570922005-01-25 22:26:29 +000022438 hashitem_T *hi;
22439 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022440 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441 char_u *p, *t;
22442
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022443 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022444 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022446 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022448 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022449 this_var = HI2DI(hi);
22450 if ((this_var->di_tv.v_type == VAR_NUMBER
22451 || this_var->di_tv.v_type == VAR_STRING)
22452 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022453 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022454 /* Escape special characters with a backslash. Turn a LF and
22455 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022456 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022457 (char_u *)"\\\"\n\r");
22458 if (p == NULL) /* out of memory */
22459 break;
22460 for (t = p; *t != NUL; ++t)
22461 if (*t == '\n')
22462 *t = 'n';
22463 else if (*t == '\r')
22464 *t = 'r';
22465 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022466 this_var->di_key,
22467 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22468 : ' ',
22469 p,
22470 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22471 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022472 || put_eol(fd) == FAIL)
22473 {
22474 vim_free(p);
22475 return FAIL;
22476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477 vim_free(p);
22478 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022479#ifdef FEAT_FLOAT
22480 else if (this_var->di_tv.v_type == VAR_FLOAT
22481 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22482 {
22483 float_T f = this_var->di_tv.vval.v_float;
22484 int sign = ' ';
22485
22486 if (f < 0)
22487 {
22488 f = -f;
22489 sign = '-';
22490 }
22491 if ((fprintf(fd, "let %s = %c&%f",
22492 this_var->di_key, sign, f) < 0)
22493 || put_eol(fd) == FAIL)
22494 return FAIL;
22495 }
22496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 }
22498 }
22499 return OK;
22500}
22501#endif
22502
Bram Moolenaar661b1822005-07-28 22:36:45 +000022503/*
22504 * Display script name where an item was last set.
22505 * Should only be invoked when 'verbose' is non-zero.
22506 */
22507 void
22508last_set_msg(scriptID)
22509 scid_T scriptID;
22510{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022511 char_u *p;
22512
Bram Moolenaar661b1822005-07-28 22:36:45 +000022513 if (scriptID != 0)
22514 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022515 p = home_replace_save(NULL, get_scriptname(scriptID));
22516 if (p != NULL)
22517 {
22518 verbose_enter();
22519 MSG_PUTS(_("\n\tLast set from "));
22520 MSG_PUTS(p);
22521 vim_free(p);
22522 verbose_leave();
22523 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022524 }
22525}
22526
Bram Moolenaard812df62008-11-09 12:46:09 +000022527/*
22528 * List v:oldfiles in a nice way.
22529 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022530 void
22531ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022532 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022533{
22534 list_T *l = vimvars[VV_OLDFILES].vv_list;
22535 listitem_T *li;
22536 int nr = 0;
22537
22538 if (l == NULL)
22539 msg((char_u *)_("No old files"));
22540 else
22541 {
22542 msg_start();
22543 msg_scroll = TRUE;
22544 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22545 {
22546 msg_outnum((long)++nr);
22547 MSG_PUTS(": ");
22548 msg_outtrans(get_tv_string(&li->li_tv));
22549 msg_putchar('\n');
22550 out_flush(); /* output one line at a time */
22551 ui_breakcheck();
22552 }
22553 /* Assume "got_int" was set to truncate the listing. */
22554 got_int = FALSE;
22555
22556#ifdef FEAT_BROWSE_CMD
22557 if (cmdmod.browse)
22558 {
22559 quit_more = FALSE;
22560 nr = prompt_for_number(FALSE);
22561 msg_starthere();
22562 if (nr > 0)
22563 {
22564 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22565 (long)nr);
22566
22567 if (p != NULL)
22568 {
22569 p = expand_env_save(p);
22570 eap->arg = p;
22571 eap->cmdidx = CMD_edit;
22572 cmdmod.browse = FALSE;
22573 do_exedit(eap, NULL);
22574 vim_free(p);
22575 }
22576 }
22577 }
22578#endif
22579 }
22580}
22581
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582#endif /* FEAT_EVAL */
22583
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022585#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586
22587#ifdef WIN3264
22588/*
22589 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22590 */
22591static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22592static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22593static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22594
22595/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022596 * Get the short path (8.3) for the filename in "fnamep".
22597 * Only works for a valid file name.
22598 * When the path gets longer "fnamep" is changed and the allocated buffer
22599 * is put in "bufp".
22600 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22601 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 */
22603 static int
22604get_short_pathname(fnamep, bufp, fnamelen)
22605 char_u **fnamep;
22606 char_u **bufp;
22607 int *fnamelen;
22608{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022609 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 char_u *newbuf;
22611
22612 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 l = GetShortPathName(*fnamep, *fnamep, len);
22614 if (l > len - 1)
22615 {
22616 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022617 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 newbuf = vim_strnsave(*fnamep, l);
22619 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022620 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621
22622 vim_free(*bufp);
22623 *fnamep = *bufp = newbuf;
22624
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022625 /* Really should always succeed, as the buffer is big enough. */
22626 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 }
22628
22629 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022630 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631}
22632
22633/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022634 * Get the short path (8.3) for the filename in "fname". The converted
22635 * path is returned in "bufp".
22636 *
22637 * Some of the directories specified in "fname" may not exist. This function
22638 * will shorten the existing directories at the beginning of the path and then
22639 * append the remaining non-existing path.
22640 *
22641 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022642 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022643 * bufp - Pointer to an allocated buffer for the filename.
22644 * fnamelen - Length of the filename pointed to by fname
22645 *
22646 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 */
22648 static int
22649shortpath_for_invalid_fname(fname, bufp, fnamelen)
22650 char_u **fname;
22651 char_u **bufp;
22652 int *fnamelen;
22653{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022654 char_u *short_fname, *save_fname, *pbuf_unused;
22655 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022657 int old_len, len;
22658 int new_len, sfx_len;
22659 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660
22661 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022662 old_len = *fnamelen;
22663 save_fname = vim_strnsave(*fname, old_len);
22664 pbuf_unused = NULL;
22665 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022667 endp = save_fname + old_len - 1; /* Find the end of the copy */
22668 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022670 /*
22671 * Try shortening the supplied path till it succeeds by removing one
22672 * directory at a time from the tail of the path.
22673 */
22674 len = 0;
22675 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022677 /* go back one path-separator */
22678 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22679 --endp;
22680 if (endp <= save_fname)
22681 break; /* processed the complete path */
22682
22683 /*
22684 * Replace the path separator with a NUL and try to shorten the
22685 * resulting path.
22686 */
22687 ch = *endp;
22688 *endp = 0;
22689 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022690 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022691 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22692 {
22693 retval = FAIL;
22694 goto theend;
22695 }
22696 *endp = ch; /* preserve the string */
22697
22698 if (len > 0)
22699 break; /* successfully shortened the path */
22700
22701 /* failed to shorten the path. Skip the path separator */
22702 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 }
22704
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022705 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022707 /*
22708 * Succeeded in shortening the path. Now concatenate the shortened
22709 * path with the remaining path at the tail.
22710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022712 /* Compute the length of the new path. */
22713 sfx_len = (int)(save_endp - endp) + 1;
22714 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022716 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022718 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022720 /* There is not enough space in the currently allocated string,
22721 * copy it to a buffer big enough. */
22722 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022724 {
22725 retval = FAIL;
22726 goto theend;
22727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 }
22729 else
22730 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022731 /* Transfer short_fname to the main buffer (it's big enough),
22732 * unless get_short_pathname() did its work in-place. */
22733 *fname = *bufp = save_fname;
22734 if (short_fname != save_fname)
22735 vim_strncpy(save_fname, short_fname, len);
22736 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022738
22739 /* concat the not-shortened part of the path */
22740 vim_strncpy(*fname + len, endp, sfx_len);
22741 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022743
22744theend:
22745 vim_free(pbuf_unused);
22746 vim_free(save_fname);
22747
22748 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749}
22750
22751/*
22752 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022753 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 */
22755 static int
22756shortpath_for_partial(fnamep, bufp, fnamelen)
22757 char_u **fnamep;
22758 char_u **bufp;
22759 int *fnamelen;
22760{
22761 int sepcount, len, tflen;
22762 char_u *p;
22763 char_u *pbuf, *tfname;
22764 int hasTilde;
22765
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022766 /* Count up the path separators from the RHS.. so we know which part
22767 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022769 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770 if (vim_ispathsep(*p))
22771 ++sepcount;
22772
22773 /* Need full path first (use expand_env() to remove a "~/") */
22774 hasTilde = (**fnamep == '~');
22775 if (hasTilde)
22776 pbuf = tfname = expand_env_save(*fnamep);
22777 else
22778 pbuf = tfname = FullName_save(*fnamep, FALSE);
22779
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022780 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022782 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22783 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022784
22785 if (len == 0)
22786 {
22787 /* Don't have a valid filename, so shorten the rest of the
22788 * path if we can. This CAN give us invalid 8.3 filenames, but
22789 * there's not a lot of point in guessing what it might be.
22790 */
22791 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022792 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22793 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 }
22795
22796 /* Count the paths backward to find the beginning of the desired string. */
22797 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022798 {
22799#ifdef FEAT_MBYTE
22800 if (has_mbyte)
22801 p -= mb_head_off(tfname, p);
22802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 if (vim_ispathsep(*p))
22804 {
22805 if (sepcount == 0 || (hasTilde && sepcount == 1))
22806 break;
22807 else
22808 sepcount --;
22809 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 if (hasTilde)
22812 {
22813 --p;
22814 if (p >= tfname)
22815 *p = '~';
22816 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022817 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818 }
22819 else
22820 ++p;
22821
22822 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22823 vim_free(*bufp);
22824 *fnamelen = (int)STRLEN(p);
22825 *bufp = pbuf;
22826 *fnamep = p;
22827
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022828 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829}
22830#endif /* WIN3264 */
22831
22832/*
22833 * Adjust a filename, according to a string of modifiers.
22834 * *fnamep must be NUL terminated when called. When returning, the length is
22835 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022836 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 * When there is an error, *fnamep is set to NULL.
22838 */
22839 int
22840modify_fname(src, usedlen, fnamep, bufp, fnamelen)
22841 char_u *src; /* string with modifiers */
22842 int *usedlen; /* characters after src that are used */
22843 char_u **fnamep; /* file name so far */
22844 char_u **bufp; /* buffer for allocated file name or NULL */
22845 int *fnamelen; /* length of fnamep */
22846{
22847 int valid = 0;
22848 char_u *tail;
22849 char_u *s, *p, *pbuf;
22850 char_u dirname[MAXPATHL];
22851 int c;
22852 int has_fullname = 0;
22853#ifdef WIN3264
22854 int has_shortname = 0;
22855#endif
22856
22857repeat:
22858 /* ":p" - full path/file_name */
22859 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
22860 {
22861 has_fullname = 1;
22862
22863 valid |= VALID_PATH;
22864 *usedlen += 2;
22865
22866 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22867 if ((*fnamep)[0] == '~'
22868#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22869 && ((*fnamep)[1] == '/'
22870# ifdef BACKSLASH_IN_FILENAME
22871 || (*fnamep)[1] == '\\'
22872# endif
22873 || (*fnamep)[1] == NUL)
22874
22875#endif
22876 )
22877 {
22878 *fnamep = expand_env_save(*fnamep);
22879 vim_free(*bufp); /* free any allocated file name */
22880 *bufp = *fnamep;
22881 if (*fnamep == NULL)
22882 return -1;
22883 }
22884
22885 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022886 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 {
22888 if (vim_ispathsep(*p)
22889 && p[1] == '.'
22890 && (p[2] == NUL
22891 || vim_ispathsep(p[2])
22892 || (p[2] == '.'
22893 && (p[3] == NUL || vim_ispathsep(p[3])))))
22894 break;
22895 }
22896
22897 /* FullName_save() is slow, don't use it when not needed. */
22898 if (*p != NUL || !vim_isAbsName(*fnamep))
22899 {
22900 *fnamep = FullName_save(*fnamep, *p != NUL);
22901 vim_free(*bufp); /* free any allocated file name */
22902 *bufp = *fnamep;
22903 if (*fnamep == NULL)
22904 return -1;
22905 }
22906
22907 /* Append a path separator to a directory. */
22908 if (mch_isdir(*fnamep))
22909 {
22910 /* Make room for one or two extra characters. */
22911 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
22912 vim_free(*bufp); /* free any allocated file name */
22913 *bufp = *fnamep;
22914 if (*fnamep == NULL)
22915 return -1;
22916 add_pathsep(*fnamep);
22917 }
22918 }
22919
22920 /* ":." - path relative to the current directory */
22921 /* ":~" - path relative to the home directory */
22922 /* ":8" - shortname path - postponed till after */
22923 while (src[*usedlen] == ':'
22924 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
22925 {
22926 *usedlen += 2;
22927 if (c == '8')
22928 {
22929#ifdef WIN3264
22930 has_shortname = 1; /* Postpone this. */
22931#endif
22932 continue;
22933 }
22934 pbuf = NULL;
22935 /* Need full path first (use expand_env() to remove a "~/") */
22936 if (!has_fullname)
22937 {
22938 if (c == '.' && **fnamep == '~')
22939 p = pbuf = expand_env_save(*fnamep);
22940 else
22941 p = pbuf = FullName_save(*fnamep, FALSE);
22942 }
22943 else
22944 p = *fnamep;
22945
22946 has_fullname = 0;
22947
22948 if (p != NULL)
22949 {
22950 if (c == '.')
22951 {
22952 mch_dirname(dirname, MAXPATHL);
22953 s = shorten_fname(p, dirname);
22954 if (s != NULL)
22955 {
22956 *fnamep = s;
22957 if (pbuf != NULL)
22958 {
22959 vim_free(*bufp); /* free any allocated file name */
22960 *bufp = pbuf;
22961 pbuf = NULL;
22962 }
22963 }
22964 }
22965 else
22966 {
22967 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
22968 /* Only replace it when it starts with '~' */
22969 if (*dirname == '~')
22970 {
22971 s = vim_strsave(dirname);
22972 if (s != NULL)
22973 {
22974 *fnamep = s;
22975 vim_free(*bufp);
22976 *bufp = s;
22977 }
22978 }
22979 }
22980 vim_free(pbuf);
22981 }
22982 }
22983
22984 tail = gettail(*fnamep);
22985 *fnamelen = (int)STRLEN(*fnamep);
22986
22987 /* ":h" - head, remove "/file_name", can be repeated */
22988 /* Don't remove the first "/" or "c:\" */
22989 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
22990 {
22991 valid |= VALID_HEAD;
22992 *usedlen += 2;
22993 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022994 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000022995 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 *fnamelen = (int)(tail - *fnamep);
22997#ifdef VMS
22998 if (*fnamelen > 0)
22999 *fnamelen += 1; /* the path separator is part of the path */
23000#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023001 if (*fnamelen == 0)
23002 {
23003 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23004 p = vim_strsave((char_u *)".");
23005 if (p == NULL)
23006 return -1;
23007 vim_free(*bufp);
23008 *bufp = *fnamep = tail = p;
23009 *fnamelen = 1;
23010 }
23011 else
23012 {
23013 while (tail > s && !after_pathsep(s, tail))
23014 mb_ptr_back(*fnamep, tail);
23015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 }
23017
23018 /* ":8" - shortname */
23019 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23020 {
23021 *usedlen += 2;
23022#ifdef WIN3264
23023 has_shortname = 1;
23024#endif
23025 }
23026
23027#ifdef WIN3264
23028 /* Check shortname after we have done 'heads' and before we do 'tails'
23029 */
23030 if (has_shortname)
23031 {
23032 pbuf = NULL;
23033 /* Copy the string if it is shortened by :h */
23034 if (*fnamelen < (int)STRLEN(*fnamep))
23035 {
23036 p = vim_strnsave(*fnamep, *fnamelen);
23037 if (p == 0)
23038 return -1;
23039 vim_free(*bufp);
23040 *bufp = *fnamep = p;
23041 }
23042
23043 /* Split into two implementations - makes it easier. First is where
23044 * there isn't a full name already, second is where there is.
23045 */
23046 if (!has_fullname && !vim_isAbsName(*fnamep))
23047 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023048 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049 return -1;
23050 }
23051 else
23052 {
23053 int l;
23054
23055 /* Simple case, already have the full-name
23056 * Nearly always shorter, so try first time. */
23057 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023058 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059 return -1;
23060
23061 if (l == 0)
23062 {
23063 /* Couldn't find the filename.. search the paths.
23064 */
23065 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023066 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 return -1;
23068 }
23069 *fnamelen = l;
23070 }
23071 }
23072#endif /* WIN3264 */
23073
23074 /* ":t" - tail, just the basename */
23075 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23076 {
23077 *usedlen += 2;
23078 *fnamelen -= (int)(tail - *fnamep);
23079 *fnamep = tail;
23080 }
23081
23082 /* ":e" - extension, can be repeated */
23083 /* ":r" - root, without extension, can be repeated */
23084 while (src[*usedlen] == ':'
23085 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23086 {
23087 /* find a '.' in the tail:
23088 * - for second :e: before the current fname
23089 * - otherwise: The last '.'
23090 */
23091 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23092 s = *fnamep - 2;
23093 else
23094 s = *fnamep + *fnamelen - 1;
23095 for ( ; s > tail; --s)
23096 if (s[0] == '.')
23097 break;
23098 if (src[*usedlen + 1] == 'e') /* :e */
23099 {
23100 if (s > tail)
23101 {
23102 *fnamelen += (int)(*fnamep - (s + 1));
23103 *fnamep = s + 1;
23104#ifdef VMS
23105 /* cut version from the extension */
23106 s = *fnamep + *fnamelen - 1;
23107 for ( ; s > *fnamep; --s)
23108 if (s[0] == ';')
23109 break;
23110 if (s > *fnamep)
23111 *fnamelen = s - *fnamep;
23112#endif
23113 }
23114 else if (*fnamep <= tail)
23115 *fnamelen = 0;
23116 }
23117 else /* :r */
23118 {
23119 if (s > tail) /* remove one extension */
23120 *fnamelen = (int)(s - *fnamep);
23121 }
23122 *usedlen += 2;
23123 }
23124
23125 /* ":s?pat?foo?" - substitute */
23126 /* ":gs?pat?foo?" - global substitute */
23127 if (src[*usedlen] == ':'
23128 && (src[*usedlen + 1] == 's'
23129 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23130 {
23131 char_u *str;
23132 char_u *pat;
23133 char_u *sub;
23134 int sep;
23135 char_u *flags;
23136 int didit = FALSE;
23137
23138 flags = (char_u *)"";
23139 s = src + *usedlen + 2;
23140 if (src[*usedlen + 1] == 'g')
23141 {
23142 flags = (char_u *)"g";
23143 ++s;
23144 }
23145
23146 sep = *s++;
23147 if (sep)
23148 {
23149 /* find end of pattern */
23150 p = vim_strchr(s, sep);
23151 if (p != NULL)
23152 {
23153 pat = vim_strnsave(s, (int)(p - s));
23154 if (pat != NULL)
23155 {
23156 s = p + 1;
23157 /* find end of substitution */
23158 p = vim_strchr(s, sep);
23159 if (p != NULL)
23160 {
23161 sub = vim_strnsave(s, (int)(p - s));
23162 str = vim_strnsave(*fnamep, *fnamelen);
23163 if (sub != NULL && str != NULL)
23164 {
23165 *usedlen = (int)(p + 1 - src);
23166 s = do_string_sub(str, pat, sub, flags);
23167 if (s != NULL)
23168 {
23169 *fnamep = s;
23170 *fnamelen = (int)STRLEN(s);
23171 vim_free(*bufp);
23172 *bufp = s;
23173 didit = TRUE;
23174 }
23175 }
23176 vim_free(sub);
23177 vim_free(str);
23178 }
23179 vim_free(pat);
23180 }
23181 }
23182 /* after using ":s", repeat all the modifiers */
23183 if (didit)
23184 goto repeat;
23185 }
23186 }
23187
23188 return valid;
23189}
23190
23191/*
23192 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23193 * "flags" can be "g" to do a global substitute.
23194 * Returns an allocated string, NULL for error.
23195 */
23196 char_u *
23197do_string_sub(str, pat, sub, flags)
23198 char_u *str;
23199 char_u *pat;
23200 char_u *sub;
23201 char_u *flags;
23202{
23203 int sublen;
23204 regmatch_T regmatch;
23205 int i;
23206 int do_all;
23207 char_u *tail;
23208 garray_T ga;
23209 char_u *ret;
23210 char_u *save_cpo;
23211
23212 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23213 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023214 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215
23216 ga_init2(&ga, 1, 200);
23217
23218 do_all = (flags[0] == 'g');
23219
23220 regmatch.rm_ic = p_ic;
23221 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23222 if (regmatch.regprog != NULL)
23223 {
23224 tail = str;
23225 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23226 {
23227 /*
23228 * Get some space for a temporary buffer to do the substitution
23229 * into. It will contain:
23230 * - The text up to where the match is.
23231 * - The substituted text.
23232 * - The text after the match.
23233 */
23234 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23235 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23236 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23237 {
23238 ga_clear(&ga);
23239 break;
23240 }
23241
23242 /* copy the text up to where the match is */
23243 i = (int)(regmatch.startp[0] - tail);
23244 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23245 /* add the substituted text */
23246 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23247 + ga.ga_len + i, TRUE, TRUE, FALSE);
23248 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023249 /* avoid getting stuck on a match with an empty string */
23250 if (tail == regmatch.endp[0])
23251 {
23252 if (*tail == NUL)
23253 break;
23254 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23255 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 }
23257 else
23258 {
23259 tail = regmatch.endp[0];
23260 if (*tail == NUL)
23261 break;
23262 }
23263 if (!do_all)
23264 break;
23265 }
23266
23267 if (ga.ga_data != NULL)
23268 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23269
23270 vim_free(regmatch.regprog);
23271 }
23272
23273 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23274 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023275 if (p_cpo == empty_option)
23276 p_cpo = save_cpo;
23277 else
23278 /* Darn, evaluating {sub} expression changed the value. */
23279 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280
23281 return ret;
23282}
23283
23284#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */